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:
Oli Gillespie 2023-04-21 09:51:36 +00:00 committed by Aleksey Shipilev
parent fdaabd6eec
commit 3da987adac
2 changed files with 73 additions and 1 deletions

View file

@ -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;
}
/**