[ISSUE #11782] Optimized the use of map (#11783)

* Optimized the use of map

* Optimized the use of map
This commit is contained in:
zhanghong 2024-03-01 10:05:33 +08:00 committed by GitHub
parent 961d56ccce
commit 8f1cb59370
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 29 additions and 71 deletions

View File

@ -61,11 +61,12 @@ public class GrpcProtocolAuthService extends AbstractProtocolAuthService<Request
return parseSpecifiedResource(secured);
}
String type = secured.signType();
if (!resourceParserMap.containsKey(type)) {
AbstractGrpcResourceParser parser = resourceParserMap.get(type);
if (parser == null) {
Loggers.AUTH.warn("Can't find Grpc request resourceParser for type {}", type);
return useSpecifiedParserToParse(secured, request);
}
return resourceParserMap.get(type).parse(request, secured);
return parser.parse(request, secured);
}
@Override

View File

@ -59,15 +59,7 @@ public final class CredentialService implements SpasCredentialLoader {
public static CredentialService getInstance(String appName) {
String key = appName != null ? appName : IdentifyConstants.NO_APP_NAME;
if (INSTANCES.get(key) == null) {
synchronized (CredentialService.INSTANCES) {
if (INSTANCES.get(key) == null) {
CredentialService instance = new CredentialService(appName);
INSTANCES.putIfAbsent(key, instance);
}
}
}
return INSTANCES.get(key);
return INSTANCES.computeIfAbsent(key, k -> new CredentialService(appName));
}
public static CredentialService freeInstance() {

View File

@ -54,10 +54,7 @@ public class NacosServiceLoader {
}
private static <T> void cacheServiceClass(final Class<T> 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());
}
/**

View File

@ -80,8 +80,7 @@ public class TraceEventPublisher extends Thread implements ShardedEventPublisher
@Override
public void addSubscriber(Subscriber subscriber, Class<? extends Event> subscribeType) {
subscribes.computeIfAbsent(subscribeType, inputType -> new ConcurrentHashSet<>());
subscribes.get(subscribeType).add(subscriber);
subscribes.computeIfAbsent(subscribeType, inputType -> new ConcurrentHashSet<>()).add(subscriber);
}
@Override

View File

@ -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() {

View File

@ -54,21 +54,9 @@ public class ConfigChangeListenContext {
*/
public synchronized void addListen(String groupKey, String md5, String connectionId) {
// 1.add groupKeyContext
Set<String> 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<String, String> 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);
}
/**

View File

@ -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);
}

View File

@ -68,15 +68,13 @@ public interface Serializer {
*/
default <T> 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;

View File

@ -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() {

View File

@ -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);

View File

@ -51,8 +51,7 @@ public final class PersistentNotifier extends Subscriber<ValueChangeEvent> {
* @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);
}
/**

View File

@ -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;
}

View File

@ -77,8 +77,7 @@ public class NamingEventPublisher extends Thread implements ShardedEventPublishe
@Override
public void addSubscriber(Subscriber subscriber, Class<? extends Event> subscribeType) {
subscribes.computeIfAbsent(subscribeType, inputType -> new ConcurrentHashSet<>());
subscribes.get(subscribeType).add(subscriber);
subscribes.computeIfAbsent(subscribeType, inputType -> new ConcurrentHashSet<>()).add(subscriber);
}
@Override

View File

@ -51,12 +51,11 @@ public class NamingEventPublisherFactory implements EventPublisherFactory {
// Like ClientEvent$ClientChangeEvent cache by ClientEvent
Class<? extends Event> cachedEventType =
eventType.isMemberClass() ? (Class<? extends Event>) 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() {

View File

@ -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<String> 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<String> 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);
}
}

View File

@ -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);
}
/**