Improves metrics performance by not guarding map.get

ConcurrentHashMap implements `containsKey` with `get`. By removing a
redundant call to `containsKey`, we guarantee better performance in our
counter services.

The geek inside measured this with JMH, and found under 4 threads of
contention, throughput on this check was 40% higher in success case.

Benchmark                                  Mode  Cnt     Score     Error   Units
TestBenchmarks.containsKeyAndGet_success  thrpt   30   432.389 ±  20.616  ops/us
TestBenchmarks.get_success                thrpt   30   606.789 ±  10.848  ops/us

Closes gh-6379
pull/6059/head
Adrian Cole 8 years ago committed by Andy Wilkinson
parent 0792d43a93
commit 38e3b39d3b

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -58,8 +58,9 @@ public class BufferCounterService implements CounterService {
} }
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("counter") || metricName.startsWith("meter")) { if (metricName.startsWith("counter") || metricName.startsWith("meter")) {
return metricName; return metricName;

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -128,8 +128,9 @@ public class DropwizardMetricServices implements CounterService, GaugeService {
} }
private String wrapName(String metricName, String prefix) { private String wrapName(String metricName, String prefix) {
if (this.names.containsKey(metricName)) { String cached = this.names.get(metricName);
return this.names.get(metricName); if (cached != null) {
return cached;
} }
if (metricName.startsWith(prefix)) { if (metricName.startsWith(prefix)) {
return metricName; return metricName;

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -55,8 +55,9 @@ public class DefaultCounterService implements CounterService {
} }
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("counter.") || metricName.startsWith("meter.")) { if (metricName.startsWith("counter.") || metricName.startsWith("meter.")) {
return metricName; return metricName;

Loading…
Cancel
Save