-
Notifications
You must be signed in to change notification settings - Fork 25
Avoid use of id2ref for weak mapping #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
In what version was that fixed? Line 16 in 10fceea
And 2.7 seems to support ObjectSpace::WeakMap fixnum keys: irb(main):005:0> RUBY_DESCRIPTION
=> "ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5) [x86_64-linux]"
irb(main):006:0> m=ObjectSpace::WeakMap.new
=> #<ObjectSpace::WeakMap:0x00000000028174f8>
irb(main):007:0> m[2] = 3
=> 3
irb(main):008:0> m
=> #<ObjectSpace::WeakMap:0x00000000028174f8: 2 => 3> |
A version with WeakMap also passes tests: def initialize
@id2ref = ObjectSpace::WeakMap.new
end
# Convert an object reference id to an object.
#
# This implementation looks up the reference id in the local object
# space and returns the object it refers to.
def to_obj(ref)
@id2ref[ref]
end
# Convert an object into a reference id.
#
# This implementation returns the object's __id__ in the local
# object space.
def to_id(obj)
return if obj == nil
id = obj.__id__
@id2ref[id] = obj
id
end
If this gem is only supported on 2.7 or higher, then the WeakMap version would be fine. |
It appears that WeakMap was updated to allow Integer keys in https://bugs.ruby-lang.org/issues/16035 (for 2.7). The limitation mentioned there on JRuby (Fixnum idempotency) was resolved by having a separate value-based weak map for such objects. |
Big 👍 to remove the usage of From https://bugs.ruby-lang.org/issues/15711#note-12 I found there is already a WeakMap implementation in Could we change DRb so it uses that one by default and remove the |
Ah I did not notice that had gotten merged into the gem. It was merged as part of my original issue years ago, but never used. I will adjust the patch to unconditionally use the WeakIdConv implementation. |
Digging deeper it appears that I'm making modifications to deprecate the old version, use |
This is done. It could be cleaner. We could also just remove the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I filed a PR to deprecate |
This LGTM and was mentioned and approved years ago in https://bugs.ruby-lang.org/issues/15408, so 👍 @hsbt you seem to be the maintainer of |
I'm talking about this with @seki, the author of DRb. |
It's based on https://gist.github.com/seki/773ab9ffe403a5197e82e1a90cb2c583 that is implemented by @seki. |
Revisiting my report in https://bugs.ruby-lang.org/issues/15711 and trying to finally get rid of the use of
ObjectSpace._id2ref
in drb. That report was closed but never actually fixed in DRb.The implementation of
ObjectSpace._id2ref
on JRuby (and TruffleRuby) is very expensive, and on JRuby we normally do not have it enabled because it impacts performance of the entire runtime.This alternate implementation uses the
weakref
library to build a weak ID map. It could possibly use the new ObjectSpace::WeakMap, but until recently that did not support Fixnum keys and I did not want to break DRb for older Ruby versions.Unfortunately, due to the lack of "reference queue" support in Ruby's Weakref, this implementation must do a full scan for dead references. This may be possible to improve, and would probably be resolved by using WeakMap.
We recently got another report pry-remote not working on JRuby. pry-remote uses DRb, and DRb still uses
ObjectSpace._id2ref
to simulate a weak map of objects. We would like to get this fixed and released so users can use pry-remote without enabling full ObjectSpace support.Notes:
Edit: Modified to use existing thread-safe
WeakMap
-based implementationWeakIdConv
by default. Without it being default, users of DRb will always end up using the_id2ref
version. We should simply eliminate that possibility.