Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

MethodHandle performance in Java 7

DZone's Guide to

MethodHandle performance in Java 7

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

A new feature in Java 7 provides a MethodHandle which "is a typed, directly executable reference to an underlying method, constructor, field, or similar low-level operation, with optional transformations of arguments or return values"

This supports currying methods and many other features lambda based languages take for granted.

Example

Say you have two method
public static int multiply(int i, int j) {
    return i * j;
}

public static int subtract(int i, int j) {
    return i - j;
}
With MethodHandle you can not only get a reference to the Method (like in reflection) but you can construct new MethodHandles. For example, you can bind the Object concerned or one of the arguments.

final Lookup lookup = lookup();
MethodHandle multiply = lookup.findStatic(MethodHandleMain.class, "multiply", methodType(int.class, int.class, int.class));
MethodHandle quadruple = insertArguments(multiply, 1, 4);

System.out.println(multiply.invoke(3, 2)); // prints 6
System.out.println(quadruple.invoke(5)); // prints 20

MethodHandle subtract = lookup.findStatic(MethodHandleMain.class, "subtract", methodType(int.class, int.class, int.class));
MethodHandle subtractFromFour = insertArguments(subtract, 0, 4);
MethodHandle fourLess = insertArguments(subtract, 1, 4);
System.out.println(subtract.invoke(10, 5)); // prints 5
System.out.println(subtractFromFour.invoke(10)); // prints -6
System.out.println(fourLess.invoke(10)); // prints 6

Performance

This is very cool, but how does it perform? ;)

There is a lot of potential for it to perform very well and the interface is cleaner than plain reflections however when comparing the alternatives, it doesn't perform as well as the alternatives in Java 7 (update 0)
Call method Average time per call
Direct Method calls 31 ns
Reflections 353 ns
MethodHandle.invoke() 5,378 ns
It is a very cool API and hopefully it will perform much better in the future. ;)

The code

MethodHandleTest.java

 

From http://vanillajava.blogspot.com/2011/08/methodhandle-performance-in-java-7.html

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}