Skip to content
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

perf: improve memory usage in apmhttp module #1531

Merged
merged 1 commit into from
Oct 26, 2023

Conversation

kruskall
Copy link
Member

Few performance improvements related to memory usage:

  • use simple string concat if there's no loop and we are concatenating a few strings (this is faster)
  • use the Header map instead of calling Set if the key is a constant value (avoid alloc in canonical conversion)
  • replace printf with more specific methods when possible (faster and less memory usage)
  • let the runtime handle map realloc and do not prealloc a map with max size (this is wasted memory)

@kruskall kruskall requested a review from a team as a code owner October 25, 2023 13:25
@@ -139,11 +139,11 @@ func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
func SetHeaders(req *http.Request, traceContext apm.TraceContext, propagateLegacyHeader bool) {
headerValue := FormatTraceparentHeader(traceContext)
if propagateLegacyHeader {
req.Header.Set(ElasticTraceparentHeader, headerValue)
req.Header[ElasticTraceparentHeader] = []string{headerValue}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on the change but out of curiosity, won't they have the same allocs? Probably a bit high CPU cost in case of using Set due to canonical conversion of header keys but allocation wise I think they would be same.

@kruskall kruskall merged commit 099bcae into elastic:main Oct 26, 2023
9 checks passed
@kruskall kruskall deleted the perf/apmhttp-memory branch October 26, 2023 06:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants