From 38e3b39d3bac2d1e86dff4ecf4859995bd88af66 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Tue, 12 Jul 2016 16:27:29 +0800 Subject: [PATCH] Improves metrics performance by not guarding map.get MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../boot/actuate/metrics/buffer/BufferCounterService.java | 7 ++++--- .../metrics/dropwizard/DropwizardMetricServices.java | 7 ++++--- .../boot/actuate/metrics/writer/DefaultCounterService.java | 7 ++++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterService.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterService.java index c68ca3d97af..0717b3df2b4 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterService.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterService.java @@ -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"); * 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) { - if (this.names.containsKey(metricName)) { - return this.names.get(metricName); + String cached = this.names.get(metricName); + if (cached != null) { + return cached; } if (metricName.startsWith("counter") || metricName.startsWith("meter")) { return metricName; diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/dropwizard/DropwizardMetricServices.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/dropwizard/DropwizardMetricServices.java index b011dfa5b8a..7e4f6c92cc3 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/dropwizard/DropwizardMetricServices.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/dropwizard/DropwizardMetricServices.java @@ -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"); * 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) { - if (this.names.containsKey(metricName)) { - return this.names.get(metricName); + String cached = this.names.get(metricName); + if (cached != null) { + return cached; } if (metricName.startsWith(prefix)) { return metricName; diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/writer/DefaultCounterService.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/writer/DefaultCounterService.java index bd41b647d00..20b8183fac2 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/writer/DefaultCounterService.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/writer/DefaultCounterService.java @@ -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"); * 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) { - if (this.names.containsKey(metricName)) { - return this.names.get(metricName); + String cached = this.names.get(metricName); + if (cached != null) { + return cached; } if (metricName.startsWith("counter.") || metricName.startsWith("meter.")) { return metricName;