RFR: 8339358: Optimize TypeKind#from

Claes Redestad redestad at openjdk.org
Sun Sep 1 23:34:57 UTC 2024


On Fri, 30 Aug 2024 01:21:14 GMT, Shaojin Wen <swen at openjdk.org> wrote:

>> 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

I said it can be relatively slow in the interpreter. This was an invitation to measure carefully (with and without `-Xint`), not an authoritative statement that we should avoid `Class::isPrimitive` everywhere. I think you need to provide supporting evidence that these kinds of optimizations are worthwhile.

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

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


More information about the core-libs-dev mailing list