user: avoid full metadata copy in ExtractFromGRPCRequest#9
Merged
Conversation
Use metadata.ValueFromIncomingContext to read only the x-scope-orgid key instead of metadata.FromIncomingContext which deep-copies the entire metadata map. FromIncomingContext copies every key-value pair in the metadata map on every call. With 10 metadata keys per request, this is 16 allocations and 1016 bytes per call. ValueFromIncomingContext only copies the single requested key's []string slice. Benchmark results (10 metadata keys): name old time/op new time/op delta ExtractFromGRPCRequest-48 1389ns ± 2% 135ns ± 1% -90.3% name old alloc/op new alloc/op delta ExtractFromGRPCRequest-48 1016B ± 0% 80B ± 0% -92.1% name old allocs/op new allocs/op delta ExtractFromGRPCRequest-48 16.0 ± 0% 3.0 ± 0% -81.2% Signed-off-by: Alan Protasio <approtas@amazon.com>
danielblando
approved these changes
May 8, 2026
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.
Use metadata.ValueFromIncomingContext to read only the x-scope-orgid key instead of metadata.FromIncomingContext which deep-copies the entire metadata map.
FromIncomingContext copies every key-value pair in the metadata map on every call. With 10 metadata keys per request, this is 16 allocations and 1016 bytes per call. ValueFromIncomingContext only copies the single requested key's []string slice.
Benchmark results (10 metadata keys):