From 8f1cb59370f489f7241208778a205429a3753eb8 Mon Sep 17 00:00:00 2001 From: zhanghong <985492783@qq.com> Date: Fri, 1 Mar 2024 10:05:33 +0800 Subject: [PATCH] [ISSUE #11782] Optimized the use of map (#11783) * Optimized the use of map * Optimized the use of map --- .../nacos/auth/GrpcProtocolAuthService.java | 5 +++-- .../auth/ram/identify/CredentialService.java | 10 +--------- .../nacos/common/spi/NacosServiceLoader.java | 5 +---- .../trace/publisher/TraceEventPublisher.java | 3 +-- .../publisher/TraceEventPublisherFactory.java | 3 +-- .../server/remote/ConfigChangeListenContext.java | 16 ++-------------- .../nacos/consistency/ProtocolMetaData.java | 12 ++++-------- .../alibaba/nacos/consistency/Serializer.java | 4 +--- .../distro/monitor/DistroRecordsHolder.java | 3 +-- .../nacos/core/remote/ConnectionManager.java | 5 +---- .../persistent/PersistentNotifier.java | 3 +-- .../nacos/naming/core/v2/ServiceManager.java | 6 ++---- .../v2/event/publisher/NamingEventPublisher.java | 3 +-- .../publisher/NamingEventPublisherFactory.java | 3 +-- .../v2/index/ClientServiceIndexesManager.java | 14 +++++++------- .../core/v2/metadata/NamingMetadataManager.java | 5 +---- 16 files changed, 29 insertions(+), 71 deletions(-) diff --git a/auth/src/main/java/com/alibaba/nacos/auth/GrpcProtocolAuthService.java b/auth/src/main/java/com/alibaba/nacos/auth/GrpcProtocolAuthService.java index 7e054d824..7b4c31979 100644 --- a/auth/src/main/java/com/alibaba/nacos/auth/GrpcProtocolAuthService.java +++ b/auth/src/main/java/com/alibaba/nacos/auth/GrpcProtocolAuthService.java @@ -61,11 +61,12 @@ public class GrpcProtocolAuthService extends AbstractProtocolAuthService new CredentialService(appName)); } public static CredentialService freeInstance() { diff --git a/common/src/main/java/com/alibaba/nacos/common/spi/NacosServiceLoader.java b/common/src/main/java/com/alibaba/nacos/common/spi/NacosServiceLoader.java index 315073365..cee339dc0 100644 --- a/common/src/main/java/com/alibaba/nacos/common/spi/NacosServiceLoader.java +++ b/common/src/main/java/com/alibaba/nacos/common/spi/NacosServiceLoader.java @@ -54,10 +54,7 @@ public class NacosServiceLoader { } private static void cacheServiceClass(final Class service, final T instance) { - if (!SERVICES.containsKey(service)) { - SERVICES.put(service, new LinkedHashSet<>()); - } - SERVICES.get(service).add(instance.getClass()); + SERVICES.computeIfAbsent(service, k -> new LinkedHashSet<>()).add(instance.getClass()); } /** diff --git a/common/src/main/java/com/alibaba/nacos/common/trace/publisher/TraceEventPublisher.java b/common/src/main/java/com/alibaba/nacos/common/trace/publisher/TraceEventPublisher.java index 7fc879f46..c0b297192 100644 --- a/common/src/main/java/com/alibaba/nacos/common/trace/publisher/TraceEventPublisher.java +++ b/common/src/main/java/com/alibaba/nacos/common/trace/publisher/TraceEventPublisher.java @@ -80,8 +80,7 @@ public class TraceEventPublisher extends Thread implements ShardedEventPublisher @Override public void addSubscriber(Subscriber subscriber, Class subscribeType) { - subscribes.computeIfAbsent(subscribeType, inputType -> new ConcurrentHashSet<>()); - subscribes.get(subscribeType).add(subscriber); + subscribes.computeIfAbsent(subscribeType, inputType -> new ConcurrentHashSet<>()).add(subscriber); } @Override diff --git a/common/src/main/java/com/alibaba/nacos/common/trace/publisher/TraceEventPublisherFactory.java b/common/src/main/java/com/alibaba/nacos/common/trace/publisher/TraceEventPublisherFactory.java index 36f46759c..e8ce321a4 100644 --- a/common/src/main/java/com/alibaba/nacos/common/trace/publisher/TraceEventPublisherFactory.java +++ b/common/src/main/java/com/alibaba/nacos/common/trace/publisher/TraceEventPublisherFactory.java @@ -59,12 +59,11 @@ public class TraceEventPublisherFactory implements EventPublisherFactory { } } - publisher.computeIfAbsent(cachedEventType, eventClass -> { + return publisher.computeIfAbsent(cachedEventType, eventClass -> { TraceEventPublisher result = new TraceEventPublisher(); result.init(eventClass, maxQueueSize); return result; }); - return publisher.get(cachedEventType); } public String getAllPublisherStatues() { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigChangeListenContext.java b/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigChangeListenContext.java index 53d0e2207..adf745355 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigChangeListenContext.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigChangeListenContext.java @@ -54,21 +54,9 @@ public class ConfigChangeListenContext { */ public synchronized void addListen(String groupKey, String md5, String connectionId) { // 1.add groupKeyContext - Set listenClients = groupKeyContext.get(groupKey); - if (listenClients == null) { - groupKeyContext.putIfAbsent(groupKey, new HashSet<>()); - listenClients = groupKeyContext.get(groupKey); - } - listenClients.add(connectionId); - + groupKeyContext.computeIfAbsent(groupKey, k -> new HashSet<>()).add(connectionId); // 2.add connectionIdContext - HashMap groupKeys = connectionIdContext.get(connectionId); - if (groupKeys == null) { - connectionIdContext.putIfAbsent(connectionId, new HashMap<>(16)); - groupKeys = connectionIdContext.get(connectionId); - } - groupKeys.put(groupKey, md5); - + connectionIdContext.computeIfAbsent(connectionId, k -> new HashMap<>(16)).put(groupKey, md5); } /** diff --git a/consistency/src/main/java/com/alibaba/nacos/consistency/ProtocolMetaData.java b/consistency/src/main/java/com/alibaba/nacos/consistency/ProtocolMetaData.java index d03fbcfe1..592c6085e 100644 --- a/consistency/src/main/java/com/alibaba/nacos/consistency/ProtocolMetaData.java +++ b/consistency/src/main/java/com/alibaba/nacos/consistency/ProtocolMetaData.java @@ -82,13 +82,11 @@ public final class ProtocolMetaData { * If MetaData does not exist, actively create a MetaData. */ public void subscribe(final String group, final String key, final Observer observer) { - metaDataMap.computeIfAbsent(group, s -> new MetaData(group)); - metaDataMap.get(group).subscribe(key, observer); + metaDataMap.computeIfAbsent(group, s -> new MetaData(group)).subscribe(key, observer); } public void unSubscribe(final String group, final String key, final Observer observer) { - metaDataMap.computeIfAbsent(group, s -> new MetaData(group)); - metaDataMap.get(group).unSubscribe(key, observer); + metaDataMap.computeIfAbsent(group, s -> new MetaData(group)).unSubscribe(key, observer); } @SuppressWarnings("PMD.ThreadPoolCreationRule") @@ -107,8 +105,7 @@ public final class ProtocolMetaData { } void put(String key, Object value) { - itemMap.computeIfAbsent(key, s -> new ValueItem(group + "/" + key)); - ValueItem item = itemMap.get(key); + ValueItem item = itemMap.computeIfAbsent(key, s -> new ValueItem(group + "/" + key)); item.setData(value); } @@ -119,8 +116,7 @@ public final class ProtocolMetaData { // If ValueItem does not exist, actively create a ValueItem void subscribe(final String key, final Observer observer) { - itemMap.computeIfAbsent(key, s -> new ValueItem(group + "/" + key)); - final ValueItem item = itemMap.get(key); + final ValueItem item = itemMap.computeIfAbsent(key, s -> new ValueItem(group + "/" + key)); item.addObserver(observer); } diff --git a/consistency/src/main/java/com/alibaba/nacos/consistency/Serializer.java b/consistency/src/main/java/com/alibaba/nacos/consistency/Serializer.java index 41a69d318..c7ca1c9e7 100644 --- a/consistency/src/main/java/com/alibaba/nacos/consistency/Serializer.java +++ b/consistency/src/main/java/com/alibaba/nacos/consistency/Serializer.java @@ -68,15 +68,13 @@ public interface Serializer { */ default T deserialize(byte[] data, String classFullName) { try { - Class cls; - CLASS_CACHE.computeIfAbsent(classFullName, name -> { + Class cls = CLASS_CACHE.computeIfAbsent(classFullName, name -> { try { return Class.forName(classFullName); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } }); - cls = CLASS_CACHE.get(classFullName); return (T) deserialize(data, cls); } catch (Exception ignore) { return null; diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/distro/monitor/DistroRecordsHolder.java b/core/src/main/java/com/alibaba/nacos/core/distributed/distro/monitor/DistroRecordsHolder.java index 42748f87c..abce1325c 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/distro/monitor/DistroRecordsHolder.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/distro/monitor/DistroRecordsHolder.java @@ -46,8 +46,7 @@ public class DistroRecordsHolder { } public DistroRecord getRecord(String type) { - distroRecords.computeIfAbsent(type, s -> new DistroRecord(type)); - return distroRecords.get(type); + return distroRecords.computeIfAbsent(type, s -> new DistroRecord(type)); } public long getTotalSyncCount() { diff --git a/core/src/main/java/com/alibaba/nacos/core/remote/ConnectionManager.java b/core/src/main/java/com/alibaba/nacos/core/remote/ConnectionManager.java index 6e7dc31af..5ad868d41 100644 --- a/core/src/main/java/com/alibaba/nacos/core/remote/ConnectionManager.java +++ b/core/src/main/java/com/alibaba/nacos/core/remote/ConnectionManager.java @@ -113,10 +113,7 @@ public class ConnectionManager { connection.setTraced(true); } connections.put(connectionId, connection); - if (!connectionForClientIp.containsKey(clientIp)) { - connectionForClientIp.put(clientIp, new AtomicInteger(0)); - } - connectionForClientIp.get(clientIp).getAndIncrement(); + connectionForClientIp.computeIfAbsent(clientIp, k -> new AtomicInteger(0)).getAndIncrement(); clientConnectionEventListenerRegistry.notifyClientConnected(connection); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/PersistentNotifier.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/PersistentNotifier.java index 15750b314..1356c53af 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/PersistentNotifier.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/PersistentNotifier.java @@ -51,8 +51,7 @@ public final class PersistentNotifier extends Subscriber { * @param listener {@link RecordListener} */ public void registerListener(final String key, final RecordListener listener) { - listenerMap.computeIfAbsent(key, s -> new ConcurrentHashSet<>()); - listenerMap.get(key).add(listener); + listenerMap.computeIfAbsent(key, s -> new ConcurrentHashSet<>()).add(listener); } /** diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/ServiceManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/ServiceManager.java index aaa8305dc..29f95a9fa 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/ServiceManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/ServiceManager.java @@ -59,13 +59,11 @@ public class ServiceManager { * @return if service is exist, return exist service, otherwise return new service */ public Service getSingleton(Service service) { - singletonRepository.computeIfAbsent(service, key -> { + Service result = singletonRepository.computeIfAbsent(service, key -> { NotifyCenter.publishEvent(new MetadataEvent.ServiceMetadataEvent(service, false)); return service; }); - Service result = singletonRepository.get(service); - namespaceSingletonMaps.computeIfAbsent(result.getNamespace(), namespace -> new ConcurrentHashSet<>()); - namespaceSingletonMaps.get(result.getNamespace()).add(result); + namespaceSingletonMaps.computeIfAbsent(result.getNamespace(), namespace -> new ConcurrentHashSet<>()).add(result); return result; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/publisher/NamingEventPublisher.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/publisher/NamingEventPublisher.java index ca43a9158..e8a1b5699 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/publisher/NamingEventPublisher.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/publisher/NamingEventPublisher.java @@ -77,8 +77,7 @@ public class NamingEventPublisher extends Thread implements ShardedEventPublishe @Override public void addSubscriber(Subscriber subscriber, Class subscribeType) { - subscribes.computeIfAbsent(subscribeType, inputType -> new ConcurrentHashSet<>()); - subscribes.get(subscribeType).add(subscriber); + subscribes.computeIfAbsent(subscribeType, inputType -> new ConcurrentHashSet<>()).add(subscriber); } @Override diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/publisher/NamingEventPublisherFactory.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/publisher/NamingEventPublisherFactory.java index 3a9d30c62..989da4543 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/publisher/NamingEventPublisherFactory.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/publisher/NamingEventPublisherFactory.java @@ -51,12 +51,11 @@ public class NamingEventPublisherFactory implements EventPublisherFactory { // Like ClientEvent$ClientChangeEvent cache by ClientEvent Class cachedEventType = eventType.isMemberClass() ? (Class) eventType.getEnclosingClass() : eventType; - publisher.computeIfAbsent(cachedEventType, eventClass -> { + return publisher.computeIfAbsent(cachedEventType, eventClass -> { NamingEventPublisher result = new NamingEventPublisher(); result.init(eventClass, maxQueueSize); return result; }); - return publisher.get(cachedEventType); } public String getAllPublisherStatues() { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/index/ClientServiceIndexesManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/index/ClientServiceIndexesManager.java index d80824f59..48ea5611a 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/index/ClientServiceIndexesManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/index/ClientServiceIndexesManager.java @@ -128,8 +128,7 @@ public class ClientServiceIndexesManager extends SmartSubscriber { } private void addPublisherIndexes(Service service, String clientId) { - publisherIndexes.computeIfAbsent(service, key -> new ConcurrentHashSet<>()); - publisherIndexes.get(service).add(clientId); + publisherIndexes.computeIfAbsent(service, key -> new ConcurrentHashSet<>()).add(clientId); NotifyCenter.publishEvent(new ServiceEvent.ServiceChangedEvent(service, true)); } @@ -142,19 +141,20 @@ public class ClientServiceIndexesManager extends SmartSubscriber { } private void addSubscriberIndexes(Service service, String clientId) { - subscriberIndexes.computeIfAbsent(service, key -> new ConcurrentHashSet<>()); + Set clientIds = subscriberIndexes.computeIfAbsent(service, key -> new ConcurrentHashSet<>()); // Fix #5404, Only first time add need notify event. - if (subscriberIndexes.get(service).add(clientId)) { + if (clientIds.add(clientId)) { NotifyCenter.publishEvent(new ServiceEvent.ServiceSubscribedEvent(service, clientId)); } } private void removeSubscriberIndexes(Service service, String clientId) { - if (!subscriberIndexes.containsKey(service)) { + Set clientIds = subscriberIndexes.get(service); + if (clientIds == null) { return; } - subscriberIndexes.get(service).remove(clientId); - if (subscriberIndexes.get(service).isEmpty()) { + clientIds.remove(clientId); + if (clientIds.isEmpty()) { subscriberIndexes.remove(service); } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/NamingMetadataManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/NamingMetadataManager.java index 47f4657a6..5d0957087 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/NamingMetadataManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/NamingMetadataManager.java @@ -127,10 +127,7 @@ public class NamingMetadataManager extends SmartSubscriber { * @param instanceMetadata new instance metadata */ public void updateInstanceMetadata(Service service, String metadataId, InstanceMetadata instanceMetadata) { - if (!instanceMetadataMap.containsKey(service)) { - instanceMetadataMap.putIfAbsent(service, new ConcurrentHashMap<>(INITIAL_CAPACITY)); - } - instanceMetadataMap.get(service).put(metadataId, instanceMetadata); + instanceMetadataMap.computeIfAbsent(service, k -> new ConcurrentHashMap<>(INITIAL_CAPACITY)).put(metadataId, instanceMetadata); } /**