From 942ba9f4ffc1b1ee11552717d230dfdcd953a475 Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Sat, 6 Nov 2021 15:09:56 +0800 Subject: [PATCH] [3.0] Pre-allocate map size in Directory (#9222) --- .../registry/client/ServiceDiscoveryRegistryDirectory.java | 4 ++-- .../apache/dubbo/registry/integration/RegistryDirectory.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistryDirectory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistryDirectory.java index 7f10291b72f..29f07bfa0ce 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistryDirectory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistryDirectory.java @@ -40,12 +40,12 @@ import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import static org.apache.dubbo.common.constants.CommonConstants.DISABLED_KEY; import static org.apache.dubbo.common.constants.CommonConstants.ENABLED_KEY; @@ -256,7 +256,7 @@ private void refreshInvoker(List invokerUrls) { * @return invokers */ private Map> toInvokers(Map> oldUrlInvokerMap, List urls) { - Map> newUrlInvokerMap = new HashMap<>(); + Map> newUrlInvokerMap = new ConcurrentHashMap<>(urls == null ? 1 : (int) (urls.size() / 0.75f + 1)); if (urls == null || urls.isEmpty()) { return newUrlInvokerMap; } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java index fe0c8ee58a2..d02823b2686 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java @@ -327,7 +327,7 @@ private Optional> toRouters(List urls) { * @return invokers */ private Map> toInvokers(Map> oldUrlInvokerMap, List urls) { - Map> newUrlInvokerMap = new ConcurrentHashMap<>(); + Map> newUrlInvokerMap = new ConcurrentHashMap<>(urls == null ? 1 : (int) (urls.size() / 0.75f + 1)); if (urls == null || urls.isEmpty()) { return newUrlInvokerMap; }