John Lam 1997 — 2026

RubyCLR identity woes

The last bug that I wanted to squash before the next release is proving to be a bit tricky.

Up until today, RubyCLR didn’t really respect identity of CLR objects all that well. If you marshaled the same CLR object across the interop boundary twice you’d get two Ruby proxy objects.

This is a potential problem if you start stuffing state into those Ruby proxy objects and expect identity to be maintained – it won’t. So I fixed that problem today by caching Ruby proxy objects inside of a global hashtable. ~~The key to each object is the value that’s returned from Object.GetHashCode.~~

There’s another problem, however. My interface implementation requires a separate Ruby object. So if you did something like:

enum = object.as(IEnumerable)

enum and object would now be two separate objects. This is problematic because Ruby doesn’t support interfaces natively, so I can’t really think of ways around this problem.

Suggestions would now be welcome :)

Update: It’s a really bad idea to use Object.GetHashCode as an object’s identity. So please don’t do this. I’m working on some other possible ways of solving this problem, but the more I look at the identity problem the harder it becomes. I may not have a fix for this in time for the next drop (pre Tech Ed) as I suspect that this is probably a much harder problem than it appears.

4 archived reader comments

Mark (2006-06-02)

GetHashCode doesn’t seem like a very reliable mechanism for identity, especially if the hashtable is global (across all types). I suppose if you’re calling Object’s GetHashCode rather than doing a virtual call, it wouldn’t be as bad.

Just some “from the hip” brainstorming… Perhaps your interface proxies need to hang off of the original object proxies (or vice versa)somehow so that any state changes made to the interface proxy are actually stored in the original proxy. Then any marshalling back and forth results in the original object, and the interface proxy can be grabbed again via the “as” operator. Or perhaps some variant on that idea.

I might be persuaded to think of the interface transformation as a temporary “view” of an object that would be “undone” in a round-trip back through the CLR. Just some thoughts.

Wilco Bauwer (2006-06-03)

Right now what I do in IronRuby to support interfaces is create proxy types on the fly (per interface type). Proxy types derive from a shared base class in which I partially deal with identity (or rather equality). It overloads the relevant operators and implements some relevant interfaces by basically forwarding calls to the underlying Ruby object it represents.

There are some obvious caveats however. Proxy types do not represent the true identity of the underlying Ruby object - it only tries to give you the illusion. When you would actually compare references though (which is what will happen in several scenarios), you will not always get the expected result. Imagine a Ruby object which implements IFoo and IBar and lets say we’ve got a .NET type with the following methods:

void X(IFoo f); void Y(IBar b);

When you would pass the Ruby object to X(IFoo f), a proxy type is generated (if it doesn’t exist yet) and it is instantiated (if no instance was created yet). In subsequent calls the same proxy type instance will be passed, in which case identity is preserved.

However, when you would pass the same Ruby object to Y(IBar b) an instance of a different proxy type is passed. Identity crisis is what follows when object references are compared instead of checking equality.

Nick Guerrera (2006-06-03)

It’s probably a good idea to use System.CompilerServices.RuntimeHelpers.GetHashCode. A non-virtual call to Object.GetHashCode is equivalent, but unverifiable.

As for the interface issue, what about getting rid of the .as operation entirely? It seems out of place to me in a world of duck typing.

Can you make the original proxy object surface all interface methods as well? I’m sure this already works for implicit interface implementations. For explicit interface implementations, you could elevate them to normal methods in the absence of ambiguity.

Dealing with ambiguity in the face of explicit interface implementation is the tough part. One dodge would be to use name mangling for disambiguation, but that would break duck typing.

I think a better approach would be to provide a method on the proxy object which allows the user to do the disambiguation. For example, given an object which implements both IEnumerable.GetEnumerator and IFoo.GetEnumerator, you could do the following:

object.select_interface_method("GetEnumerator", IEnumerable)
x = object.GetEnumerator()
    — calls IEnumerable.GetEnumerator()

object.select_interface_method("GetEnumerator", IFoo)
x = object.GetEnumerator()
    — calls IFoo.GetEnumerator()

Just a thought.

Nick Guerrera (2006-06-03)

Also, the value returned from GetHashCode (whether overridden or not) is not guaranteed to be unique to the object instance and therefore cannot be used as the key into a hashtable.

See http://blogs.msdn.com/brada/archive/2003/09/30/50396.aspx

You’ll have to effectively store a list of proxy objects for each hash code and then compare the target .NET object of each element in the list to the incoming .NET object for reference equality.

There’s also the hard problem of making sure that the global cache does not prevent prevent objects (both Ruby and .NET) from being GC’ed when they are otherwise unreachable.