Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@
import io.micrometer.core.instrument.MeterRegistry;
import org.jspecify.annotations.Nullable;

import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

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

private static final IdentityHashMap<MeterRegistry, Meter> EMPTY_CHILDREN = new IdentityHashMap<>(0);

private final AtomicBoolean childrenGuard = new AtomicBoolean();

private Map<MeterRegistry, T> children = Collections.emptyMap();
// Enforcing type of Map to explicitly be constrained to one type may help JIT optimizations.
@SuppressWarnings("unchecked")
private IdentityHashMap<MeterRegistry, T> children = (IdentityHashMap<MeterRegistry, T>) EMPTY_CHILDREN;

private volatile @Nullable T noopMeter;

Expand Down Expand Up @@ -72,7 +75,7 @@ public final void add(MeterRegistry registry) {
for (;;) {
if (childrenGuard.compareAndSet(false, true)) {
try {
Map<MeterRegistry, T> newChildren = new IdentityHashMap<>(children);
IdentityHashMap<MeterRegistry, T> newChildren = new IdentityHashMap<>(children);
newChildren.put(registry, newMeter);
this.children = newChildren;
break;
Expand All @@ -95,7 +98,7 @@ public final void remove(MeterRegistry registry) {
for (;;) {
if (childrenGuard.compareAndSet(false, true)) {
try {
Map<MeterRegistry, T> newChildren = new IdentityHashMap<>(children);
IdentityHashMap<MeterRegistry, T> newChildren = new IdentityHashMap<>(children);
newChildren.remove(registry);
this.children = newChildren;
break;
Expand Down