using MethodHandle in place of virtual methods?

Per Bothner per at bothner.com
Fri May 18 23:04:01 PDT 2012


On 05/16/2012 05:52 PM, Charles Oliver Nutter wrote:

Thanks for a detailed and helpful response!

> On Wed, May 16, 2012 at 12:55 PM, Per Bothner<per at bothner.com>  wrote:
>> 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.
>
> I don't think this would optimize like you're hoping.

I'm not expecting a big performance gain.
I'm hoping for a slight gain getting rid of using a dispatch-switch,
partly because it avoids an extra indirection (and virtual dispatch), and
partly because (as you mentioned) HotSpot may have trouble
optimizing switches, at least large ones.

To summarize my questions, before I actually try to implement this:
(1) A change to replace a virtual applyX method by a final method
that calls a InvokeMethod in a final field: My guess is this would
performancewise be more-or-less a wash, with neither major gain
or less.  Is that likely?
(2) My guess is using the new MethodHandle scheme might be slighly
more efficient than using a switch on a "procedure-index".  It be
be significantly more efficient in cases of big switch.  Is that likely?

I think there may be additional benefits to getting rid of the
dispatch-switch: More helpful exception stack traces;
plus avoiding the need to generate the dispatch classes.

>> Finally, it seems that if Kawa in the future makes use of invokedynamic,
>> having the MethodHandles in the Procedure would be an advantage.
>
> We do keep a method handle in each method object, but only so we can
> more easily retrieve it and bind it to the invokedynamic call site. I
> don't think your'e going to see the performance gain you're hoping for
> with the Procedure object that aggregates handles. You really need the
> invokedynamic call site for the whole thing to optimize together well.

It probably makes sense to not implement a switch to using MethodHandles
until I have a design (roadmap) for using invokedynamic, since the
benefit of the former change is at best modest and probably not worth the
effort unless Kawa also makes use of invokedynamic.  Does that match
your advice?
-- 
	--Per Bothner
per at bothner.com   http://per.bothner.com/


More information about the mlvm-dev mailing list