RFR: 8339358: Optimize TypeKind#from

Shaojin Wen swen at openjdk.org
Sun Sep 1 23:34:57 UTC 2024


On Fri, 30 Aug 2024 00:19:35 GMT, ExE Boss <duke at openjdk.org> wrote:

>> TypeKind.from(Class) is a frequently called method, which provides a specialized method to improve performance.
>> 
>> The following Compiler log shows that the call stack level is reduced and two reference accesses (descriptorString() -> String.value) are reduced, which can reduce the performance degradation caused by cache misses.
>> 
>> * baseline
>> 
>> @ 48   java.lang.classfile.TypeKind::from (25 bytes)   inline
>>   @ 1   java.lang.Class::isPrimitive (0 bytes)   intrinsic
>>   @ 10   java.lang.Class::descriptorString (170 bytes)   failed to inline: callee is too large
>>   @ 15   java.lang.classfile.TypeKind::fromDescriptor (232 bytes)   failed to inline: callee is too large
>> 
>> 
>> * current
>> 
>> @ 52   java.lang.classfile.TypeKind::from (103 bytes)   failed to inline: callee is too large
>
> src/java.base/share/classes/java/lang/classfile/TypeKind.java line 176:
> 
>> 174:         if (cl == double.class ) return DoubleType;
>> 175:         if (cl == void.class   ) return VoidType;
>> 176:         else                     return ReferenceType;
> 
> This can call `Class​::isPrimitive()` to perform an implicit null check and short‑circuit for reference types:
> Suggestion:
> 
>         if (cl.isPrimitive()) { // implicit null check
>             if (cl == boolean.class) return BooleanType;
>             if (cl == byte.class   ) return ByteType;
>             if (cl == char.class   ) return CharType;
>             if (cl == int.class    ) return IntType;
>             if (cl == long.class   ) return LongType;
>             if (cl == short.class  ) return ShortType;
>             if (cl == float.class  ) return FloatType;
>             if (cl == double.class ) return DoubleType;
>             if (cl == void.class   ) return VoidType;
>         }
>         return ReferenceType;

https://github.com/openjdk/jdk/pull/20759#issuecomment-2317655533 
Refer to @cl4es 's comment, isPrimitive is a native method and will be slow

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

PR Review Comment: https://git.openjdk.org/jdk/pull/20762#discussion_r1737599447


More information about the core-libs-dev mailing list