Reflection vs MethodHandle performance in Oracle JDK 7u7
Christian Thalinger
christian.thalinger at oracle.com
Fri Oct 12 08:58:55 PDT 2012
Could you rerun the test with a recent JDK 8 build?
-- Chris
On Oct 10, 2012, at 3:02 PM, Ashwin Jayaprakash <ashwin.jayaprakash at gmail.com> wrote:
> Hi, I was looking at the Java 7 InvokeDynamic/MethodHandle feature set and I was curious to know how it performed compared to good old Reflection.
>
> I had refrained from posting this question for some time but I couldn't wait any longer after I saw a reference to this bug fix http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7023639 (Bug ID: 7023639 JSR 292 method handle invocation needs a fast path for compiled code) on this forum. I suppose this change will go into the Oracle JDK sometime later.
>
> I still went ahead with Oracle JDK 7u7 and ran a simple test using Google Caliper (http://code.google.com/p/caliper/) on a Win 7, 64bit, 1 cpu x 2 core x 4 HT, i7 920 laptop.
>
> Q1: MethodHandle still seems slower than Reflection. Will that change sometime in the future?
>
> Q2: Also, am I using MethodHandle the right way in the code Temp::timeHandle(int) below?
>
> Test results:
> 0% Scenario{vm=java, trial=0, benchmark=Reflect, serverMode=-server, memoryMin=-Xmx96M, memoryMax=-Xmx96M} 16.76 ns; ?=0.18 ns @ 10 trials
> 20% Scenario{vm=java, trial=0, benchmark=Handle, serverMode=-server, memoryMin=-Xmx96M, memoryMax=-Xmx96M} 21.98 ns; ?=0.57 ns @ 10 trials
> 40% Scenario{vm=java, trial=0, benchmark=Direct, serverMode=-server, memoryMin=-Xmx96M, memoryMax=-Xmx96M} 4.93 ns; ?=0.06 ns @ 10 trials
> 60% Scenario{vm=java, trial=0, benchmark=Iface, serverMode=-server, memoryMin=-Xmx96M, memoryMax=-Xmx96M} 4.94 ns; ?=0.30 ns @ 10 trials
> 80% Scenario{vm=java, trial=0, benchmark=Static, serverMode=-server, memoryMin=-Xmx96M, memoryMax=-Xmx96M} 4.88 ns; ?=0.03 ns @ 3 trials
>
> benchmark ns linear runtime
> Reflect 16.76 ======================
> Handle 21.98 ==============================
> Direct 4.93 ======
> Iface 4.94 ======
> Static 4.88 ======
>
> vm: java
> trial: 0
> serverMode: -server
> memoryMin: -Xmx96M
> memoryMax: -Xmx96M
>
>
> Test code:
> public class Temp extends SimpleBenchmark {
> public static void main(String[] args) throws Exception {
> Runner.main(Temp.class, args);
> }
>
> public void timeReflect(int reps) throws Exception {
> Method m = K.class.getMethod("exec", Object.class);
>
> K k = new K();
>
> for (int i = 0; i < reps; i++) {
> m.invoke(k, i);
> }
> }
>
> public void timeHandle(int reps) throws Throwable {
> MethodHandle mh = MethodHandles.lookup().findVirtual(K.class, "exec", MethodType.methodType(void.class, Object.class));
>
> K k = new K();
>
> for (int i = 0; i < reps; i++) {
> mh.invokeExact(k, (Object) i);
> }
> }
>
> public void timeDirect(int reps) throws Exception {
> Method m = K.class.getMethod("exec", Object.class);
>
> K k = new K();
>
> for (int i = 0; i < reps; i++) {
> k.exec(i);
> }
> }
>
> public void timeIface(int reps) throws Exception {
> M m = new K();
>
> for (int i = 0; i < reps; i++) {
> m.exec(i);
> }
> }
>
> public void timeStatic(int reps) throws Exception {
> for (int i = 0; i < reps; i++) {
> K.execS(i);
> }
> }
> }
>
> public class K implements M {
> static long s = 0;
>
> long l = 0;
>
> @Override
> public void exec(Object o) {
> l += o.hashCode();
> }
>
> public static void execS(Object o) {
> s += o.hashCode();
> }
> }
>
> public interface M {
> void exec(Object o);
> }
>
> Thanks,
> Ashwin (http://www.ashwinjayaprakash.com).
> _______________________________________________
> mlvm-dev mailing list
> mlvm-dev at openjdk.java.net
> http://mail.openjdk.java.net/mailman/listinfo/mlvm-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/mlvm-dev/attachments/20121012/88d18ea4/attachment.html
More information about the mlvm-dev
mailing list