https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112012-04-20T04:51:25ZRuby Issue Tracking SystemRuby master - Feature #6309: Add a reference queue for weak referenceshttps://redmine.ruby-lang.org/issues/6309?journal_id=260252012-04-20T04:51:25Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li></ul><p>There is no maintainer for weakref, so please create a patch yourself!<br>
We may import it if matz accepts.</p>
<p>BTW, the name "reference queue" is very bad, I think.</p>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp" class="email">mame@tsg.ne.jp</a></p> Ruby master - Feature #6309: Add a reference queue for weak referenceshttps://redmine.ruby-lang.org/issues/6309?journal_id=262802012-04-28T14:47:38Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>Ok, fair enough.</p>
<p>Here is a <em>very primitive</em> 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.</p>
<p>Patch: <a href="https://gist.github.com/2516338" class="external">https://gist.github.com/2516338</a></p>
<p>Example usage: <a href="https://gist.github.com/2516355" class="external">https://gist.github.com/2516355</a></p>
<p>This works mostly like I expect a reference queue to work, but there are many inefficiencies here:</p>
<ul>
<li>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.</li>
<li>A Ruby-level finalizer is much more expensive than a purpose-built native GC hook would be.</li>
<li>A Ruby-based Queue is much more expensive than a purpose-built reference queue would be.</li>
</ul>
<p>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.</p> Ruby master - Feature #6309: Add a reference queue for weak referenceshttps://redmine.ruby-lang.org/issues/6309?journal_id=264012012-05-03T11:29:17Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>Ah, I knew what you are proposing by seeing Javadoc:</p>
<p><a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/ref/ReferenceQueue.html" class="external">http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/ref/ReferenceQueue.html</a><br>
<a href="http://docs.oracle.com/javase/6/docs/api/java/lang/ref/WeakReference.html#WeakReference(T" class="external">http://docs.oracle.com/javase/6/docs/api/java/lang/ref/WeakReference.html#WeakReference(T</a>, java.lang.ref.ReferenceQueue)</p>
<p>I don't know the (real-world) use case of the feature, though.</p>
<p>Anyway, I mean I'd like you to create a patch written <em>in C</em>.<br>
If there is a patch that we can review and import "as is",<br>
I will be happy to assign this ticket to some core committers,<br>
such as ko1 and kosaki.</p>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp" class="email">mame@tsg.ne.jp</a></p> Ruby master - Feature #6309: Add a reference queue for weak referenceshttps://redmine.ruby-lang.org/issues/6309?journal_id=264022012-05-03T11:50:58Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Feedback</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li></ul><p>On second thought, the proposal should first get an approval from matz. Sorry. Assigning this to him.<br>
Still, it would be helpful to show a concrete use case, I think.</p>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp" class="email">mame@tsg.ne.jp</a></p> Ruby master - Feature #6309: Add a reference queue for weak referenceshttps://redmine.ruby-lang.org/issues/6309?journal_id=264322012-05-04T05:59:19Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>I linked to a concrete use case in the original report...an implementation of a "weak ID map" entirely in Ruby without scanning for dead references: <a href="https://github.com/headius/weakling/blob/master/lib/weakling/collections.rb" class="external">https://github.com/headius/weakling/blob/master/lib/weakling/collections.rb</a></p>
<p>It is not possible to implement weak data structures efficiently without a reference queue, since you would be forced to periodically do an O(N) scan for dead references to clean them out.</p> Ruby master - Feature #6309: Add a reference queue for weak referenceshttps://redmine.ruby-lang.org/issues/6309?journal_id=329732012-11-17T01:26:36Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>Seven months and no activity. Can we get a reference queue in Ruby 2.0 please? I believe it could be added to weakref.rb using 2.0's WeakHash, or built atop the C code that implements WeakHash (since it contains most of a reference queue implementation already).</p> Ruby master - Feature #6309: Add a reference queue for weak referenceshttps://redmine.ruby-lang.org/issues/6309?journal_id=337232012-11-24T10:50:04Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Target version</strong> set to <i>2.6</i></li></ul> Ruby master - Feature #6309: Add a reference queue for weak referenceshttps://redmine.ruby-lang.org/issues/6309?journal_id=376352013-03-16T02:27:16Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>I request a ruling by matz about adding a ReferenceQueue to weakref.rb.</p> Ruby master - Feature #6309: Add a reference queue for weak referenceshttps://redmine.ruby-lang.org/issues/6309?journal_id=420442013-09-27T20:19:21Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>I again request approval from matz to add this feature :-) Can we do it for 2.1, please?</p> Ruby master - Feature #6309: Add a reference queue for weak referenceshttps://redmine.ruby-lang.org/issues/6309?journal_id=421042013-09-30T22:21:54Zheadius (Charles Nutter)headius@headius.com
<ul><li><strong>Target version</strong> changed from <i>2.6</i> to <i>2.1.0</i></li></ul> Ruby master - Feature #6309: Add a reference queue for weak referenceshttps://redmine.ruby-lang.org/issues/6309?journal_id=421292013-10-01T10:23:52Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>Put my patch plus a test in a PR: <a href="https://github.com/ruby/ruby/pull/408" class="external">https://github.com/ruby/ruby/pull/408</a></p>
<p>Unfortunately the test doesn't pass, and I think it should. I'm confused as to why it fails.</p> Ruby master - Feature #6309: Add a reference queue for weak referenceshttps://redmine.ruby-lang.org/issues/6309?journal_id=436632013-12-14T13:33:16Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>My PR: <a href="https://github.com/ruby/ruby/pull/480" class="external">https://github.com/ruby/ruby/pull/480</a></p> Ruby master - Feature #6309: Add a reference queue for weak referenceshttps://redmine.ruby-lang.org/issues/6309?journal_id=439442013-12-30T01:07:36Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>Sorry this didn't get into 2.1 and I was unable to review. December was probably too late to get it in anyway.</p>
<p>Nobu's patch looks fine. If other ruby-core folks really want to keep Weakref as-is for compatibility and introduce a new type, I guess that's the way we'll have to go.</p>
<p>A couple comments.</p>
<ul>
<li>
<p>ext/weakref should be deprecated and warn when loaded once ext/weak is in place.</p>
</li>
<li>
<p>Is there interest in other possible reference types? If so, having a namespace for these new references would be less cumbersome. I will describe the reference types on JVM below.</p>
</li>
</ul>
<p>On JVM, there is WeakReference, of course. There's also two others that are useful:</p>
<ul>
<li>
<p>SoftReference is a reference cleared less frequently than a weak reference. This is JVM implememtation-specific, but on OpenJDK it is a combination of heap pressure (if the heap has to be expanded, soft references are cleared) or if the soft reference is not traversed for some period of time (configurable as some number of ms/MB of heap).</p>
</li>
<li>
<p>PhantomReference is similar to weak reference in life cycle, but is not traversible and only useful when combined with a queue. This is lighter-weight than weak reference, since it does not have to be cleared when the object is collected; it just needs to be enqueued. It also does not impact GC cycles as much, since it does not count as a strong or weak traversible reference.</p>
</li>
</ul>
<p>If we might want these types of references in the future, it may be good to have a Reference namespace, a la Reference::Weak, Reference::Soft, etc.</p> Ruby master - Feature #6309: Add a reference queue for weak referenceshttps://redmine.ruby-lang.org/issues/6309?journal_id=439452013-12-30T01:07:52Zheadius (Charles Nutter)headius@headius.com
<ul><li><strong>Target version</strong> changed from <i>2.1.0</i> to <i>2.2.0</i></li></ul> Ruby master - Feature #6309: Add a reference queue for weak referenceshttps://redmine.ruby-lang.org/issues/6309?journal_id=692972018-01-05T21:00:39Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Target version</strong> deleted (<del><i>2.2.0</i></del>)</li></ul> Ruby master - Feature #6309: Add a reference queue for weak referenceshttps://redmine.ruby-lang.org/issues/6309?journal_id=871612020-08-23T21:07:32Zioquatix (Samuel Williams)samuel@oriontransfer.net
<ul></ul><p>This still seems like a good idea. Any update?</p>