Yet another extremely fast alternative for Java reflection, making dynamic calls as efficient as direct calls.
Use fast-reflection as enhanced Java reflection, see the tests for now.
Note:FastXXX instances are immutable, so it's strongly recommended to reuse them as possible as we could for better performance.
- JMH version: 1.33
- VM version: JDK 11.0.9, OpenJDK 64-Bit Server VM, 11.0.9+11-LTS
- VM options: -Xms2g -Xmx2g -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=512m -XX:+UseG1GC
- Blackhole mode: full + dont-inline hint (default, use -Djmh.blackhole.autoDetect=true to auto-detect)
- Warmup: 3 iterations, 1 s each
- Measurement: 5 iterations, 1 s each
- Timeout: 10 min per iteration
- Threads: 1 thread, will synchronize iterations
- Benchmark mode: Average time, time/op
- fast-reflection runs almost as fast as direct call
- fast-reflection runs almost as fast as constant method handle
- fast-reflection runs almost as fast as lambda meta factory
- fast-reflection runs much faster than normal reflection
- instance method handle runs almost as slow as normal reflection
BenchmarkModeCntScoreErrorUnitsFastMethodPerfTest.constructor_constant_handle_StringCtorCharArrayavgt1512.140 ± 0.064ns/opFastMethodPerfTest.constructor_constant_lambdametafactory_StringCtorCharArrayavgt1512.137 ± 0.052ns/opFastMethodPerfTest.constructor_direct_StringCtorCharArrayavgt1512.066 ± 0.031ns/opFastMethodPerfTest.constructor_fastreflect_StringCtorCharArrayavgt1514.169 ± 0.053ns/opFastMethodPerfTest.constructor_instance_handle_StringCtorCharArrayavgt1516.098 ± 0.145ns/opFastMethodPerfTest.constructor_instance_lambdametafactory_StringCtorCharArrayavgt1512.263 ± 0.428ns/opFastMethodPerfTest.constructor_reflect_StringCtorCharArrayavgt1517.296 ± 0.029ns/opFastMethodPerfTest.constructor_reflect_accessible_StringCtorCharArrayavgt1516.646 ± 0.104ns/opFastMethodPerfTest.field_constant_handle_IntegerSizeavgt152.574 ± 0.165ns/opFastMethodPerfTest.field_direct_IntegerSizeavgt152.692 ± 0.037ns/opFastMethodPerfTest.field_fastreflect_IntegerSizeavgt153.365 ± 0.034ns/opFastMethodPerfTest.field_instance_handle_IntegerSizeavgt156.477 ± 0.141ns/opFastMethodPerfTest.field_reflect_IntegerSizeavgt155.801 ± 0.378ns/opFastMethodPerfTest.field_reflect_accessible_IntegerSizeavgt155.026 ± 0.006ns/opFastMethodPerfTest.method_constant_handle_StringStartsWithavgt152.584 ± 0.173ns/opFastMethodPerfTest.method_constant_lambdametafactory_StringStartsWithavgt152.975 ± 0.117ns/opFastMethodPerfTest.method_direct_StringStartsWithavgt152.873 ± 0.150ns/opFastMethodPerfTest.method_fastreflect_StringStartsWithavgt153.206 ± 0.212ns/opFastMethodPerfTest.method_instance_handle_StringStartsWithavgt158.924 ± 0.312ns/opFastMethodPerfTest.method_instance_lambdametafactory_StringStartsWithavgt153.539 ± 0.235ns/opFastMethodPerfTest.method_reflect_StringStartsWithavgt1510.681 ± 0.626ns/opFastMethodPerfTest.method_reflect_accessible_StringStartsWithavgt159.322 ± 0.341ns/opfast-reflection runs almost as fast as direct calls, and much faster than normal reflection(see the above benchmark test report).
Only constant method handles can gain the best performance, but reflection runs on the fly, so it's impossible for us to pre-define all constant method handles we may use. The fast-reflection will generate constants method handles for dynamic calls to gain the best performance, this is the reason why we need the custom library. BTW, instance method handles are much flexible but can not help us gain better performance because they run almost as slow as normal reflection.
fast-reflection provides similar API of Java reflection for dynamic invocation cases, so use fast-reflection as enhanced Java reflection.
Just the ASM.