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:
Andy Wilkinson
2016-07-13 09:26:14 +01:00
parent 6bd7a2fedd
commit 91df749839
6 changed files with 22 additions and 23 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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