[Gc] weak maps and libgc
wingo at pobox.com
Thu Feb 23 13:15:45 PST 2012
Picking up an old thread, I was working on Guile's weak maps recently.
In the development branch, they are implemented using disappearing
links, and periodic traversal to flush out old entries.
Ivan suggested using finalizers to remove old entries, which is a good
idea. I tried replacing disappearing links with finalizers, but you can
guess the outcome: with custom hash/equality predicates, it would be
possible for lookup to race against removal from finalizers.
So, disappearing links + finalizers seems to be the thing. However, we
have a problem: guardians.
When an object is placed in a guardian, Guile chains a finalizer that
resuscitates the object. (We are using Java finalization.) Then user
code can periodically call the guardian to see if there are collectable
objects. If there are, the guardian returns them, and user code can do
what it wants with the object -- probably cleanup.
The problem comes in when user code needs to associate information with
the object, but the object doesn't have a field for it. In that case,
user code probably uses a weak map. The associations in Guile's weak
maps are documented as persisting past the resuscitation of an object by
a guardian. However, disappearing links are cleared before finalizers
run, so in practice we're losing that information.
It seems that these two things are in conflict: weak map entries
persisting past finalization (but being removed after collection
ultimately happens), versus the desire to see entries disappear from
weak maps "atomically" -- that is, disappearing links are synchronized
with the collector, and the finalizer doesn't affect the semantics of
the map. Whereas if there isn't a disappearing link, there is that
lookup / removal race I mentioned.
I'm not sure what to do. In the end, I think I'm going to have to
expose this tradeoff to my users, and make them choose. But I wish I
didn't have to do that.
Does anyone have ideas about how you could make a weak map whose entries
are present until the object is actually collected?
More information about the Gc