Skip to content

Commit

Permalink
8306075: Micro-optimize Enum.hashCode
Browse files Browse the repository at this point in the history
Co-authored-by: Aleksey Shipilev <shade@openjdk.org>
Reviewed-by: redestad, shade, rriggs, liach, apangin, jvernee
  • Loading branch information
olivergillespie and shipilev committed Apr 21, 2023
1 parent fdaabd6 commit 3da987a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/java.base/share/classes/java/lang/Enum.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.lang.invoke.MethodHandles;
import java.util.Optional;

import jdk.internal.vm.annotation.Stable;

import static java.util.Objects.requireNonNull;

/**
Expand Down Expand Up @@ -166,13 +168,28 @@ public final boolean equals(Object other) {
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;
}

/**
Expand Down
55 changes: 55 additions & 0 deletions test/micro/org/openjdk/bench/java/lang/EnumHashCode.java
Original file line number Diff line number Diff line change
@@ -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();
}

}

1 comment on commit 3da987a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.