3 ms·
I just set a "visited" bit in the data structure while the code walks it. There's a cycle if the bit is already set. It's the same algorithm as yours, but a lot
by WalterBright 8y ago
I just set a "visited" bit in the data structure while the code walks it. There's a cycle if the bit is already set. It's the same algorithm as yours, but a lot cheaper.
- jmts 8y agoPerhaps cheaper in space, but you will have to do an initial pass over the entire structure to zero your visited bits first, so it may not be cheaper in time.
- WalterBright 8y agoYou have to walk the hash table again to free it.
- ufo 8y agoThe GC already needs to sweep through the whole allocated memory to find the unreacheble objects to be freed. Flipping a bit while doing this isnt a big deal.
- stcredzero 8y agoJust ensure the bits are always zero on allocation, use DFS, and ensure you re-zero the bits on the way out.
- WalterBright 8y agoSince the first pass quit when it finds the first marked node, the zeroing can traverse the structure, erasing marked bits, and stopping at the first 0. The entire structure doesn't have to be visited.
- stcredzero 8y agoThe entire structure doesn't have to be visited. We are both saying this.