Improve usage of ConcurrentMap
- Call get rather than containsKey then get - Only call putIfAbsent after get has returned null to avoid unnecessary object creation Closes gh-6382
This commit is contained in:
@@ -223,7 +223,7 @@ public class EndpointWebMvcHypermediaManagementContextConfiguration {
|
||||
@Autowired
|
||||
private List<RequestMappingHandlerAdapter> handlerAdapters;
|
||||
|
||||
private Map<MediaType, HttpMessageConverter<?>> converterCache = new ConcurrentHashMap<MediaType, HttpMessageConverter<?>>();
|
||||
private final Map<MediaType, HttpMessageConverter<?>> converterCache = new ConcurrentHashMap<MediaType, HttpMessageConverter<?>>();
|
||||
|
||||
@Override
|
||||
public boolean supports(MethodParameter returnType,
|
||||
@@ -278,8 +278,10 @@ public class EndpointWebMvcHypermediaManagementContextConfiguration {
|
||||
private HttpMessageConverter<Object> findConverter(
|
||||
Class<? extends HttpMessageConverter<?>> selectedConverterType,
|
||||
MediaType mediaType) {
|
||||
if (this.converterCache.containsKey(mediaType)) {
|
||||
return (HttpMessageConverter<Object>) this.converterCache.get(mediaType);
|
||||
HttpMessageConverter<Object> cached = (HttpMessageConverter<Object>) this.converterCache
|
||||
.get(mediaType);
|
||||
if (cached != null) {
|
||||
return cached;
|
||||
}
|
||||
for (RequestMappingHandlerAdapter handlerAdapter : this.handlerAdapters) {
|
||||
for (HttpMessageConverter<?> converter : handlerAdapter
|
||||
|
||||
@@ -50,7 +50,7 @@ public abstract class AbstractJmxCacheStatisticsProvider<C extends Cache>
|
||||
|
||||
private MBeanServer mBeanServer;
|
||||
|
||||
private Map<String, ObjectNameWrapper> caches = new ConcurrentHashMap<String, ObjectNameWrapper>();
|
||||
private final Map<String, ObjectNameWrapper> caches = new ConcurrentHashMap<String, ObjectNameWrapper>();
|
||||
|
||||
@Override
|
||||
public CacheStatistics getCacheStatistics(CacheManager cacheManager, C cache) {
|
||||
|
||||
@@ -48,8 +48,9 @@ public class BufferGaugeService implements GaugeService {
|
||||
}
|
||||
|
||||
private String wrap(String metricName) {
|
||||
if (this.names.containsKey(metricName)) {
|
||||
return this.names.get(metricName);
|
||||
String cached = this.names.get(metricName);
|
||||
if (cached != null) {
|
||||
return cached;
|
||||
}
|
||||
if (metricName.startsWith("gauge") || metricName.startsWith("histogram")
|
||||
|| metricName.startsWith("timer")) {
|
||||
|
||||
@@ -109,9 +109,13 @@ public class JmxMetricWriter implements MetricWriter {
|
||||
}
|
||||
|
||||
private MetricValue getValue(String name) {
|
||||
if (!this.values.containsKey(name)) {
|
||||
this.values.putIfAbsent(name, new MetricValue());
|
||||
MetricValue value = this.values.get(name);
|
||||
MetricValue value = this.values.get(name);
|
||||
if (value == null) {
|
||||
value = new MetricValue();
|
||||
MetricValue oldValue = this.values.putIfAbsent(name, value);
|
||||
if (oldValue != null) {
|
||||
value = oldValue;
|
||||
}
|
||||
try {
|
||||
this.exporter.registerManagedResource(value, getName(name, value));
|
||||
}
|
||||
@@ -119,7 +123,7 @@ public class JmxMetricWriter implements MetricWriter {
|
||||
// Could not register mbean, maybe just a race condition
|
||||
}
|
||||
}
|
||||
return this.values.get(name);
|
||||
return value;
|
||||
}
|
||||
|
||||
private ObjectName getName(String name, MetricValue value)
|
||||
|
||||
@@ -59,13 +59,7 @@ public class SimpleInMemoryRepository<T> {
|
||||
}
|
||||
|
||||
public void set(String name, T value) {
|
||||
T current = this.values.get(name);
|
||||
if (current != null) {
|
||||
this.values.replace(name, current, value);
|
||||
}
|
||||
else {
|
||||
this.values.putIfAbsent(name, value);
|
||||
}
|
||||
this.values.put(name, value);
|
||||
}
|
||||
|
||||
public long count() {
|
||||
@@ -77,10 +71,7 @@ public class SimpleInMemoryRepository<T> {
|
||||
}
|
||||
|
||||
public T findOne(String name) {
|
||||
if (this.values.containsKey(name)) {
|
||||
return this.values.get(name);
|
||||
}
|
||||
return null;
|
||||
return this.values.get(name);
|
||||
}
|
||||
|
||||
public Iterable<T> findAll() {
|
||||
|
||||
@@ -46,8 +46,9 @@ public class DefaultGaugeService implements GaugeService {
|
||||
}
|
||||
|
||||
private String wrap(String metricName) {
|
||||
if (this.names.containsKey(metricName)) {
|
||||
return this.names.get(metricName);
|
||||
String cached = this.names.get(metricName);
|
||||
if (cached != null) {
|
||||
return cached;
|
||||
}
|
||||
if (metricName.startsWith("gauge") || metricName.startsWith("histogram")
|
||||
|| metricName.startsWith("timer")) {
|
||||
|
||||
Reference in New Issue
Block a user