From 0fb75c164fa1ab8a5b5fc5a7308719e87b2cd48b Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Mon, 11 Dec 2023 20:04:00 +0900 Subject: [PATCH] Use cache for base time unit strings (#4487) * Use cache for base time unit strings Closes gh-4352 * Polish "Use cache for base time unit strings" --------- Co-authored-by: Jonatan Ivanov --- .../micrometer/core/instrument/MeterRegistry.java | 15 ++++++++++++++- .../core/instrument/MeterRegistryTest.java | 9 +++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java index d016131cca..8dd22eef63 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java @@ -36,6 +36,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.*; +import java.util.stream.Collectors; import static java.lang.String.format; import static java.util.Collections.emptyList; @@ -59,6 +60,18 @@ */ public abstract class MeterRegistry { + // @formatter:off + private static final EnumMap BASE_TIME_UNIT_STRING_CACHE = Arrays.stream(TimeUnit.values()) + .collect( + Collectors.toMap( + Function.identity(), + (timeUnit) -> timeUnit.toString().toLowerCase(), + (k, v) -> { throw new IllegalStateException("Duplicate keys should not exist."); }, + () -> new EnumMap<>(TimeUnit.class) + ) + ); + // @formatter:on + protected final Clock clock; private final Object meterMapLock = new Object(); @@ -280,7 +293,7 @@ protected String getConventionName(Meter.Id id) { protected abstract DistributionStatisticConfig defaultHistogramConfig(); private String getBaseTimeUnitStr() { - return getBaseTimeUnit().toString().toLowerCase(); + return BASE_TIME_UNIT_STRING_CACHE.get(getBaseTimeUnit()); } /** diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/MeterRegistryTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/MeterRegistryTest.java index b4d094ea66..0940a64a91 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/MeterRegistryTest.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/MeterRegistryTest.java @@ -15,6 +15,7 @@ */ package io.micrometer.core.instrument; +import io.micrometer.core.Issue; import io.micrometer.core.instrument.config.MeterFilter; import io.micrometer.core.instrument.config.MeterFilterReply; import io.micrometer.core.instrument.distribution.DistributionStatisticConfig; @@ -205,4 +206,12 @@ void shouldNotLetRegisteringMetersTwice() { .hasNoCause(); } + @Test + @Issue("#4352") + void baseUnitStringShouldBeCachedAndReturnTheSameInstance() { + Timer timer1 = registry.timer("test.timer1"); + Timer timer2 = registry.timer("test.timer2"); + assertThat(timer1.getId().getBaseUnit()).isSameAs(timer2.getId().getBaseUnit()); + } + }