From: "headius (Charles Nutter)" Date: 2012-04-28T14:47:38+09:00 Subject: [ruby-core:44719] [ruby-trunk - Feature #6309] Add a reference queue for weak references Issue #6309 has been updated by headius (Charles Nutter). Ok, fair enough. Here is a *very primitive* modification of the current weakref.rb to support a reference queue. I need to stress that I don't think this is the best way to implement it; a hook into the GC cycle that inserts weakrefs into a purpose-built reference queue would be better than using finalizers in this way. But the API would largely work the same. Patch: https://gist.github.com/2516338 Example usage: https://gist.github.com/2516355 This works mostly like I expect a reference queue to work, but there are many inefficiencies here: * Polling the reference queue needs to be as close to free as possible. The current Queue implementation raises an exception when empty, which is very far from being free. * A Ruby-level finalizer is much more expensive than a purpose-built native GC hook would be. * A Ruby-based Queue is much more expensive than a purpose-built reference queue would be. I know that in past discussions about improving weakref support in Ruby there were C-level patches to add all the features I'm looking for, and I'll try to dig up those discussions and patches. But hopefully this illustrates what I'm looking for in a primitive way. ---------------------------------------- Feature #6309: Add a reference queue for weak references https://bugs.ruby-lang.org/issues/6309#change-26280 Author: headius (Charles Nutter) Status: Feedback Priority: Normal Assignee: Category: Target version: Most interesting uses of WeakRef are much harder to do efficiently without a reference queue. A reference queue, as implemented by the JVM, is basically a queue into which weak references are placed some time after the object they refer to has been collected. The queue can be polled cheaply to look for collected references. A simple example of usage can be seen in the weakling gem, with an efficient implementation of an ID hash: https://github.com/headius/weakling/blob/master/lib/weakling/collections.rb Notice the _cleanup method is called for every operation, to keep the hash clear of dead references. Failure to have a _cleanup method would mean the hash grows without bounds. _cleanup cannot be implemented efficiently on MRI at present because there's no reference queue implementation. On MRI, _cleanup would have to perform a linear scan of all stored values periodically to search for dead references. For a heavily used hash with many live values, this becomes a very expensive operation. It's probably possible to implement reference queues efficiently atop the new ObjectSpace::WeakMap internals, since it already keeps track of weak references and can run code when a weak reference no longer refers to a live object. -- http://bugs.ruby-lang.org/