MethodHandle performance

前端 未结 3 2138
攒了一身酷
攒了一身酷 2021-02-07 02:30

I wrote a little benchmark that tests performance of java.lang.invoke.MethodHandle, java.lang.reflect.Method and direct calls of methods.

I re

3条回答
  •  时光取名叫无心
    2021-02-07 02:53

    If the publicStaticMethod was a simple implementation like returning a constant, It is very much possible that direct call was in-lined by JIT compiler. This may not be possible with methodHandles.

    RE http://vanillajava.blogspot.com/2011/08/methodhandle-performance-in-java-7.html example, as mentioned that comments its not great implementation. if you change the type casting to int (instead of Integer) in the calculation loop, the results are closer to direct method call.

    With convoluted implementation of ( creating and calling a future task which returns a random int) gave benchmark with closer numbers where MethodStatic was max ~10% slower than direct method. So you might be seeing 3 times slower performance due to JIT optimizations

提交回复
热议问题