Skip to content

Commit

Permalink
define DEFAULT_HASHMAP_LOAD_FACTOR constant
Browse files Browse the repository at this point in the history
  • Loading branch information
zrlw committed Sep 21, 2021
1 parent 18ce8b7 commit 8bd0479
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,6 @@ public interface RegistryConstants {

String REGISTRY_SERVICE_REFERENCE_PATH = "org.apache.dubbo.registry.RegistryService";
String INIT = "INIT";

float DEFAULT_HASHMAP_LOAD_FACTOR = 0.75f;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

import static org.apache.dubbo.common.constants.CommonConstants.DISABLED_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.ENABLED_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_HASHMAP_LOAD_FACTOR;
import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_TYPE_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.SERVICE_REGISTRY_TYPE;
Expand All @@ -56,7 +57,6 @@

public class ServiceDiscoveryRegistryDirectory<T> extends DynamicDirectory<T> {
private static final Logger logger = LoggerFactory.getLogger(ServiceDiscoveryRegistryDirectory.class);
private static final float DEFAULT_LOAD_FACTOR = 0.75f;

/**
* instance address to invoker mapping.
Expand Down Expand Up @@ -258,7 +258,7 @@ private void refreshInvoker(List<URL> invokerUrls) {
Map<String, Invoker<T>> oldUrlInvokerMap = null;
if (this.urlInvokerMap != null) {
// the initial capacity should be set greater than the maximum number of entries divided by the load factor to avoid resizing.
oldUrlInvokerMap = new LinkedHashMap<>(Math.round(1 + this.urlInvokerMap.size() / DEFAULT_LOAD_FACTOR));
oldUrlInvokerMap = new LinkedHashMap<>(Math.round(1 + this.urlInvokerMap.size() / DEFAULT_HASHMAP_LOAD_FACTOR));
this.urlInvokerMap.forEach(oldUrlInvokerMap::put);
}
Map<String, Invoker<T>> newUrlInvokerMap = toInvokers(oldUrlInvokerMap, invokerUrls);// Translate url list to Invoker map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import static org.apache.dubbo.common.constants.RegistryConstants.CONFIGURATORS_CATEGORY;
import static org.apache.dubbo.common.constants.RegistryConstants.CONSUMERS_CATEGORY;
import static org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_CATEGORY;
import static org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_HASHMAP_LOAD_FACTOR;
import static org.apache.dubbo.common.constants.RegistryConstants.DYNAMIC_CONFIGURATORS_CATEGORY;
import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL;
import static org.apache.dubbo.common.constants.RegistryConstants.PROVIDERS_CATEGORY;
Expand Down Expand Up @@ -227,7 +228,8 @@ private void refreshInvoker(List<URL> invokerUrls) {
// can't use local reference because this.urlInvokerMap might be accessed at isAvailable() by main thread concurrently.
Map<URL, Invoker<T>> oldUrlInvokerMap = null;
if (this.urlInvokerMap != null) {
oldUrlInvokerMap = new LinkedHashMap<>(this.urlInvokerMap.size());
// the initial capacity should be set greater than the maximum number of entries divided by the load factor to avoid resizing.
oldUrlInvokerMap = new LinkedHashMap<>(Math.round(1 + this.urlInvokerMap.size() / DEFAULT_HASHMAP_LOAD_FACTOR));
this.urlInvokerMap.forEach(oldUrlInvokerMap::put);
}
Map<URL, Invoker<T>> newUrlInvokerMap = toInvokers(oldUrlInvokerMap, invokerUrls);// Translate url list to Invoker map
Expand Down

0 comments on commit 8bd0479

Please sign in to comment.