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

ExE Boss duke at openjdk.org
Sat Apr 20 00:06:28 UTC 2024


On Fri, 19 Apr 2024 13:23:53 GMT, Claes Redestad <redestad 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());

src/java.base/share/classes/java/lang/runtime/SwitchBootstraps.java line 699:

> 697: 
> 698:         public boolean equals(Object other) {
> 699:             if (other instanceof TypePairs otherPair) {

To match the behaviour of the `equals` method body generated by `java​.lang​.runtime​.ObjectMethods​::bootstrap`, this should include a fast path check for `this == other`:
Suggestion:

            if (this == other) {
                return true;
            }
            if (other instanceof TypePairs otherPair) {

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

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


More information about the core-libs-dev mailing list