Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.
/ jdk15u-dev Public archive

Commit 5c9dc7b

Browse files
cliveverghesePaul Hohensee
authored and
Paul Hohensee
committed
8270317: Large Allocation in CipherSuite
Backport-of: e627caec84c169c99c04e0d355c29b806a0266ed
1 parent 010d867 commit 5c9dc7b

File tree

2 files changed

+106
-60
lines changed

2 files changed

+106
-60
lines changed

src/java.base/share/classes/sun/security/ssl/CipherSuite.java

+48-60
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@
2525

2626
package sun.security.ssl;
2727

28-
import java.util.ArrayList;
29-
import java.util.Arrays;
30-
import java.util.Collection;
31-
import java.util.Collections;
32-
import java.util.LinkedList;
33-
import java.util.List;
28+
import java.util.*;
29+
3430
import static sun.security.ssl.CipherSuite.HashAlg.*;
3531
import static sun.security.ssl.CipherSuite.KeyExchange.*;
3632
import static sun.security.ssl.CipherSuite.MacAlg.*;
@@ -857,6 +853,39 @@ enum CipherSuite {
857853

858854
final boolean exportable;
859855

856+
private static final Map<Integer, CipherSuite> cipherSuiteIds;
857+
private static final Map<String, CipherSuite> cipherSuiteNames;
858+
private static final List<CipherSuite> allowedCipherSuites;
859+
private static final List<CipherSuite> defaultCipherSuites;
860+
861+
static {
862+
Map<Integer, CipherSuite> ids = new HashMap<>();
863+
Map<String, CipherSuite> names = new HashMap<>();
864+
List<CipherSuite> allowedCS = new ArrayList<>();
865+
List<CipherSuite> defaultCS = new ArrayList<>();
866+
867+
for(CipherSuite cs : CipherSuite.values()) {
868+
ids.put(cs.id, cs);
869+
names.put(cs.name, cs);
870+
for (String alias : cs.aliases) {
871+
names.put(alias, cs);
872+
}
873+
874+
if (!cs.supportedProtocols.isEmpty()) {
875+
allowedCS.add(cs);
876+
}
877+
878+
if (cs.isDefaultEnabled) {
879+
defaultCS.add(cs);
880+
}
881+
}
882+
883+
cipherSuiteIds = Map.copyOf(ids);
884+
cipherSuiteNames = Map.copyOf(names);
885+
allowedCipherSuites = List.copyOf(allowedCS);
886+
defaultCipherSuites = List.copyOf(defaultCS);
887+
}
888+
860889
// known but unsupported cipher suite
861890
private CipherSuite(String name, int id) {
862891
this(id, false, name, "",
@@ -894,62 +923,29 @@ private CipherSuite(int id, boolean isDefaultEnabled,
894923
}
895924

896925
static CipherSuite nameOf(String ciperSuiteName) {
897-
for (CipherSuite cs : CipherSuite.values()) {
898-
if (cs.name.equals(ciperSuiteName) ||
899-
cs.aliases.contains(ciperSuiteName)) {
900-
return cs;
901-
}
902-
}
903-
904-
return null;
926+
return cipherSuiteNames.get(ciperSuiteName);
905927
}
906928

907929
static CipherSuite valueOf(int id) {
908-
for (CipherSuite cs : CipherSuite.values()) {
909-
if (cs.id == id) {
910-
return cs;
911-
}
912-
}
913-
914-
return null;
930+
return cipherSuiteIds.get(id);
915931
}
916932

917933
static String nameOf(int id) {
918-
for (CipherSuite cs : CipherSuite.values()) {
919-
if (cs.id == id) {
920-
return cs.name;
921-
}
934+
CipherSuite cs = cipherSuiteIds.get(id);
935+
936+
if (cs != null) {
937+
return cs.name;
922938
}
923939

924940
return "UNKNOWN-CIPHER-SUITE(" + Utilities.byte16HexString(id) + ")";
925941
}
926942

927943
static Collection<CipherSuite> allowedCipherSuites() {
928-
Collection<CipherSuite> cipherSuites = new LinkedList<>();
929-
for (CipherSuite cs : CipherSuite.values()) {
930-
if (!cs.supportedProtocols.isEmpty()) {
931-
cipherSuites.add(cs);
932-
} else {
933-
// values() is ordered, remaining cipher suites are
934-
// not supported.
935-
break;
936-
}
937-
}
938-
return cipherSuites;
944+
return allowedCipherSuites;
939945
}
940946

941947
static Collection<CipherSuite> defaultCipherSuites() {
942-
Collection<CipherSuite> cipherSuites = new LinkedList<>();
943-
for (CipherSuite cs : CipherSuite.values()) {
944-
if (cs.isDefaultEnabled) {
945-
cipherSuites.add(cs);
946-
} else {
947-
// values() is ordered, remaining cipher suites are
948-
// not enabled.
949-
break;
950-
}
951-
}
952-
return cipherSuites;
948+
return defaultCipherSuites;
953949
}
954950

955951
/**
@@ -972,19 +968,11 @@ static List<CipherSuite> validValuesOf(String[] names) {
972968
}
973969

974970
boolean found = false;
975-
for (CipherSuite cs : CipherSuite.values()) {
976-
if (!cs.supportedProtocols.isEmpty()) {
977-
if (cs.name.equals(name) ||
978-
cs.aliases.contains(name)) {
979-
cipherSuites.add(cs);
980-
found = true;
981-
break;
982-
}
983-
} else {
984-
// values() is ordered, remaining cipher suites are
985-
// not supported.
986-
break;
987-
}
971+
CipherSuite cs;
972+
if ((cs = cipherSuiteNames.get(name)) != null
973+
&& !cs.supportedProtocols.isEmpty()) {
974+
cipherSuites.add(cs);
975+
found = true;
988976
}
989977
if (!found) {
990978
throw new IllegalArgumentException(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
package org.openjdk.bench.java.security;
25+
26+
import org.openjdk.jmh.annotations.*;
27+
28+
import java.lang.reflect.InvocationTargetException;
29+
import java.lang.reflect.Method;
30+
import java.util.concurrent.TimeUnit;
31+
32+
33+
@Fork(jvmArgsAppend = {"--add-exports", "java.base/sun.security.ssl=ALL-UNNAMED", "--add-opens", "java.base/sun.security.ssl=ALL-UNNAMED"})
34+
@State(Scope.Benchmark)
35+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
36+
@BenchmarkMode(Mode.Throughput)
37+
public class CipherSuiteBench {
38+
39+
Method nameOf;
40+
41+
@Param({"TLS_AES_256_GCM_SHA384",
42+
"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384",
43+
"TLS_DHE_DSS_WITH_AES_128_CBC_SHA256",
44+
"TLS_DHE_RSA_WITH_AES_256_CBC_SHA" })
45+
String cipherSuite;
46+
47+
@Setup
48+
public void initilizeClass() throws ClassNotFoundException, NoSuchMethodException {
49+
Class<?> cs = Class.forName("sun.security.ssl.CipherSuite");
50+
nameOf = cs.getDeclaredMethod("nameOf", String.class);
51+
nameOf.setAccessible(true);
52+
}
53+
54+
@Benchmark
55+
public Object benchmarkCipherSuite() throws InvocationTargetException, IllegalAccessException {
56+
return nameOf.invoke(null,cipherSuite);
57+
}
58+
}

0 commit comments

Comments
 (0)