Commit 3ecc97d
committed
user: avoid full metadata copy in ExtractFromGRPCRequest
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>1 parent 0315015 commit 3ecc97d
2 files changed
Lines changed: 33 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
| 11 | + | |
| 12 | + | |
18 | 13 | | |
19 | 14 | | |
20 | 15 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
0 commit comments