Skip to content

Commit 91df749

Browse files
committed
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
1 parent 6bd7a2f commit 91df749

File tree

6 files changed

+22
-23
lines changed

6 files changed

+22
-23
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public static class MvcEndpointAdvice implements ResponseBodyAdvice<Object> {
223223
@Autowired
224224
private List<RequestMappingHandlerAdapter> handlerAdapters;
225225

226-
private Map<MediaType, HttpMessageConverter<?>> converterCache = new ConcurrentHashMap<MediaType, HttpMessageConverter<?>>();
226+
private final Map<MediaType, HttpMessageConverter<?>> converterCache = new ConcurrentHashMap<MediaType, HttpMessageConverter<?>>();
227227

228228
@Override
229229
public boolean supports(MethodParameter returnType,
@@ -278,8 +278,10 @@ private Object beforeBodyWrite(Object body, MethodParameter returnType,
278278
private HttpMessageConverter<Object> findConverter(
279279
Class<? extends HttpMessageConverter<?>> selectedConverterType,
280280
MediaType mediaType) {
281-
if (this.converterCache.containsKey(mediaType)) {
282-
return (HttpMessageConverter<Object>) this.converterCache.get(mediaType);
281+
HttpMessageConverter<Object> cached = (HttpMessageConverter<Object>) this.converterCache
282+
.get(mediaType);
283+
if (cached != null) {
284+
return cached;
283285
}
284286
for (RequestMappingHandlerAdapter handlerAdapter : this.handlerAdapters) {
285287
for (HttpMessageConverter<?> converter : handlerAdapter

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public abstract class AbstractJmxCacheStatisticsProvider<C extends Cache>
5050

5151
private MBeanServer mBeanServer;
5252

53-
private Map<String, ObjectNameWrapper> caches = new ConcurrentHashMap<String, ObjectNameWrapper>();
53+
private final Map<String, ObjectNameWrapper> caches = new ConcurrentHashMap<String, ObjectNameWrapper>();
5454

5555
@Override
5656
public CacheStatistics getCacheStatistics(CacheManager cacheManager, C cache) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ public void submit(String metricName, double value) {
4848
}
4949

5050
private String wrap(String metricName) {
51-
if (this.names.containsKey(metricName)) {
52-
return this.names.get(metricName);
51+
String cached = this.names.get(metricName);
52+
if (cached != null) {
53+
return cached;
5354
}
5455
if (metricName.startsWith("gauge") || metricName.startsWith("histogram")
5556
|| metricName.startsWith("timer")) {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,21 @@ public void reset(String name) {
109109
}
110110

111111
private MetricValue getValue(String name) {
112-
if (!this.values.containsKey(name)) {
113-
this.values.putIfAbsent(name, new MetricValue());
114-
MetricValue value = this.values.get(name);
112+
MetricValue value = this.values.get(name);
113+
if (value == null) {
114+
value = new MetricValue();
115+
MetricValue oldValue = this.values.putIfAbsent(name, value);
116+
if (oldValue != null) {
117+
value = oldValue;
118+
}
115119
try {
116120
this.exporter.registerManagedResource(value, getName(name, value));
117121
}
118122
catch (Exception ex) {
119123
// Could not register mbean, maybe just a race condition
120124
}
121125
}
122-
return this.values.get(name);
126+
return value;
123127
}
124128

125129
private ObjectName getName(String name, MetricValue value)

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,7 @@ private Object getLock(String name) {
5959
}
6060

6161
public void set(String name, T value) {
62-
T current = this.values.get(name);
63-
if (current != null) {
64-
this.values.replace(name, current, value);
65-
}
66-
else {
67-
this.values.putIfAbsent(name, value);
68-
}
62+
this.values.put(name, value);
6963
}
7064

7165
public long count() {
@@ -77,10 +71,7 @@ public void remove(String name) {
7771
}
7872

7973
public T findOne(String name) {
80-
if (this.values.containsKey(name)) {
81-
return this.values.get(name);
82-
}
83-
return null;
74+
return this.values.get(name);
8475
}
8576

8677
public Iterable<T> findAll() {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ public void submit(String metricName, double value) {
4646
}
4747

4848
private String wrap(String metricName) {
49-
if (this.names.containsKey(metricName)) {
50-
return this.names.get(metricName);
49+
String cached = this.names.get(metricName);
50+
if (cached != null) {
51+
return cached;
5152
}
5253
if (metricName.startsWith("gauge") || metricName.startsWith("histogram")
5354
|| metricName.startsWith("timer")) {

0 commit comments

Comments
 (0)