Commit 91df7498 authored by Andy Wilkinson's avatar Andy Wilkinson

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
parent 6bd7a2fe
...@@ -223,7 +223,7 @@ public class EndpointWebMvcHypermediaManagementContextConfiguration { ...@@ -223,7 +223,7 @@ public class EndpointWebMvcHypermediaManagementContextConfiguration {
@Autowired @Autowired
private List<RequestMappingHandlerAdapter> handlerAdapters; private List<RequestMappingHandlerAdapter> handlerAdapters;
private Map<MediaType, HttpMessageConverter<?>> converterCache = new ConcurrentHashMap<MediaType, HttpMessageConverter<?>>(); private final Map<MediaType, HttpMessageConverter<?>> converterCache = new ConcurrentHashMap<MediaType, HttpMessageConverter<?>>();
@Override @Override
public boolean supports(MethodParameter returnType, public boolean supports(MethodParameter returnType,
...@@ -278,8 +278,10 @@ public class EndpointWebMvcHypermediaManagementContextConfiguration { ...@@ -278,8 +278,10 @@ public class EndpointWebMvcHypermediaManagementContextConfiguration {
private HttpMessageConverter<Object> findConverter( private HttpMessageConverter<Object> findConverter(
Class<? extends HttpMessageConverter<?>> selectedConverterType, Class<? extends HttpMessageConverter<?>> selectedConverterType,
MediaType mediaType) { MediaType mediaType) {
if (this.converterCache.containsKey(mediaType)) { HttpMessageConverter<Object> cached = (HttpMessageConverter<Object>) this.converterCache
return (HttpMessageConverter<Object>) this.converterCache.get(mediaType); .get(mediaType);
if (cached != null) {
return cached;
} }
for (RequestMappingHandlerAdapter handlerAdapter : this.handlerAdapters) { for (RequestMappingHandlerAdapter handlerAdapter : this.handlerAdapters) {
for (HttpMessageConverter<?> converter : handlerAdapter for (HttpMessageConverter<?> converter : handlerAdapter
......
...@@ -50,7 +50,7 @@ public abstract class AbstractJmxCacheStatisticsProvider<C extends Cache> ...@@ -50,7 +50,7 @@ public abstract class AbstractJmxCacheStatisticsProvider<C extends Cache>
private MBeanServer mBeanServer; private MBeanServer mBeanServer;
private Map<String, ObjectNameWrapper> caches = new ConcurrentHashMap<String, ObjectNameWrapper>(); private final Map<String, ObjectNameWrapper> caches = new ConcurrentHashMap<String, ObjectNameWrapper>();
@Override @Override
public CacheStatistics getCacheStatistics(CacheManager cacheManager, C cache) { public CacheStatistics getCacheStatistics(CacheManager cacheManager, C cache) {
......
...@@ -48,8 +48,9 @@ public class BufferGaugeService implements GaugeService { ...@@ -48,8 +48,9 @@ public class BufferGaugeService implements GaugeService {
} }
private String wrap(String metricName) { private String wrap(String metricName) {
if (this.names.containsKey(metricName)) { String cached = this.names.get(metricName);
return this.names.get(metricName); if (cached != null) {
return cached;
} }
if (metricName.startsWith("gauge") || metricName.startsWith("histogram") if (metricName.startsWith("gauge") || metricName.startsWith("histogram")
|| metricName.startsWith("timer")) { || metricName.startsWith("timer")) {
......
...@@ -109,9 +109,13 @@ public class JmxMetricWriter implements MetricWriter { ...@@ -109,9 +109,13 @@ public class JmxMetricWriter implements MetricWriter {
} }
private MetricValue getValue(String name) { private MetricValue getValue(String name) {
if (!this.values.containsKey(name)) { MetricValue value = this.values.get(name);
this.values.putIfAbsent(name, new MetricValue()); if (value == null) {
MetricValue value = this.values.get(name); value = new MetricValue();
MetricValue oldValue = this.values.putIfAbsent(name, value);
if (oldValue != null) {
value = oldValue;
}
try { try {
this.exporter.registerManagedResource(value, getName(name, value)); this.exporter.registerManagedResource(value, getName(name, value));
} }
...@@ -119,7 +123,7 @@ public class JmxMetricWriter implements MetricWriter { ...@@ -119,7 +123,7 @@ public class JmxMetricWriter implements MetricWriter {
// Could not register mbean, maybe just a race condition // Could not register mbean, maybe just a race condition
} }
} }
return this.values.get(name); return value;
} }
private ObjectName getName(String name, MetricValue value) private ObjectName getName(String name, MetricValue value)
......
...@@ -59,13 +59,7 @@ public class SimpleInMemoryRepository<T> { ...@@ -59,13 +59,7 @@ public class SimpleInMemoryRepository<T> {
} }
public void set(String name, T value) { public void set(String name, T value) {
T current = this.values.get(name); this.values.put(name, value);
if (current != null) {
this.values.replace(name, current, value);
}
else {
this.values.putIfAbsent(name, value);
}
} }
public long count() { public long count() {
...@@ -77,10 +71,7 @@ public class SimpleInMemoryRepository<T> { ...@@ -77,10 +71,7 @@ public class SimpleInMemoryRepository<T> {
} }
public T findOne(String name) { public T findOne(String name) {
if (this.values.containsKey(name)) { return this.values.get(name);
return this.values.get(name);
}
return null;
} }
public Iterable<T> findAll() { public Iterable<T> findAll() {
......
...@@ -46,8 +46,9 @@ public class DefaultGaugeService implements GaugeService { ...@@ -46,8 +46,9 @@ public class DefaultGaugeService implements GaugeService {
} }
private String wrap(String metricName) { private String wrap(String metricName) {
if (this.names.containsKey(metricName)) { String cached = this.names.get(metricName);
return this.names.get(metricName); if (cached != null) {
return cached;
} }
if (metricName.startsWith("gauge") || metricName.startsWith("histogram") if (metricName.startsWith("gauge") || metricName.startsWith("histogram")
|| metricName.startsWith("timer")) { || metricName.startsWith("timer")) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment