using MethodHandle in place of virtual methods?

Per Bothner per at bothner.com
Wed May 16 10:55:06 PDT 2012


I'm pondering how Kawa can make use of MethodHandles without (yet) going
the full invokedynamic route.  Specifically, I'm considering using
MethodHandles in place of virtual methods, and I'm hoping someone has
some information on the likely costs and benefits.

The attachment ProcUsingVirt.java sketches out the current implementation.
This uses virtual apply[01N] methods that are overridden by specific
Procedure subclasses.  (Kawa actually has apply[01234N], but I've 
simplified.)

(See also http://www.gnu.org/software/kawa/internals/procedures.html)

The attachment ProcUsingMH.java is a sketch of an alternative implementation
where each apply[01N] method has a matching MethodHandle field.  The 
apply[01N]
method is now final and just invokes the corresponding MethodHandle.

The first issue is that ProcUsingMH requires more fields, and thus more 
memory.
This is mitigated because we don't need space for the virtual method tables.
Because Procedure classes are often singletons, this may be a wash.

Calling an MethodHandle using invokeExact requires more indirection and
more checking than just doing an invokevirtual.  Would Hotspot be able to
optimize this to roughly the same speed?  Presumably it might be a little
bit slower on less-optimizing VMs.

To avoid one-class-per-function, Kawa will compile a switch-table per 
module,
as sketched by the ProcUsingVirt.Builtins2 class.  (see the section named
"Closures" in the internals document linked to above.)  This is where using
MethodHandles seems most winning, since we can do as shown in the
ProcUingMH.Builins class.  Is this likely to have a performance impact?

What if the apply[01N] method is non-final?  That might make 
interoperability
easier.  For example, it's desirable to support a '--target 6' flag, though
supporting both styles in the same runtime may reduce the advantages of 
either.

Finally, it seems that if Kawa in the future makes use of invokedynamic,
having the MethodHandles in the Procedure would be an advantage.

Comments?  Advice?
-- 
	--Per Bothner
per at bothner.com   http://per.bothner.com/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ProcUsingVirt.java
Type: text/x-java
Size: 1717 bytes
Desc: not available
Url : http://mail.openjdk.java.net/pipermail/mlvm-dev/attachments/20120516/687b21e5/ProcUsingVirt.java 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ProcUsingMH.java
Type: text/x-java
Size: 3343 bytes
Desc: not available
Url : http://mail.openjdk.java.net/pipermail/mlvm-dev/attachments/20120516/687b21e5/ProcUsingMH.java 


More information about the mlvm-dev mailing list