Skip to content

user: avoid full metadata copy in ExtractFromGRPCRequest#9

Merged
alanprot merged 1 commit into
masterfrom
reduce-grpc-metadata-allocs
May 8, 2026
Merged

user: avoid full metadata copy in ExtractFromGRPCRequest#9
alanprot merged 1 commit into
masterfrom
reduce-grpc-metadata-allocs

Conversation

@alanprot
Copy link
Copy Markdown
Member

@alanprot alanprot commented May 8, 2026

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%

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>
@alanprot alanprot merged commit 75366cf into master May 8, 2026
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants