On Tue, 19 Nov 2024 16:08:20 GMT, Hannes Greule <hgre...@openjdk.org> wrote:

>> When core reflection was migrated to be implemented by Method Handles, 
>> somehow, the method handles are not used for native methods, which are 
>> generally linkable by method handles.  This causes significant performance 
>> regressions when reflecting native methods, even if their overrides may be 
>> non-native methods.  This is evident in `Object.clone` and `Object.hashCode` 
>> as shown in the original report.
>> 
>> I believe the blanket restriction previously placed on the native methods 
>> was because of signature polymorphic methods ([JLS 
>> 15.12.3](https://docs.oracle.com/javase/specs/jls/se23/html/jls-15.html#jls-15.12.3),
>>  [JVMS 
>> 2.9.3](https://docs.oracle.com/javase/specs/jvms/se23/html/jvms-2.html#jvms-2.9.3))
>>  for MethodHandle and VarHandle; method handles do not link to the backing 
>> implementation that throws UOE while core reflection is required to do so.  
>> I have narrowed the restrictions to be specifically against these methods.
>> 
>> Additionally, I cleaned up another check for invalid varargs flag.  
>> Together, I clarified the scenarios where native method accessors are used - 
>> all to bypass restrictions of java.lang.invoke.
>> 
>> Testing: tier 1-5 green
>
> src/java.base/share/classes/jdk/internal/reflect/MethodHandleAccessorFactory.java
>  line 430:
> 
>> 428:         // Single parameter of declared type Object[]
>> 429:         Class<?>[] parameters = 
>> reflectionFactory.getExecutableSharedParameterTypes(method);
>> 430:         return parameters.length == 1 && parameters[0] == 
>> Object[].class;
> 
> Would it be possible to check for the `PolymorphicSignature` annotation 
> instead?

I think the main problem with annotation is that annotation creation needs to 
use `java.lang.reflect.Proxy`, which is unavailable until module system is 
initialized. Extra allocations are costs too, while checks here only reuse 
existing objects.

In fact, I don't think Java's core libraries ever check for annotations, and 
the annotation's support class, `jdk.internal.reflect.ConstantPool`, has always 
been only loaded but never initialized in a simple hello world program since 
Java 5.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/22169#discussion_r1848692679

Reply via email to