Skip to content

Commit a55c2da

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 a55c2da

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,22 @@
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+
@SuppressWarnings("rawtypes")
31+
private static final Map EMPTY_CHILDREN = new IdentityHashMap<>(0);
32+
3133
private final AtomicBoolean childrenGuard = new AtomicBoolean();
3234

33-
private Map<MeterRegistry, T> children = Collections.emptyMap();
35+
// IdentityHashMap instead of Collections.emptyMap() keeps implementation type
36+
// consistent, which may help JIT optimization.
37+
@SuppressWarnings("unchecked")
38+
private Map<MeterRegistry, T> children = EMPTY_CHILDREN;
3439

3540
private volatile @Nullable T noopMeter;
3641

0 commit comments

Comments
 (0)