Performance of java.lang.reflect.Array

后端 未结 1 1567
天涯浪人
天涯浪人 2021-01-08 00:42

Since I\'m making heavy use of reflective access to arrays in a project, I decided to compare the performance of array[index] vs java.lang.reflect.Array.g

相关标签:
1条回答
  • 2021-01-08 01:31

    Yes, Array.get is slow in OpenJDK / Oracle JDK because it is implemented by a native method and is not optimized by JIT.

    There is no special reason for Array.get to be native except that it has been so from the earliest releases of JDK (when JVM was not so good and there was no JIT at all). Moreover, there is a pure Java compatible implementation of java.lang.reflect.Array from GNU Classpath.

    Currently (as of JDK 8u45) only Array.newInstance and Array.getLength are optimized (being JVM intrinsics). Looks like nobody really cared about performance of reflective get/set methods. But as @Marco13 noticed there is an open issue JDK-8051447 to improve the performance of Array.* methods somewhen in future.

    0 讨论(0)
提交回复
热议问题