Avoid allocation of iterator with Tags#empty instance by shakuzen · Pull Request #3313 · micrometer-metrics/micrometer (original) (raw)
Special case calling Tags.and and Tags.of on a Tags.empty() instance to avoid instantiating an iterator for the EMPTY constant static instance.
I've checked this change locally with a JMH test and profiling. This change avoids unnecessary allocation in e.g. the following code:
Tags extraTags = Tags.empty(); Tags anotherTag = Tags.of("a", "b"); Tags combined = anotherTag.and(extraTags); combined = Tags.concat(extraTags, "a", "b");
This shows up in several places in our instrumentation where we allow passing extraTags to add to meters created by the instrumentation, and the default is no extra tags (Tags.empty()).