8267321: Use switch expression for VarHandle$AccessMode lookup

Reviewed-by: jvernee
This commit is contained in:
Claes Redestad 2021-05-19 15:23:44 +00:00
parent fdd0352884
commit 9760dba71c
2 changed files with 34 additions and 59 deletions

View File

@ -1890,18 +1890,6 @@ public abstract class VarHandle implements Constable {
GET_AND_BITWISE_XOR_ACQUIRE("getAndBitwiseXorAcquire", AccessType.GET_AND_UPDATE),
;
static final Map<String, AccessMode> methodNameToAccessMode;
static {
AccessMode[] values = AccessMode.values();
// Initial capacity of # values divided by the load factor is sufficient
// to avoid resizes for the smallest table size (64)
int initialCapacity = (int)(values.length / 0.75f) + 1;
methodNameToAccessMode = new HashMap<>(initialCapacity);
for (AccessMode am : values) {
methodNameToAccessMode.put(am.methodName, am);
}
}
final String methodName;
final AccessType at;
@ -1934,9 +1922,40 @@ public abstract class VarHandle implements Constable {
* @see #methodName()
*/
public static AccessMode valueFromMethodName(String methodName) {
AccessMode am = methodNameToAccessMode.get(methodName);
if (am != null) return am;
throw new IllegalArgumentException("No AccessMode value for method name " + methodName);
return switch (methodName) {
case "get" -> GET;
case "set" -> SET;
case "getVolatile" -> GET_VOLATILE;
case "setVolatile" -> SET_VOLATILE;
case "getAcquire" -> GET_ACQUIRE;
case "setRelease" -> SET_RELEASE;
case "getOpaque" -> GET_OPAQUE;
case "setOpaque" -> SET_OPAQUE;
case "compareAndSet" -> COMPARE_AND_SET;
case "compareAndExchange" -> COMPARE_AND_EXCHANGE;
case "compareAndExchangeAcquire" -> COMPARE_AND_EXCHANGE_ACQUIRE;
case "compareAndExchangeRelease" -> COMPARE_AND_EXCHANGE_RELEASE;
case "weakCompareAndSet" -> WEAK_COMPARE_AND_SET;
case "weakCompareAndSetPlain" -> WEAK_COMPARE_AND_SET_PLAIN;
case "weakCompareAndSetAcquire" -> WEAK_COMPARE_AND_SET_ACQUIRE;
case "weakCompareAndSetRelease" -> WEAK_COMPARE_AND_SET_RELEASE;
case "getAndSet" -> GET_AND_SET;
case "getAndSetAcquire" -> GET_AND_SET_ACQUIRE;
case "getAndSetRelease" -> GET_AND_SET_RELEASE;
case "getAndAdd" -> GET_AND_ADD;
case "getAndAddAcquire" -> GET_AND_ADD_ACQUIRE;
case "getAndAddRelease" -> GET_AND_ADD_RELEASE;
case "getAndBitwiseOr" -> GET_AND_BITWISE_OR;
case "getAndBitwiseOrRelease" -> GET_AND_BITWISE_OR_RELEASE;
case "getAndBitwiseOrAcquire" -> GET_AND_BITWISE_OR_ACQUIRE;
case "getAndBitwiseAnd" -> GET_AND_BITWISE_AND;
case "getAndBitwiseAndRelease" -> GET_AND_BITWISE_AND_RELEASE;
case "getAndBitwiseAndAcquire" -> GET_AND_BITWISE_AND_ACQUIRE;
case "getAndBitwiseXor" -> GET_AND_BITWISE_XOR;
case "getAndBitwiseXorRelease" -> GET_AND_BITWISE_XOR_RELEASE;
case "getAndBitwiseXorAcquire" -> GET_AND_BITWISE_XOR_ACQUIRE;
default -> throw new IllegalArgumentException("No AccessMode value for method name " + methodName);
};
}
}

View File

@ -1,44 +0,0 @@
/*
* Copyright (c) 2018, Oracle and/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.
*/
/**
* @test
* @bug 8200788
* @summary Optimal initial capacity of AccessMode.methodNameToAccessMode
* @library /test/lib
* @modules java.base/java.lang.invoke:open
* java.base/java.util:open
* @build jdk.test.lib.util.OptimalCapacity
* @run main OptimalMapSize
*/
import java.lang.invoke.VarHandle.AccessMode;
import jdk.test.lib.util.OptimalCapacity;
public class OptimalMapSize {
public static void main(String[] args) throws Throwable {
int initialCapacity = (int)(AccessMode.values().length / 0.75f) + 1;
OptimalCapacity.ofHashMap(AccessMode.class, "methodNameToAccessMode",
initialCapacity);
}
}