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

server: avoid call to trace.FromContext and resulting allocations when tracing is disabled #2926

Merged
merged 1 commit into from
Jul 30, 2019

Conversation

dzbarsky
Copy link
Contributor

Sadly trace.FromContext isn't cheap because it uses a string as the context key which leads to allocations. This patch makes the function a no-op when tracing is disabled, as it should be.

@dzbarsky
Copy link
Contributor Author

@dfawley mind taking a look?

@dfawley
Copy link
Member

dfawley commented Jul 25, 2019

I don't understand - how does trace.FromContext allocate memory? It uses a global var as the context key, so I don't see where the allocation would come in. I'm not opposed to this change if it shows a meaningful performance improvement, but I would like to make sure there is a real benefit first.

@dzbarsky
Copy link
Contributor Author

See https://golang.org/pkg/context/#WithValue - To avoid allocating when assigning to an interface{}, context keys often have concrete type struct{}. Alternatively, exported context key variables' static type should be a pointer or interface.

basically a string is not a single pointer, so it can't be passed to an interface{} without an intermediate allocation.

@dzbarsky
Copy link
Contributor Author

@dfawley ping on this one? i can show you a profile if you want :)

@dzbarsky
Copy link
Contributor Author

@dfawley
Copy link
Member

dfawley commented Jul 30, 2019

Interesting. Should net/trace be changed, then?

This is fine. I am not sure we need to do a trace.FromContext at all, even with tracing enabled, but that would be a bigger change and this is an easy quick-fix.

@dfawley dfawley added the Type: Performance Performance improvements (CPU, network, memory, etc) label Jul 30, 2019
@dfawley dfawley changed the title Make Server.traceInfo cheaper server: avoid call to trace.FromContext and resulting allocations when tracing is disabled Jul 30, 2019
@dfawley dfawley merged commit 92635fa into grpc:master Jul 30, 2019
@dzbarsky dzbarsky deleted the traceinfo branch July 30, 2019 20:29
@dzbarsky
Copy link
Contributor Author

Yeah i'll send net/trace a patch as well

@dfawley dfawley added this to the 1.23 Release milestone Aug 1, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jan 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Type: Performance Performance improvements (CPU, network, memory, etc)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants