Skip to content

Commit

Permalink
metadata: reduce memory footprint in FromOutgoingContext
Browse files Browse the repository at this point in the history
When Looking at memory profiles for cockroachdb/cockroach, we observed
that the intermediate metadata.MD array constructed to iterate over
appended metadata escaped to the heap. Fortunately, this is easily
rectifiable.

  go build -gcflags '-m' google.golang.org/grpc/metadata
  ...
  google.golang.org/grpc/metadata/metadata.go:198:13: make([]MD, 0, len(raw.added) + 1) escapes to heap
  • Loading branch information
irfansharif committed Apr 22, 2021
1 parent 7276af6 commit 610b085
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions metadata/metadata.go
Expand Up @@ -195,12 +195,22 @@ func FromOutgoingContext(ctx context.Context) (MD, bool) {
return nil, false
}

mds := make([]MD, 0, len(raw.added)+1)
mds = append(mds, raw.md)
for _, vv := range raw.added {
mds = append(mds, Pairs(vv...))
out := raw.md.Copy()
for _, added := range raw.added {
if len(added)%2 == 1 {
panic(fmt.Sprintf("metadata: FromOutgoingContext got the odd number of input pairs for metadata: %d", len(added)))
}

var key string
for i, s := range added {
if i%2 == 0 {
key = strings.ToLower(s)
continue
}
out[key] = append(out[key], s)
}
}
return Join(mds...), ok
return out, ok
}

type rawMD struct {
Expand Down

0 comments on commit 610b085

Please sign in to comment.