Filter out duplicates before trying to find the alias to use (#10612)

Motivation:

Calling chooseServerAlias(...) may be expensive so we should ensure we not call it multiple times for the same auth methods.

Modifications:

Remove duplicated from authMethods before trying to call chooseServerAlias(...)

Result:

Less performance overhead during key material selection
This commit is contained in:
Norman Maurer 2020-09-25 19:12:28 +02:00
parent c9e42106d1
commit d92d92a923

View File

@ -21,8 +21,10 @@ import javax.net.ssl.X509KeyManager;
import javax.security.auth.x500.X500Principal; import javax.security.auth.x500.X500Principal;
import java.security.PrivateKey; import java.security.PrivateKey;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -68,8 +70,12 @@ final class OpenSslKeyMaterialManager {
if (authMethods.length == 0) { if (authMethods.length == 0) {
return; return;
} }
Set<String> aliases = new HashSet<>(authMethods.length); // authMethods may contain duplicates but call chooseServerAlias(...) may be expensive. So let's ensure
for (String authMethod : authMethods) { // we filter out duplicates.
Set<String> authMethodsSet = new LinkedHashSet<>(authMethods.length);
Collections.addAll(authMethodsSet, authMethods);
Set<String> aliases = new HashSet<>(authMethodsSet.size());
for (String authMethod : authMethodsSet) {
String type = KEY_TYPES.get(authMethod); String type = KEY_TYPES.get(authMethod);
if (type != null) { if (type != null) {
String alias = chooseServerAlias(engine, type); String alias = chooseServerAlias(engine, type);