MCPcopy Index your code
hub / github.com/daniellansun/fast-reflection

github.com/daniellansun/fast-reflection @1.0.3

Chat with this repo
repository ↗ · DeepWiki ↗ · release 1.0.3 ↗ · + Follow
292 symbols 844 edges 30 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

fast-reflection

Yet another extremely fast alternative for Java reflection, making dynamic calls as efficient as direct calls.

Usage

Use fast-reflection as enhanced Java reflection, see the tests for now. * FastMethod * FastConstructor * FastField * FastClass

Note: FastXXX instances are immutable, so it's strongly recommended to reuse them as possible as we could for better performance.

Benchmark Test

Run gradlew jmh to get the benchmark test report

  • 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

Summary

  • 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

Benchmark Test Report

Benchmark                                                                      Mode  Cnt   Score   Error  Units
FastMethodPerfTest.constructor_constant_handle_StringCtorCharArray             avgt   15  12.140 ± 0.064  ns/op
FastMethodPerfTest.constructor_constant_lambdametafactory_StringCtorCharArray  avgt   15  12.137 ± 0.052  ns/op
FastMethodPerfTest.constructor_direct_StringCtorCharArray                      avgt   15  12.066 ± 0.031  ns/op
FastMethodPerfTest.constructor_fastreflect_StringCtorCharArray                 avgt   15  14.169 ± 0.053  ns/op
FastMethodPerfTest.constructor_instance_handle_StringCtorCharArray             avgt   15  16.098 ± 0.145  ns/op
FastMethodPerfTest.constructor_instance_lambdametafactory_StringCtorCharArray  avgt   15  12.263 ± 0.428  ns/op
FastMethodPerfTest.constructor_reflect_StringCtorCharArray                     avgt   15  17.296 ± 0.029  ns/op
FastMethodPerfTest.constructor_reflect_accessible_StringCtorCharArray          avgt   15  16.646 ± 0.104  ns/op
FastMethodPerfTest.field_constant_handle_IntegerSize                           avgt   15   2.574 ± 0.165  ns/op
FastMethodPerfTest.field_direct_IntegerSize                                    avgt   15   2.692 ± 0.037  ns/op
FastMethodPerfTest.field_fastreflect_IntegerSize                               avgt   15   3.365 ± 0.034  ns/op
FastMethodPerfTest.field_instance_handle_IntegerSize                           avgt   15   6.477 ± 0.141  ns/op
FastMethodPerfTest.field_reflect_IntegerSize                                   avgt   15   5.801 ± 0.378  ns/op
FastMethodPerfTest.field_reflect_accessible_IntegerSize                        avgt   15   5.026 ± 0.006  ns/op
FastMethodPerfTest.method_constant_handle_StringStartsWith                     avgt   15   2.584 ± 0.173  ns/op
FastMethodPerfTest.method_constant_lambdametafactory_StringStartsWith          avgt   15   2.975 ± 0.117  ns/op
FastMethodPerfTest.method_direct_StringStartsWith                              avgt   15   2.873 ± 0.150  ns/op
FastMethodPerfTest.method_fastreflect_StringStartsWith                         avgt   15   3.206 ± 0.212  ns/op
FastMethodPerfTest.method_instance_handle_StringStartsWith                     avgt   15   8.924 ± 0.312  ns/op
FastMethodPerfTest.method_instance_lambdametafactory_StringStartsWith          avgt   15   3.539 ± 0.235  ns/op
FastMethodPerfTest.method_reflect_StringStartsWith                             avgt   15  10.681 ± 0.626  ns/op
FastMethodPerfTest.method_reflect_accessible_StringStartsWith                  avgt   15   9.322 ± 0.341  ns/op

FAQ

How fast can fast-reflection run?

fast-reflection runs almost as fast as direct calls, and much faster than normal reflection(see the above benchmark test report).

Method handles are available since Java 7, why to use fast-reflection?

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.

How to use fast-reflection?

fast-reflection provides similar API of Java reflection for dynamic invocation cases, so use fast-reflection as enhanced Java reflection.

What dependencies does fast-reflection require?

Just the ASM.

Extension points exported contracts — how you extend this code

FastMember (Interface)
(no doc) [8 implementers]
src/main/java/me/sunlan/fastreflection/FastMember.java
MemberLoadable (Interface)
(no doc) [2 implementers]
src/main/java/me/sunlan/fastreflection/MemberLoadable.java

Core symbols most depended-on inside this repo

create
called by 30
src/main/java/me/sunlan/fastreflection/FastClass.java
invoke
called by 26
src/main/java/me/sunlan/fastreflection/FastMethod.java
getName
called by 20
src/main/java/me/sunlan/fastreflection/FastMember.java
create
called by 20
src/main/java/me/sunlan/fastreflection/FastMethod.java
getMethod
called by 18
src/main/java/me/sunlan/fastreflection/FastClass.java
get
called by 17
src/main/java/me/sunlan/fastreflection/FastField.java
getModifiers
called by 17
src/main/java/me/sunlan/fastreflection/FastMember.java
getConstructor
called by 11
src/main/java/me/sunlan/fastreflection/FastClass.java

Shape

Method 262
Class 27
Interface 3

Languages

Java100%

Modules by API surface

src/jmh/java/me/sunlan/fastreflection/FastMethodPerfTest.java29 symbols
src/main/java/me/sunlan/fastreflection/FastClass.java28 symbols
src/test/java/me/sunlan/fastreflection/FastClassTest.java21 symbols
src/test/java/me/sunlan/fastreflection/FastMethodTest.java18 symbols
src/main/java/me/sunlan/fastreflection/generator/FastMethodGenerator.java14 symbols
src/main/java/me/sunlan/fastreflection/generator/FastMemberGenerator.java14 symbols
src/main/java/me/sunlan/fastreflection/generator/FastConstructorGenerator.java14 symbols
src/main/java/me/sunlan/fastreflection/LazyFastMethod.java13 symbols
src/main/java/me/sunlan/fastreflection/LazyFastConstructor.java12 symbols
src/main/java/me/sunlan/fastreflection/LazyFastField.java11 symbols
src/main/java/me/sunlan/fastreflection/FastMethod.java11 symbols
src/main/java/me/sunlan/fastreflection/FastField.java11 symbols

For agents

$ claude mcp add fast-reflection \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact