metadata: reduce memory footprint in FromOutgoingContext (#4360)
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
This commit is contained in:
Родитель
24d03d9f76
Коммит
b602d17e45
|
@ -75,13 +75,9 @@ func Pairs(kv ...string) MD {
|
||||||
panic(fmt.Sprintf("metadata: Pairs got the odd number of input pairs for metadata: %d", len(kv)))
|
panic(fmt.Sprintf("metadata: Pairs got the odd number of input pairs for metadata: %d", len(kv)))
|
||||||
}
|
}
|
||||||
md := MD{}
|
md := MD{}
|
||||||
var key string
|
for i := 0; i < len(kv); i += 2 {
|
||||||
for i, s := range kv {
|
key := strings.ToLower(kv[i])
|
||||||
if i%2 == 0 {
|
md[key] = append(md[key], kv[i+1])
|
||||||
key = strings.ToLower(s)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
md[key] = append(md[key], s)
|
|
||||||
}
|
}
|
||||||
return md
|
return md
|
||||||
}
|
}
|
||||||
|
@ -195,12 +191,18 @@ func FromOutgoingContext(ctx context.Context) (MD, bool) {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
mds := make([]MD, 0, len(raw.added)+1)
|
out := raw.md.Copy()
|
||||||
mds = append(mds, raw.md)
|
for _, added := range raw.added {
|
||||||
for _, vv := range raw.added {
|
if len(added)%2 == 1 {
|
||||||
mds = append(mds, Pairs(vv...))
|
panic(fmt.Sprintf("metadata: FromOutgoingContext got an odd number of input pairs for metadata: %d", len(added)))
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(added); i += 2 {
|
||||||
|
key := strings.ToLower(added[i])
|
||||||
|
out[key] = append(out[key], added[i+1])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Join(mds...), ok
|
return out, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
type rawMD struct {
|
type rawMD struct {
|
||||||
|
|
Загрузка…
Ссылка в новой задаче