Browse Source

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
pull/6059/head
Andy Wilkinson 10 years ago
parent
commit
91df749839
  1. 8
      spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcHypermediaManagementContextConfiguration.java
  2. 2
      spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/AbstractJmxCacheStatisticsProvider.java
  3. 5
      spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/BufferGaugeService.java
  4. 12
      spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/jmx/JmxMetricWriter.java
  5. 13
      spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/util/SimpleInMemoryRepository.java
  6. 5
      spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/writer/DefaultGaugeService.java

8
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcHypermediaManagementContextConfiguration.java

@ -223,7 +223,7 @@ public class EndpointWebMvcHypermediaManagementContextConfiguration { @@ -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 { @@ -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

2
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/AbstractJmxCacheStatisticsProvider.java vendored

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

5
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/BufferGaugeService.java

@ -48,8 +48,9 @@ public class BufferGaugeService implements GaugeService { @@ -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")) {

12
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/jmx/JmxMetricWriter.java

@ -109,9 +109,13 @@ public class JmxMetricWriter implements MetricWriter { @@ -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 { @@ -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)

13
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/util/SimpleInMemoryRepository.java

@ -59,13 +59,7 @@ public class SimpleInMemoryRepository<T> { @@ -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> { @@ -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() {

5
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/writer/DefaultGaugeService.java

@ -46,8 +46,9 @@ public class DefaultGaugeService implements GaugeService { @@ -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")) {

Loading…
Cancel
Save