Skip to content

Commit a10492b

Browse files
committed
Efficient copy of meters to list
Gets rid of an extra copy, which generates garbage for no benefit. The new implementation is at least as performant as the previous one while generating approximately half the garbage. Resolves gh-7035 Supersedes gh-7040
1 parent 84ed865 commit a10492b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,18 @@ Meter register(Meter.Id id, Meter.Type type, Iterable<Measurement> measurements)
408408
}
409409

410410
/**
411-
* @return The set of registered meters.
411+
* Get currently registered meters.
412+
* @return An unmodifiable copy of registered meters.
413+
* @implNote Avoids the ArrayList constructor because it copies the array
414+
* twice.
412415
*/
416+
@SuppressWarnings("unchecked")
413417
public List<Meter> getMeters() {
414-
return Collections.unmodifiableList(new ArrayList<>(meterMap.values()));
418+
return Collections.unmodifiableList(
419+
// Arrays.asList wraps the array without copying it (unlike new ArrayList)
420+
// The cast is safe because we know the map only contains Meters and the
421+
// returned list is unmodifiable
422+
(List<Meter>) (List<?>) Arrays.asList(meterMap.values().toArray()));
415423
}
416424

417425
/**

0 commit comments

Comments
 (0)