Hi Stephen,
this is surprising; handles should typically be as fast or much
faster than reflection (VarHandles can be faster than Unsafe in certain
cases), but may carry a slightly more expensive setup cost - do you have
a reproducer I could try?
8b122 - do you mean 8u122 EA?
Thanks!
/Claes
On 2017-01-12 15:23, Stephen Colebourne wrote:
I've recently tried [1] converting Joda-Beans use of setAccessible()
to use MethodHandle. Since it is a code generator, the actual coding
is relatively easy, and obtaining the MethodHandles.Lookup instance
with the "private" capability is simple. While the MethodHandles API
looks very complex, it isn't too bad to use, although it is
undoubtedly more complex than reflection.
(Note that the standard Joda-Beans technique is to code generate
normal Java code to avoid the need to use reflection, but it can
optionally generate reflection-based code in "light bean" mode. It is
that reflection approach that is being examined here).
The real problem however is performance. In my tests, I am seeing a
MethodHandle approach being 2 to 3 times slower than a reflection
approach for identical functionality, which is quite a significant
degradation. (using Java 8 b122)
Given the performance, I left to question whether the repeated Jigsaw
advice to use MethodHandle instead of setAccessible is viable - in the
kinds of places that use reflection, performance tends to be critical.
Is there, or has there been, work in Java 9 to improve the performance
of method handles?
Stephen
[1] https://github.com/JodaOrg/joda-beans/commits/wip/methodhandles