# Using id2ref for anything?

**URL:** <https://www.ruby-forum.com/t/using-id2ref-for-anything/177865>\
**Category:** Ruby\
**Created:** [October 25, 2009, 9:42pm UTC](https://www.ruby-forum.com/t/using-id2ref-for-anything/177865 "2009-10-25T21:42:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![headius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7c8e57/32.png) [@headius](https://www.ruby-forum.com/u/headius)\
**Post date:** [October 25, 2009, 9:42pm UTC](https://www.ruby-forum.com/t/using-id2ref-for-anything/177865/1 "2009-10-25T21:42:06Z")

</div>

ObjectSpace.\_id2ref is another of those peculiar methods, an artifact  
of a particular implementation which, due to its lack of a  
copying/compacting garbage collector, can always locate in memory an  
object given its “id”. This is typically _not_ easily possible on  
other VMs, where objects move around and it may even be difficult to  
get a unique “id” for a given object since memory locations keep  
moving and adding a numeric ID would increase object or object handle  
sizes.

On JRuby, \_id2ref is implemented as a pair with Object#object\_id/id.  
The latter, when called on an object, atomically constructs a numeric  
ID for the object in question. It then asks our ObjectSpace  
implementation to insert a weak reference to the object into a table  
keyed on numeric ID. This allows the resulting ID to be used later for  
\_id2ref to retrieve the object.

Unfortunately object\_id, in its #id form, is often used to get a  
unique non-#hash key for an object for purposes entirely unrelated to  
\_id2ref. As a result, any code using object\_id or id on JRuby pays a  
significantly higher cost than you might expect.

If we no longer supported \_id2ref, the only cost would be in producing  
an ID, probably with a strictly-increasing atomic 64-bit value. There  
would be no weakref map and no cost of constructing and managing the  
weakrefs within that map.

So I am asking you Rubyists…does this sound like a problem? In the  
1.8/1.9 stdlib, the only reference to \_id2ref is one in drb.rb, which  
could be replaced with a “better way”. None of the gems I have  
installed use \_id2ref. Originally, weakref.rb used \_id2ref, but we  
have a native impl of weakref that uses Java’s built-in weakrefs.  
Google code search only brings up about 353 hits for “lang:ruby  
\_id2ref”, most of them the already-mentioned cases.

One last demonstration of the perf difference between the current  
Object#object\_id and one that does not use the ObjectSpace weak map:

Current:

```
                                          user system

```

total real  
1M calls to obj.object\_id 0.658000 0.000000  
0.658000 ( 0.658000)  
1M calls to Object.new.object\_id 6.636000 0.000000  
6.636000 ( 6.636000)

Using object’s “identity hash”:

```
                                          user system

```

total real  
1M calls to obj.object\_id 0.356000 0.000000  
0.356000 ( 0.356000)  
1M calls to Object.new.object\_id 0.636000 0.000000  
0.636000 ( 0.636000)

It’s also interesting to note that even maintaining the contract of  
object\_id being unique is hard. On the JVM, for example, it is not  
possible to get a unique numeric id or pointer for a given object  
unless you manage a weak map of objects on your own…

- Charlie

---

<div class="post-metadata">

**Author:** ![headius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7c8e57/32.png) [@headius](https://www.ruby-forum.com/u/headius)\
**Post date:** [October 25, 2009, 10:05pm UTC](https://www.ruby-forum.com/t/using-id2ref-for-anything/177865/2 "2009-10-25T22:05:31Z")

</div>

On 10/25/2009 09:41 PM, Charles Oliver N. wrote:

> So I am asking you Rubyists…does this sound like a problem? In the  
> 1.8/1.9 stdlib, the only reference to \_id2ref is one in drb.rb, which  
> could be replaced with a “better way”. None of the gems I have  
> installed use \_id2ref. Originally, weakref.rb used \_id2ref, but we  
> have a native impl of weakref that uses Java’s built-in weakrefs.  
> Google code search only brings up about 353 hits for “lang:ruby  
> \_id2ref”, most of them the already-mentioned cases.

Charles, thanks for the elaborate report and request! I for my part do  
not see an issue with removing \_id2ref if a better solution for DRb can  
be devised.

> It’s also interesting to note that even maintaining the contract of  
> object\_id being unique is hard. On the JVM, for example, it is not  
> possible to get a unique numeric id or pointer for a given object  
> unless you manage a weak map of objects on your own…

I believe there is an alternative solution which comes at the cost of  
the memory overhead for every object: place the id in the instance and  
use a central AtomicLong for “generating” ids. You also save the  
overhead of map maintenance which would be a central synchronization  
point.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![headius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7c8e57/32.png) [@headius](https://www.ruby-forum.com/u/headius)\
**Post date:** [October 25, 2009, 11:02pm UTC](https://www.ruby-forum.com/t/using-id2ref-for-anything/177865/3 "2009-10-25T23:02:53Z")

</div>

Hi,

In message “Re: Using id2ref for anything?”  
on Mon, 26 Oct 2009 05:41:52 +0900, Charles Oliver N.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|ObjectSpace.\_id2ref is another of those peculiar methods, an artifact  
|of a particular implementation which, due to its lack of a  
|copying/compacting garbage collector, can always locate in memory an  
|object given its “id”. This is typically _not_ easily possible on  
|other VMs, where objects move around and it may even be difficult to  
|get a unique “id” for a given object since memory locations keep  
|moving and adding a numeric ID would increase object or object handle  
|sizes.

Originally, \_id2ref is an implementation dependent hack for weakref,  
so that you can remove it, if you can provide the better way.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![headius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7c8e57/32.png) [@headius](https://www.ruby-forum.com/u/headius)\
**Post date:** [October 26, 2009, 1:18am UTC](https://www.ruby-forum.com/t/using-id2ref-for-anything/177865/4 "2009-10-26T01:18:39Z")

</div>

On Sun, Oct 25, 2009 at 5:02 PM, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Originally, \_id2ref is an implementation dependent hack for weakref,  
> so that you can remove it, if you can provide the better way.

I suspected as much, since that seemed to be the primary place for it  
to be used. I guess the remaining question is about the uniqueness of  
object\_id. I believe on Sun’s JVMs java.lang.System.identityHashcode  
of an object will be unique for the lifetime of the object, but not  
unique forever (which I’m sure is the case on MRI). However, I don’t  
think there’s any guarantee that the identityHashCode will be unique  
across JVMs, though the documentation says an implementation should  
make a “best effort” to keep it unique.

We will look at removing \_id2ref in 1.5 (or making it do nothing, with  
a warning) as well as modifying the one stdlib that uses it (DRb, for  
reasons I have not yet explored). And I will explore whether  
identityHashcode will be “unique enough” as I suspect it should be.

- Charlie

---

<div class="post-metadata">

**Author:** ![headius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7c8e57/32.png) [@headius](https://www.ruby-forum.com/u/headius)\
**Post date:** [October 26, 2009, 12:34am UTC](https://www.ruby-forum.com/t/using-id2ref-for-anything/177865/5 "2009-10-26T00:34:45Z")

</div>

On 10/25/09, Charles Oliver N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If we no longer supported \_id2ref, the only cost would be in producing  
> an ID, probably with a strictly-increasing atomic 64-bit value. There  
> would be no weakref map and no cost of constructing and managing the  
> weakrefs within that map.
> 
> So I am asking you Rubyists…does this sound like a problem?

In my own projects, I use \_id2ref/ **id** in a couple places that I can  
recall. So, I was about to object to it going away… but on  
reflection, it seems like \_id2ref can always be replaced by a WeakRef,  
(at least when running in JRuby). So, removing it shouldn’t really be  
a problem.

---

<div class="post-metadata">

**Author:** ![headius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7c8e57/32.png) [@headius](https://www.ruby-forum.com/u/headius)\
**Post date:** [October 26, 2009, 1:22am UTC](https://www.ruby-forum.com/t/using-id2ref-for-anything/177865/6 "2009-10-26T01:22:10Z")

</div>

On Sun, Oct 25, 2009 at 4:05 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > It’s also interesting to note that even maintaining the contract of  
> > object\_id being unique is hard. On the JVM, for example, it is not  
> > possible to get a unique numeric id or pointer for a given object  
> > unless you manage a weak map of objects on your own…
> 
> I believe there is an alternative solution which comes at the cost of the  
> memory overhead for every object: place the id in the instance and use a  
> central AtomicLong for “generating” ids. Â You also save the overhead of map  
> maintenance which would be a central synchronization point.

Yes, that may be too high a cost for us to pay. On 32/64-bit JVMs,  
adding another field would cost 4 or 8 bytes per object. Stuffing it  
into the instance variable table would force ivar tables to be created  
when object\_id is called, which comes with a base cost of 2 words (4  
or 8-byte) plus the word for the reference to a Fixnum object (or 4-8  
bytes for a reference to an int or long). It’s too high to put on all  
Objects, especially if identityHashcode is unique enough.

- Charlie

---

<div class="post-metadata">

**Author:** ![headius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7c8e57/32.png) [@headius](https://www.ruby-forum.com/u/headius)\
**Post date:** [October 26, 2009, 1:19am UTC](https://www.ruby-forum.com/t/using-id2ref-for-anything/177865/7 "2009-10-26T01:19:50Z")

</div>

On Sun, Oct 25, 2009 at 6:34 PM, Caleb C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> In my own projects, I use \_id2ref/ **id** in a couple places that I can  
> recall. So, I was about to object to it going away… but on  
> reflection, it seems like \_id2ref can always be replaced by a WeakRef,  
> (at least when running in JRuby). So, removing it shouldn’t really be  
> a problem.

Yes, all cases of \_id2ref could be implemented yourself by building a  
weak map from your own user-generated (or from object\_id) to objects.  
So I think there’s probably no good reason we need to have \_id2ref  
support if we have our own weakref implementation.

- Charlie
