From: Kurt Stephens Date: 2011-01-25T14:44:21+09:00 Subject: [ruby-core:34846] [Ruby 1.9-Bug#4168] WeakRef is unsafe to use in Ruby 1.9 Issue #4168 has been updated by Kurt Stephens. Progress: https://github.com/kstephens/ruby/commit/6f3f2a538455187d67ef504cfa266cf5789ca099 * Support for multiple Reference subclass instance caches. * Working WeakReference, SoftReference and ReferenceQueue in ext/reference/reference.c * SoftReference responds to new GC heap allocations as "memory pressure" by globally reducing all SoftReference#_mri_gc_left counts. * Sample WeakKeyHash class using WeakReference and ReferenceQueue. * Support for JRuby-compatible WeakReference.new(object, reference_queue) protocol. * Removed ReferenceQueue#monitor method which cannot be supported in JRuby. * Renamed MRI-specific methods to _mri_*. * Optimized gc_api.c callback lists. * Clean up the C stack after gc_api.c callbacks to avoid conservative GC pinning (might be overkill). * Removed the ugly (and stupid) flags hacks. * Added some basic RDOC-style comments. Charles: I'm caching live Reference instances to minimize the number of References objects that must be scanned before the sweep phase. I'd either need to keep a doubly-linked list of References or scan all the GC heap for References (which I did not want to expose from gc.c). Using a lookup table (st_table in MRI) to track of live References was not much more work than a doubly-linked list and it collapses Reference.new(object) requests into a single instance. This is beneficial for other reasons: it reduces the total number of References to common objects and keeps the Reference#== operator simple for live instances (it's simply Reference#object_id#==). Once Reference#object is sweeped, all the References to #object are no longer "live"; and are removed from cache and scheduled for ReferenceQueues after GC. References to immediates are never cached. Dead References will probably will be removed from collections or instance variables shortly thereafter (i.e.: via ReferenceQueues). Why would a user want to insure that Reference.new(object) always returns a new instance, since References are/should be immutable? JRuby and other Rubies do not need to implement the Reference cache. Aside: Q: Should we have a trivial HardReference class that always keeps its #object pinned, just for API consistency? ---------------------------------------- http://redmine.ruby-lang.org/issues/show/4168 ---------------------------------------- http://redmine.ruby-lang.org