Skip to content

Commit

Permalink
optimize putIfAbsent method to computeIfAbsent which may reduce some …
Browse files Browse the repository at this point in the history
…creation of object. (#7543)

Co-authored-by: lihaoyue002 <lihaoyue002@ke.com>
  • Loading branch information
likemoongg and lihaoyue002 authored Apr 15, 2021
1 parent 884c2ab commit a666294
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static <T> ExtensionLoader<T> getExtensionLoader(Class<T> type) {

ExtensionLoader<T> loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type);
if (loader == null) {
EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader<T>(type));
EXTENSION_LOADERS.computeIfAbsent(type, ExtensionLoader::new);
loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type);
}
return loader;
Expand Down Expand Up @@ -371,7 +371,7 @@ public T getLoadedExtension(String name) {
private Holder<Object> getOrCreateHolder(String name) {
Holder<Object> holder = cachedInstances.get(name);
if (holder == null) {
cachedInstances.putIfAbsent(name, new Holder<>());
cachedInstances.computeIfAbsent(name, k -> new Holder<>());
holder = cachedInstances.get(name);
}
return holder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ public void registerConsumer(String serviceKey,
ReferenceConfigBase<?> rc,
Object proxy,
ServiceMetadata serviceMetadata) {
ConsumerModel consumerModel = new ConsumerModel(serviceMetadata.getServiceKey(), proxy, serviceDescriptor, rc,
serviceMetadata);
consumers.putIfAbsent(serviceKey, consumerModel);
consumers.computeIfAbsent(serviceKey, k -> new ConsumerModel(serviceMetadata.getServiceKey(), proxy, serviceDescriptor, rc,
serviceMetadata));
}

public void reRegisterConsumer(String newServiceKey, String serviceKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public GroupServiceKeyCache(String serviceGroup) {
public String getServiceKey(String serviceName, String serviceVersion, int port) {
ConcurrentMap<String, ConcurrentMap<Integer, String>> versionMap = serviceKeyMap.get(serviceName);
if (versionMap == null) {
serviceKeyMap.putIfAbsent(serviceName, new ConcurrentHashMap<>());
serviceKeyMap.computeIfAbsent(serviceName, k-> new ConcurrentHashMap<>());
versionMap = serviceKeyMap.get(serviceName);
}

serviceVersion = serviceVersion == null ? "" : serviceVersion;
ConcurrentMap<Integer, String> portMap = versionMap.get(serviceVersion);
if (portMap == null) {
versionMap.putIfAbsent(serviceVersion, new ConcurrentHashMap<>());
versionMap.computeIfAbsent(serviceVersion, k-> new ConcurrentHashMap<>());
portMap = versionMap.get(serviceVersion);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static String serviceKey(int port, String serviceName, String serviceVers
serviceGroup = serviceGroup == null ? "" : serviceGroup;
GroupServiceKeyCache groupServiceKeyCache = groupServiceKeyCacheMap.get(serviceGroup);
if (groupServiceKeyCache == null) {
groupServiceKeyCacheMap.putIfAbsent(serviceGroup, new GroupServiceKeyCache(serviceGroup));
groupServiceKeyCacheMap.computeIfAbsent(serviceGroup, GroupServiceKeyCache::new);
groupServiceKeyCache = groupServiceKeyCacheMap.get(serviceGroup);
}
return groupServiceKeyCache.getServiceKey(serviceName, serviceVersion, port);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Set<String> getServiceAppMapping(String serviceKey, MappingListener liste

@Override
public void registerServiceAppMapping(String serviceKey, String application, URL url) {
appMapping.putIfAbsent(serviceKey, new ConcurrentHashSet<>());
appMapping.computeIfAbsent(serviceKey, k -> new ConcurrentHashSet<>());
Set<String> appNames = appMapping.get(serviceKey);
appNames.add(application);
}
Expand All @@ -96,7 +96,7 @@ public List<String> getExportedURLs(ServiceMetadataIdentifier metadataIdentifier

@Override
public void saveServiceMetadata(ServiceMetadataIdentifier metadataIdentifier, URL url) {
serviceMetadata.putIfAbsent(metadataIdentifier, new CopyOnWriteArrayList<>());
serviceMetadata.computeIfAbsent(metadataIdentifier, k -> new CopyOnWriteArrayList<>());
List<String> urls = serviceMetadata.get(metadataIdentifier);
urls.add(url.toFullString());
}
Expand All @@ -108,7 +108,7 @@ public void removeServiceMetadata(ServiceMetadataIdentifier metadataIdentifier)

@Override
public void saveSubscribedData(SubscriberMetadataIdentifier subscriberMetadataIdentifier, Set<String> urls) {
subscribeMetadata.putIfAbsent(subscriberMetadataIdentifier, new CopyOnWriteArraySet());
subscribeMetadata.computeIfAbsent(subscriberMetadataIdentifier, k -> new CopyOnWriteArraySet());
Set<String> metadataUrls = subscribeMetadata.get(subscriberMetadataIdentifier);
metadataUrls.addAll(urls);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private Map<String, Set<String>> parseMetadata(String revision, MetadataInfo met
private MetadataInfo getMetadataInfo(ServiceInstance instance) {
String metadataType = ServiceInstanceMetadataUtils.getMetadataStorageType(instance);
// FIXME, check "REGISTRY_CLUSTER_KEY" must be set by every registry implementation.
instance.getExtendParams().putIfAbsent(REGISTRY_CLUSTER_KEY, RegistryClusterIdentifier.getExtension(url).consumerKey(url));
instance.getExtendParams().computeIfAbsent(REGISTRY_CLUSTER_KEY, k -> RegistryClusterIdentifier.getExtension(url).consumerKey(url));
MetadataInfo metadataInfo;
try {
if (logger.isDebugEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,11 @@ public void doSubscribe(final URL url, final NotifyListener listener) {
String service = toServicePath(url);
Notifier notifier = notifiers.get(service);
if (notifier == null) {
Notifier newNotifier = new Notifier(service);
notifiers.putIfAbsent(service, newNotifier);
notifier = notifiers.get(service);
if (notifier == newNotifier) {
notifier.start();
}
notifiers.computeIfAbsent(service, k -> {
Notifier notifier1 = new Notifier(k);
notifier1.start();
return notifier1;
});
}
try {
if (service.endsWith(ANY_VALUE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public boolean isAllowable(URL url, Invocation invocation) {
if (rate > 0) {
StatItem statItem = stats.get(serviceKey);
if (statItem == null) {
stats.putIfAbsent(serviceKey, new StatItem(serviceKey, rate, interval));
stats.computeIfAbsent(serviceKey, k -> new StatItem(k, rate, interval));
statItem = stats.get(serviceKey);
} else {
//rate or interval has changed, rebuild
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ private Object decode(TProtocol protocol)
Request request = new Request(id);
request.setData(result);

CACHED_REQUEST.putIfAbsent(id,
CACHED_REQUEST.computeIfAbsent(id, k ->
RequestData.create(message.seqid, serviceName, message.name));

return request;
Expand Down

0 comments on commit a666294

Please sign in to comment.