Any way to further optimize Java reflective method invocation?

前端 未结 5 1344
感动是毒
感动是毒 2020-12-08 08:22

I am wondering if there are any additional optimizations I can implement to improve the speed of reflective invocations in Java. Not that the performance is prohibitive, but

5条回答
  •  囚心锁ツ
    2020-12-08 08:53

    I ran some tests around Chris Jester-Young's answer and using the verbose options, I definitely observed the compiler take some action around the 15th invocation. Hard to say if there's much performance differential without a more sophisticated test, but it is persuasive. Here's the output:

    Test# 0
    Test# 1
    Test# 2
    Test# 3
    Test# 4
    Test# 5
    Test# 6
    Test# 7
    Test# 8
    Test# 9
    Test# 10
    Test# 11
    Test# 12
    Test# 13
    Test# 14
    [Loaded sun.reflect.ClassFileConstants from C:\jdk1.5.0_06\jre\lib\rt.jar]
    [Loaded sun.reflect.AccessorGenerator from C:\jdk1.5.0_06\jre\lib\rt.jar]
    [Loaded sun.reflect.MethodAccessorGenerator from C:\jdk1.5.0_06\jre\lib\rt.jar]
    [Loaded sun.reflect.ByteVectorFactory from C:\jdk1.5.0_06\jre\lib\rt.jar]
    [Loaded sun.reflect.ByteVector from C:\jdk1.5.0_06\jre\lib\rt.jar]
    [Loaded sun.reflect.ByteVectorImpl from C:\jdk1.5.0_06\jre\lib\rt.jar]
    [Loaded sun.reflect.ClassFileAssembler from C:\jdk1.5.0_06\jre\lib\rt.jar]
    [Loaded sun.reflect.UTF8 from C:\jdk1.5.0_06\jre\lib\rt.jar]
    [Loaded java.lang.Void from C:\jdk1.5.0_06\jre\lib\rt.jar]
    [Loaded sun.reflect.Label from C:\jdk1.5.0_06\jre\lib\rt.jar]
    [Loaded sun.reflect.Label$PatchInfo from C:\jdk1.5.0_06\jre\lib\rt.jar]
    [Loaded java.util.AbstractList$Itr from C:\jdk1.5.0_06\jre\lib\rt.jar]
    [Loaded sun.reflect.MethodAccessorGenerator$1 from C:\jdk1.5.0_06\jre\lib\rt.jar]
    [Loaded sun.reflect.ClassDefiner from C:\jdk1.5.0_06\jre\lib\rt.jar]
    [Loaded sun.reflect.ClassDefiner$1 from C:\jdk1.5.0_06\jre\lib\rt.jar]
    [Loaded sun.reflect.GeneratedMethodAccessor1 from __JVM_DefineClass__]
    Test# 15
    Test# 16
    Test# 17
    

    I guess the InvokeDynamic business is not attracting too many developers on the basis of reflection speedup/elimination.

    Thanks Chris.

提交回复
热议问题