-
Notifications
You must be signed in to change notification settings - Fork 656
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
Reduce fmt.Sprintf allocations in query encoding #2919
base: main
Are you sure you want to change the base?
Reduce fmt.Sprintf allocations in query encoding #2919
Conversation
Thanks for your contribution. Just letting you know this is still on our radar, but we've been a bit busy. For context, was there a particular scenario where this was causing a performance issue for you? |
Thanks for the response. I work on an OSS CloudWatch exporter https://github.com/prometheus-community/yet-another-cloudwatch-exporter/. When running high volumes we see some rather spiky memory and I was trying to reduce it a bit. This shows the top allocators from pprof after running for some time, Almost all of the |
// Lists can't have flat members | ||
return newValue(a.values, fmt.Sprintf("%s.%d", prefix, a.size), false) | ||
return newValue(a.values, fmt.Sprintf("%s.%d", a.prefix, a.size), false) |
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.
Why leave Sprintf here?
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.
The extra int formatting for this case causes an increase in allocations when the size of the array being serialized was between 100-500. There's a nice drop in CPU but a lot of the CPU utilization we see is driven by GC so I chose to leave this one alone. I'll edit the full benchmark results in to the PR description.
Can we see some benchmarks that actually use the query encoder for some structure (ideally something copied from say EC2)? This seems fine on paper but all the benchmarks are demonstrating is the difference between these operations in isolation. |
We recently added instructions on how to generate a changelog entry for PRs. In the past, we had to do this on behalf of contributors, slowing down the review process. Can you go through this document and generate a changelog entry for this PR? |
This PR eliminates some calls to
fmt.Sprintf
in favor of string concatenation when the values being concatenated are strings. This should reduce the memory required to construct queries with complex objects / larger arrays. Arrays which are not flat should see a further improvement as the prefix is now computed once ahead of time instead of doing it with anfmt.Sprintf
call on every element.Included are some benchmarks which I used to decide that the maps + array use cases with
fmt.Sprintf
seem okay. Droppingfmt.Sprintf
is faster but won't lead to any reduction in allocations and in the case of arrays a size between 100 and 499 is actually more allocations.String formatting benchmark results
Int formatting benchmark results
Map key formatting benchmark results