On Mon Jul 17 16:06:18 2023 +0000, Gabriel Ivăncescu wrote:
There could be cyclic refs in the entry value's path. If we unconditionally visit them from both the key and the WeakMap, they will be marked alive when either of them is alive, not when *both* of them are. For example if we keep the WeakMap alive (local variable for example), the entries will also remain alive, even if the key is released. Traversing basically unmarks the entire path from garbage collection; only one traversal is enough to do this, it's an "OR" operation, not an "AND". That's why we need to add those conditions I mentioned.
We wouldn't unconditionally visit value:
During GC_TRAVERSE, you could traverse weak ref entry both when traversing the object itself and the weak map. On the first visit, just mark the entry as visited. On the second visit, continue visiting the value.
Note that the marking the entry is not the same as "mark alive" in GC. It's purely a marker to know if we should visit the value or leave it for the potential second traversal.