Martin C. Martin wrote:
Efficient dynamic method dispatch on the JVM is hard. Since classes can
be loaded at any time, even a call site has been executed, you need to
use reflection. But explicitly considering the signatures of all
relevant methods every time is slow. And reflection is slow, too slow
to be used for every single method call.
So there are various tricks: call site caching lets you skip the method
determination logic most of the time. And once you've determined a
method, you could generate a little bytecode at runtime to do a direct
method call, avoiding reflection.
But that bytecode may have to change if new classes are loaded or new
types are used to dispatch, so we'd like it to be garbage collected.
Since the class loader maintains a hard reference to the class, we need
to wrap each one in its own classloader. All of this lives in the
PermGen, so in the end we can't have many of them.
So we end up with a somewhat complicated strategy. JRuby starts by
interpreting the AST; after a while it will compile it to JVM bytecode,
which uses reflection; Ruby core methods have wrappers that call them
directly (i.e. without reflection) and that don't need 1 classloader per
method; but not for Java's standard library, since that would be too
many classes.
So this is a large engineering project that all dynamic languages on the
JVM have to wrestle with. Is there scope here to share work? A common
architecture, perhaps a lot like invokedynamic, e.g. a call site cache
with a guard? LRU logic for evicting old JITted call sites?
I've posted about such projects to Groovy list in the past, but usually
there didn't seem to be much interest. In JRuby there's some code
already that can generate lighter-weight method handles for Java classes
too, making them a lot faster than reflection. We also generate handles
for all the JRuby core classes, drastically improving the performance of
numbers, strings, arrays, and so on.
I know we could share a lot of this code and both benefit from it, and
I'd love to collaborate on that. We're going to be making another push
over the summer to improve JRuby's java integration, and along with that
will come a second look at generated method handles and improved call
pipeline performance to Java classes, as well as modifications within
JRuby's dispatch logic to allow pre-Java 7 JVMs to inline calls (which
actually works already on JRuby master, by setting
-J-Djruby.compile.inlinedyncalls=true).
- Charlie
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email