I think I just thunk up a way to optimize integer math to primitives
safely in JRuby. Perhaps this will work for us and for other languages
attempting to do the same thing.

So the fundamental problem here is that we only get one return type,
so we have to choose either a primitive (int/long) or a reference type
(for the overflow case). That was the showstopper for me, and talking
to others doing this sort of optimization it seemed like there was no
good solution.

But then I had a "duh" moment tonight once I returned to SF: integer
math is side-effect free!

So in the following dynopt'ed code from JRuby, you can see we have a
serial number check before the direct dispatch to RubyFixnum.op_minus:

    LDC 499
    INVOKESTATIC
org/jruby/javasupport/util/RuntimeHelpers.isGenerationEqual
(Lorg/jruby/runtime/builtin/IRubyObject;I)Z
    IFNE L26
    ALOAD 11
    CHECKCAST org/jruby/RubyFixnum
    ALOAD 1
    LDC 1
    INVOKEVIRTUAL org/jruby/RubyFixnum.op_minus
(Lorg/jruby/runtime/ThreadContext;J)Lorg/jruby/runtime/builtin/IRubyObject;

So if we pass the check, we know we're invoking the builtin Fixnum#+
method that has no side effects. Since it has no side effects, we
could insert the hacker's delight overflow guard into the compiled
bytecode as a second check, and then branch to an Object version of
the same algorithm! Ideally that branch will never be followed, and
we're good to go.

Managing the stack is still tricky, but at least the return value
issue can be solved.

- Charlie

-- 
You received this message because you are subscribed to the Google Groups "JVM 
Languages" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/jvm-languages?hl=en.

Reply via email to