-
Notifications
You must be signed in to change notification settings - Fork 19
Define a custom Scope Key for reducing memory allocations #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Define a custom Scope Key for reducing memory allocations #116
Conversation
cdvr1993
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
SokolAndrey
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
|
@SokolAndrey : Thanks for the review. Ran all the benchmarks in tally-core and committed it. Please take a look again. |
SokolAndrey
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm! looking forward to see it in production!
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