Skip to content

Commit 1e39d3f

Browse files
committed
Use IdentityHashMap for consistent JIT optimization in AbstractCompositeMeter
Replace Collections.emptyMap() with new IdentityHashMap<>() to maintain consistent map implementation type throughout the object's lifecycle, enabling JVM scalar replacement optimization and reducing iterator allocation overhead. Fixes gh-6811 Signed-off-by: David Mollitor <[email protected]>
1 parent 65301f0 commit 1e39d3f

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

micrometer-core/src/main/java/io/micrometer/core/instrument/composite/AbstractCompositeMeter.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@
2020
import io.micrometer.core.instrument.MeterRegistry;
2121
import org.jspecify.annotations.Nullable;
2222

23-
import java.util.Collections;
2423
import java.util.IdentityHashMap;
2524
import java.util.Iterator;
2625
import java.util.Map;
2726
import java.util.concurrent.atomic.AtomicBoolean;
2827

2928
abstract class AbstractCompositeMeter<T extends Meter> extends AbstractMeter implements CompositeMeter {
3029

30+
// IdentityHashMap instead of Collections.emptyMap() keeps implementation type
31+
// consistent, which may help JIT optimization.
32+
private static final Map<MeterRegistry, Meter> EMPTY_CHILDREN = new IdentityHashMap<>(0);
33+
3134
private final AtomicBoolean childrenGuard = new AtomicBoolean();
3235

33-
private Map<MeterRegistry, T> children = Collections.emptyMap();
36+
@SuppressWarnings("unchecked")
37+
private Map<MeterRegistry, T> children = (Map<MeterRegistry, T>) EMPTY_CHILDREN;
3438

3539
private volatile @Nullable T noopMeter;
3640

0 commit comments

Comments
 (0)