Define a custom Scope Key for reducing memory allocations#116
Merged
SokolAndrey merged 13 commits intouber-java:masterfrom Feb 13, 2023
Merged
Define a custom Scope Key for reducing memory allocations#116SokolAndrey merged 13 commits intouber-java:masterfrom
SokolAndrey merged 13 commits intouber-java:masterfrom
Conversation
SokolAndrey
reviewed
Feb 9, 2023
Collaborator
SokolAndrey
left a comment
There was a problem hiding this comment.
we also keep the file with other benchmarks here, since you're changing the core part of the library could you please re-run those benchmarks and see if you change affects those
./gradlew runJmhTests
Contributor
Author
|
@SokolAndrey : Thanks for the review. Ran all the benchmarks in tally-core and committed it. Please take a look again. |
SokolAndrey
approved these changes
Feb 13, 2023
Collaborator
SokolAndrey
left a comment
There was a problem hiding this comment.
lgtm! looking forward to see it in production!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why ?
The current Scope Key is defined by creating a new string based on the metric tags and prefixes. This is inefficient as it ends up creating temporary strings for each metric emission. In our production service, the memory allocations due to scope key construction contribute to ~10% of the entire service allocations. This puts pressure on GC cycles, impacting the latencies of our service.
What ?
Instead of defining the key based on a temporary string(StringBuilder inside ScopeImpl.keyForPrefixedStringMap), we define it on already present objects. This will reduce unnecessary string allocations.
We create a new POJO called
ScopeKeywhich wraps the existing tags and prefix objects, which will be the key for HashMap. HashMap internally uses hashcode() and equal() methods to determine the key. The memory of temporary string outweighs the memory ofScopeKeyobject as the latter has just the references to existing objects.In short, We're changing the implementation of
ScopeImpl.keyForPrefixedStringMapas part of this diff.Benchmark results
Summary
Before
After