From 3da987adacc3acc120a2781ee9a2111e26356723 Mon Sep 17 00:00:00 2001 From: Oli Gillespie Date: Fri, 21 Apr 2023 09:51:36 +0000 Subject: [PATCH] 8306075: Micro-optimize Enum.hashCode Co-authored-by: Aleksey Shipilev Reviewed-by: redestad, shade, rriggs, liach, apangin, jvernee --- .../share/classes/java/lang/Enum.java | 19 ++++++- .../openjdk/bench/java/lang/EnumHashCode.java | 55 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 test/micro/org/openjdk/bench/java/lang/EnumHashCode.java diff --git a/src/java.base/share/classes/java/lang/Enum.java b/src/java.base/share/classes/java/lang/Enum.java index 355d18d621b..56016779243 100644 --- a/src/java.base/share/classes/java/lang/Enum.java +++ b/src/java.base/share/classes/java/lang/Enum.java @@ -37,6 +37,8 @@ import java.lang.constant.DynamicConstantDesc; import java.lang.invoke.MethodHandles; import java.util.Optional; +import jdk.internal.vm.annotation.Stable; + import static java.util.Objects.requireNonNull; /** @@ -166,13 +168,28 @@ public abstract class Enum> return this==other; } + /** + * The hash code of this enumeration constant. + */ + @Stable + private int hash; + /** * Returns a hash code for this enum constant. * * @return a hash code for this enum constant. */ public final int hashCode() { - return super.hashCode(); + // Once initialized, the hash field value does not change. + // HotSpot's identity hash code generation also never returns zero + // as the identity hash code. This makes zero a convenient marker + // for the un-initialized value for both @Stable and the lazy + // initialization code below. + int hc = hash; + if (hc == 0) { + hc = hash = System.identityHashCode(this); + } + return hc; } /** diff --git a/test/micro/org/openjdk/bench/java/lang/EnumHashCode.java b/test/micro/org/openjdk/bench/java/lang/EnumHashCode.java new file mode 100644 index 00000000000..eb941bd86e3 --- /dev/null +++ b/test/micro/org/openjdk/bench/java/lang/EnumHashCode.java @@ -0,0 +1,55 @@ +/* + * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package org.openjdk.bench.java.lang; + +import org.openjdk.jmh.annotations.*; + +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@State(Scope.Thread) +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Fork(3) +public class EnumHashCode { + + enum E { + A, B, C; + } + + Enum e = E.A; + + @Benchmark + public int constant() { + // Tests that hash code is constant-foldable + return E.A.hashCode(); + } + + @Benchmark + public int field() { + // Tests that hash code is efficient + return e.hashCode(); + } + +}