MethodHandle performance

前端 未结 3 2097
攒了一身酷
攒了一身酷 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 03:05

    Looks like this was indirectly answered by @AlekseyShipilev in reference to a different query. In the following link How can I improve performance of Field.set (perhap using MethodHandles)?

    If you read through you will see additional benchmarks that show similar findings. It is likely that direct calls can simply be optimized by JIT in ways that According to the findings above, the difference is:

    • MethodHandle.invoke =~195ns
    • MethodHandle.invokeExact =~10ns
    • Direct calls = 1.266ns

    So - direct calls will still be faster, but MH is very fast. For most use-cases this should be sufficient and is certainly faster than the old reflection framework (btw - according to the findings above, reflection is also significantly faster under java8 vm)

    If this difference is significant in your system, i would suggest finding different patterns rather than direct reflection which will support direct calls.

提交回复
热议问题