Skip to content

Avoid allocation of iterator with Tags#empty instance#3313

Merged
shakuzen merged 2 commits intomicrometer-metrics:1.8.xfrom
shakuzen:tags-and-empty
Jul 26, 2022
Merged

Avoid allocation of iterator with Tags#empty instance#3313
shakuzen merged 2 commits intomicrometer-metrics:1.8.xfrom
shakuzen:tags-and-empty

Conversation

@shakuzen
Copy link
Copy Markdown
Member

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()).

@shakuzen shakuzen added enhancement A general enhancement performance Issues related to general performance module: micrometer-core An issue that is related to our core module labels Jul 25, 2022
@shakuzen shakuzen added this to the 1.8.9 milestone Jul 25, 2022
Special case calling Tags.and and Tags.of on a Tags.empty() instance to avoid instantiating an iterator for the EMPTY constant static instance.
@shakuzen
Copy link
Copy Markdown
Member Author

I've added tests verifying there are now no allocations when using and or of with the empty Tags instance. These tests may be re-worked in the future with #3209.

@shakuzen shakuzen merged commit b93fc3b into micrometer-metrics:1.8.x Jul 26, 2022
@shakuzen shakuzen deleted the tags-and-empty branch July 26, 2022 06:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement A general enhancement module: micrometer-core An issue that is related to our core module performance Issues related to general performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants