Search code examples
javaperformancebytecode

Bytecode generated access objects vs GeneratedMethodAccessor


I have bean util library and we cache Method/Fields of properties, of course. Reading and writing goes via reflection.

There is an idea to skip reflection and for each method/field to bytecode-generate a simple object that directly calls the target. For example, if we have setFoo(String s) method, we would call a set(String s) method of this generated class that internally calls setFoo(). Again, we are replacing the reflection call with the runtime generated direct call.

I know Java does similar thing with GeneratedMethodAccessor. But it's cache may be limited by JVM argument.

Does anyone know if it make sense to roll-on my implementation, considering the performance? On one hand, it sounds fine, but on other, there are many new classes that will be created - and fill perm gen space.

Any experience on this subject?


Solution

  • You are trying to re-invent cglib's FastMethod
    In fact, Reflection is not slower at all. See https://stackoverflow.com/a/23580143/3448419

    Reflection can do more than 50,000,000 invocations per second. It is unlikely to be a bottleneck.