8306075: Micro-optimize Enum.hashCode
Co-authored-by: Aleksey Shipilev <shade@openjdk.org> Reviewed-by: redestad, shade, rriggs, liach, apangin, jvernee
This commit is contained in:
parent
fdaabd6eec
commit
3da987adac
@ -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<E extends Enum<E>>
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
55
test/micro/org/openjdk/bench/java/lang/EnumHashCode.java
Normal file
55
test/micro/org/openjdk/bench/java/lang/EnumHashCode.java
Normal file
@ -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();
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user