tc39 / proposal-symbols-as-weakmap-keys

Permit Symbols as keys in WeakMaps, entries in WeakSets and WeakRefs, and registered in FinalizationRegistries
http://tc39.es/proposal-symbols-as-weakmap-keys
MIT License
90 stars 7 forks source link

clarification on "not needed anymore" and garbage collection #2

Closed mheiber closed 4 years ago

mheiber commented 4 years ago

The README is really clear!

Re:

Boxes will not keep objects indefinitely in memory. When a box is not needed anymore, the attached object can be let go.

Does "a box is not needed anymore" mean the same thing as "a box is inaccessible"?

Is this sentence saying the same thing as "when the box is garbage-collected, the attached object can be garbage-collected as well"?

If so, we might want to tweak the semantics so that the boxed object is eligible for collection when the object and its box are inaccessible.

const obj = { hello: "world" };
const box = Box(obj);
window.leakObj = obj; // the box can be collected, but the object itself must live on
littledan commented 4 years ago

No semantic tweaks are in the cards here, but we should definitely do what we can to clarify wording, since people seem to keep getting tripped up when it comes to WeakRefs, WeakMaps, RefCollections and now Box. The whole process of garbage collection is unobservable and people shouldn't really be thinking about things actively getting garbage-collected at certain predictable times.

If we want to get correct/technical/pedantic about it, we could talk about "reachability" or "liveness", the latter of which is used in the WeakRefs spec. I don't think we should use any wording that implies that, once something is inaccessible, it's let go--it's just not true, since the GC can be arbitrarily lazy (e.g., collecting the Box a while before collecting the object).

Instead, people should be thinking, "Boxes are a way to get at objects, without having to hold them alive forever by linking them from the global object, and without having to pass around data structures explicitly just to minimize that extended lifetime."

I'm not really sure how to frame this in the document, since the things that I tend to write about these topics end up confusing people...

mheiber commented 4 years ago

I agree with you point, but want to also illustrate that the example I gave points to a genuine difference in semantics, probably accidental. We almost certainly don't want objects that are still referenced to be garbage-collected just because a box that pointed to it is no longer referenced.

littledan commented 4 years ago

At the risk of being pedantic, I'd categorize any text that implies something different from the semantics I was trying to describe above as "an error in the text" rather than "a genuine difference in semantics". Messing with how GC works in those sorts of ways just isn't in the available design space here, and I think we all agree that we're not proposing something like that, though the text may have errors.

littledan commented 4 years ago

Hopefully the language about garbage collection has been clarified now; please file another issue if there's anything additional to clear up.