Inlining/Overhead implications of MethodHandle invoke() vs invokeExact()
Vladimir Ivanov
vladimir.x.ivanov at oracle.com
Tue Jun 14 11:41:55 UTC 2016
Ian,
invokeExact() [1] requires exact match between call site and method
handle types (reference equality check on method types), while invoke()
[2] tries to adapt the method handle for call site type when exact type
check fails (equivalent to MH.asType(callSiteType).invokeExact(...)).
When inlining happens, call site & method handle types are compile time
constants, so exact type check collapses.
For invoke() there's 1-element cache [3] for MH.asType() conversion,
which helps in some scenarios. In the worst case, inexact/generic
invocation goes through expensive transformation.
Can you elaborate what use case do you have in mind?
For snippets, you can use Object in snippet type, but do an explicit
upcast in the wrapper, e.g.:
static final MethodHandle copyL2 =
MachineCodeSnippet.make("...",
MethodType.methodType(void.class,
Object.class /*src*/, long.class /*offset*/,
Object.class /*dst*/, long.class /*offset*/), ...);
static void copyL2(int[] src, int srcIdx, int[] dst, int dstIdx) {
<< ... range checks & offset calculations ... >>
try {
mh.invokeExact((Object)src, off1, (Object)dst, off2);
} catch (Throwable e) {
throw new Error(e);
}
}
static void copyL2(long[] src, int srcIdx, long[] dst, int dstIdx) {
<< ... range checks & offset calculations ... >>
try {
mh.invokeExact((Object)src, off1, (Object)dst, off2);
} catch (Throwable e) {
throw new Error(e);
}
}
Best regards,
Vladimir Ivanov
[1]
http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/75d680f9f3bb/src/java.base/share/classes/java/lang/invoke/Invokers.java#l469
[2]
http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/75d680f9f3bb/src/java.base/share/classes/java/lang/invoke/Invokers.java#l481
[3]
http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/75d680f9f3bb/src/java.base/share/classes/java/lang/invoke/MethodHandle.java#l760
On 6/10/16 10:00 PM, Graves, Ian L wrote:
>
> All,
>
> I've been going through the docs and the code and was curious about the difference between invoke() and invokeExact() on a MethodHandle object on the back end. Specifically its inlining and code-path-length characteristics. Currently code snippets makes use of invokeExact, but there are some cases now where if we want to pass in arrays instead of objects, invokeExact is too restrictive and only invoke will work. The work around is to create another code snippet that accepts the object of the correct type, but this seems a bit repetitive. My gut tells me that the restrictiveness of invokeExact would come with positive back end tradeoffs, but I wanted to verify the nature of these tradeoffs with somebody in the know. Does anybody know if, in the case of code snippets, one is more desirable than the other, or - what the overhead of invoke would be if used instead?
>
> Thanks!
>
> Ian
>
More information about the panama-dev
mailing list