Skip to content

8312423: Cache the enum values for VarHandle.AccessType #14943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,6 @@ public boolean isAccessModeSupported(AccessMode accessMode) {
@Override
MethodHandle getMethodHandleUncached(int mode) {
MethodHandle targetHandle = target.getMethodHandle(mode); // might throw UOE of access mode is not supported, which is ok
return handleFactory.apply(AccessMode.values()[mode], targetHandle);
return handleFactory.apply(AccessMode.valueFromOrdinal(mode), targetHandle);
}
}
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/lang/invoke/VarForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ final MemberName getMemberNameOrNull(int mode) {

@DontInline
MemberName resolveMemberName(int mode) {
AccessMode value = AccessMode.values()[mode];
AccessMode value = AccessMode.valueFromOrdinal(mode);
String methodName = value.methodName();
MethodType type = methodType_table[value.at.ordinal()].insertParameterTypes(0, VarHandle.class);
return memberName_table[mode] = MethodHandles.Lookup.IMPL_LOOKUP
Expand All @@ -144,7 +144,7 @@ MemberName resolveMemberName(int mode) {

@ForceInline
final MethodType[] getMethodType_V_init() {
MethodType[] table = new MethodType[VarHandle.AccessType.values().length];
MethodType[] table = new MethodType[VarHandle.AccessType.COUNT];
for (int i = 0; i < methodType_table.length; i++) {
MethodType mt = methodType_table[i];
// TODO only adjust for sig-poly methods returning Object
Expand Down
14 changes: 12 additions & 2 deletions src/java.base/share/classes/java/lang/invoke/VarHandle.java
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,11 @@ private static int fillParameters(Class<?>[] ps,
ps[i++] = intermediate[j];
return i;
}

private static final @Stable AccessType[] VALUES = values();
static AccessType valueFromOrdinal(int mode) {
return VALUES[mode];
}
}

/**
Expand Down Expand Up @@ -2003,6 +2008,11 @@ public static AccessMode valueFromMethodName(String methodName) {
default -> throw new IllegalArgumentException("No AccessMode value for method name " + methodName);
};
}

private static final @Stable AccessMode[] VALUES = values();
static AccessMode valueFromOrdinal(int mode) {
return VALUES[mode];
}
}

static final class AccessDescriptor {
Expand Down Expand Up @@ -2120,7 +2130,7 @@ final MethodType accessModeType(int accessTypeOrdinal) {
}

final MethodType accessModeTypeUncached(int accessTypeOrdinal) {
return accessModeTypeUncached(AccessType.values()[accessTypeOrdinal]);
return accessModeTypeUncached(AccessType.valueFromOrdinal(accessTypeOrdinal));
}

abstract MethodType accessModeTypeUncached(AccessType accessMode);
Expand Down Expand Up @@ -2215,7 +2225,7 @@ final MethodHandle getMethodHandle(int mode) {
* @throws UnsupportedOperationException if the access mode is not supported
*/
MethodHandle getMethodHandleUncached(int mode) {
MethodType mt = accessModeType(AccessMode.values()[mode]).
MethodType mt = accessModeType(AccessMode.valueFromOrdinal(mode)).
insertParameterTypes(0, VarHandle.class);
MemberName mn = vform.getMemberName(mode);
DirectMethodHandle dmh = DirectMethodHandle.make(mn);
Expand Down