RFR: 8330681: Explicit hashCode and equals for java.lang.runtime.SwitchBootstraps$TypePairs

Chen Liang liach at openjdk.org
Sat Apr 20 07:42:27 UTC 2024


On Sat, 20 Apr 2024 00:00:54 GMT, ExE Boss <duke at openjdk.org> wrote:

>> We can reduce overhead of first use of a switch bootstrap by moving `typePairToName` into `TypePairs` and by explicitly overriding `hashCode` and `equals`. The first change avoids loading and initializing the `TypePairs` class in actual cases, the second remove some excess code generation from happening on first use.
>
> src/java.base/share/classes/java/lang/runtime/SwitchBootstraps.java line 685:
> 
>> 683:     record TypePairs(Class<?> from, Class<?> to) {
>> 684: 
>> 685:         private static final Map<TypePairs, String> typePairToName = initialize();
> 
> If `TypePairs.typePairToName` is never modified after initialisation, then it should probably be made immutable:
> Suggestion:
> 
>         private static final Map<TypePairs, String> typePairToName = Map.copyOf(initialize());

If you really think about it, the `initialize` method itself is somewhat problematic, as it's initializing with byte/short/char on the left, all of which are already converted to int in the of() factory. This should be done in a separate issue.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/18865#discussion_r1573195851


More information about the core-libs-dev mailing list