Skip to content

Commit

Permalink
perf: metainfo reduce mallocgc (bytedance#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
joway committed Apr 19, 2024
1 parent 21fc7a1 commit fefc805
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 90 deletions.
62 changes: 50 additions & 12 deletions cloud/metainfo/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,35 +76,73 @@ func FromHTTPHeader(ctx context.Context, h HTTPHeaderCarrier) context.Context {
if ctx == nil || h == nil {
return ctx
}

var m *mapView
if x := getNode(ctx); x != nil {
m = x.mapView()
} else {
m = newMapView()
nd := getNode(ctx)
if nd == nil || nd.size() == 0 {
return newCtxFromHTTPHeader(ctx, h)
}

// inherit from exist ctx node
persistent := newKVStore()
transient := newKVStore()
sliceToMap(nd.persistent, persistent)
sliceToMap(nd.transient, transient)

// insert new kvs from http header
h.Visit(func(k, v string) {
if len(v) == 0 {
return
}

kk := strings.ToLower(k)
ln := len(kk)
if ln > lenHPT && strings.HasPrefix(kk, HTTPPrefixTransient) {
kk = HTTPHeaderToCGIVariable(kk[lenHPT:])
transient[kk] = v
} else if ln > lenHPP && strings.HasPrefix(kk, HTTPPrefixPersistent) {
kk = HTTPHeaderToCGIVariable(kk[lenHPP:])
persistent[kk] = v
}
})

// return original ctx if no invalid key in http header
if (persistent.size() + transient.size()) == 0 {
return ctx
}

// make new kvs
nd = newNodeFromMaps(persistent, transient, nil)
persistent.recycle()
transient.recycle()
ctx = withNode(ctx, nd)
return ctx
}

func newCtxFromHTTPHeader(ctx context.Context, h HTTPHeaderCarrier) context.Context {
nd := &node{
persistent: make([]kv, 0, 16), // 32B * 16 = 512B
transient: make([]kv, 0, 16),
stale: []kv{},
}
// insert new kvs from http header to node
h.Visit(func(k, v string) {
if len(v) == 0 {
return
}
kk := strings.ToLower(k)
ln := len(kk)
if ln > lenHPT && strings.HasPrefix(kk, HTTPPrefixTransient) {
kk = HTTPHeaderToCGIVariable(kk[lenHPT:])
m.transient[kk] = v
nd.transient = append(nd.transient, kv{key: kk, val: v})
} else if ln > lenHPP && strings.HasPrefix(kk, HTTPPrefixPersistent) {
kk = HTTPHeaderToCGIVariable(kk[lenHPP:])
m.persistent[kk] = v
nd.persistent = append(nd.persistent, kv{key: kk, val: v})
}
})

if m.size() == 0 {
// TODO: remove this?
// return original ctx if no invalid key in http header
if nd.size() == 0 {
return ctx
}
return withNode(ctx, m.toNode())
return withNode(ctx, nd)
}

// ToHTTPHeader writes all metainfo into the given HTTP header.
Expand Down
2 changes: 1 addition & 1 deletion cloud/metainfo/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestFromHTTPHeaderKeepPreviousData(t *testing.T) {
c1 := metainfo.FromHTTPHeader(c0, metainfo.HTTPHeader(h))
assert(t, c0 != c1)
vs := metainfo.GetAllValues(c1)
assert(t, len(vs) == 3)
assert(t, len(vs) == 3, len(vs))
assert(t, vs["tk"] == "tv" && vs["uk"] == "uv" && vs["XK"] == "xv")
vs = metainfo.GetAllPersistentValues(c1)
assert(t, len(vs) == 3)
Expand Down
65 changes: 30 additions & 35 deletions cloud/metainfo/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

package metainfo

import "context"
import (
"context"
)

type ctxKeyType struct{}

Expand All @@ -25,6 +27,33 @@ type kv struct {
val string
}

func newNodeFromMaps(persistent, transient, stale kvstore) *node {
ps, ts, sz := persistent.size(), transient.size(), stale.size()
// make slices together to reduce malloc cost
kvs := make([]kv, ps+ts+sz)
nd := new(node)
nd.persistent = kvs[:ps]
nd.transient = kvs[ps : ps+ts]
nd.stale = kvs[ps+ts:]

i := 0
for k, v := range persistent {
nd.persistent[i].key, nd.persistent[i].val = k, v
i++
}
i = 0
for k, v := range transient {
nd.transient[i].key, nd.transient[i].val = k, v
i++
}
i = 0
for k, v := range stale {
nd.stale[i].key, nd.stale[i].val = k, v
i++
}
return nd
}

type node struct {
persistent []kv
transient []kv
Expand Down Expand Up @@ -122,40 +151,6 @@ func (n *node) delPersistent(k string) (r *node) {
return n
}

type mapView struct {
persistent map[string]string
transient map[string]string
stale map[string]string
}

func newMapView() *mapView {
return &mapView{
persistent: make(map[string]string),
transient: make(map[string]string),
stale: make(map[string]string),
}
}

func (m *mapView) size() int {
return len(m.persistent) + len(m.transient) + len(m.stale)
}

func (m *mapView) toNode() *node {
return &node{
persistent: mapToSlice(m.persistent),
transient: mapToSlice(m.transient),
stale: mapToSlice(m.stale),
}
}

func (n *node) mapView() *mapView {
return &mapView{
persistent: sliceToMap(n.persistent),
transient: sliceToMap(n.transient),
stale: sliceToMap(n.stale),
}
}

func search(kvs []kv, key string) (idx int, ok bool) {
for i := range kvs {
if kvs[i].key == key {
Expand Down
55 changes: 55 additions & 0 deletions cloud/metainfo/kvstore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2023 ByteDance Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package metainfo

import "sync"

type kvstore map[string]string

var kvpool sync.Pool

func newKVStore(size ...int) kvstore {
kvs := kvpool.Get()
if kvs == nil {
if len(size) > 0 {
return make(kvstore, size[0])
}
return make(kvstore)
}
return kvs.(kvstore)
}

func (store kvstore) size() int {
return len(store)
}

func (store kvstore) recycle() {
/*
for k := range m {
delete(m, k)
}
==>
LEAQ type.map[string]int(SB), AX
MOVQ AX, (SP)
MOVQ "".m(SB), AX
MOVQ AX, 8(SP)
PCDATA $1, $0
CALL runtime.mapclear(SB)
*/
for key := range store {
delete(store, key)
}
kvpool.Put(store)
}
66 changes: 66 additions & 0 deletions cloud/metainfo/kvstore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2023 ByteDance Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package metainfo

import (
"fmt"
"testing"
)

func TestKVStore(t *testing.T) {
store := newKVStore()
store["a"] = "a"
store["a"] = "b"
if store["a"] != "b" {
t.Fatal()
}
store.recycle()
if store["a"] == "b" {
t.Fatal()
}
store = newKVStore()
if store["a"] == "b" {
t.Fatal()
}
}

func BenchmarkMap(b *testing.B) {
for keys := 1; keys <= 1000; keys *= 10 {
b.Run(fmt.Sprintf("keys=%d", keys), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
m := make(map[string]string)
for idx := 0; idx < 1000; idx++ {
m[fmt.Sprintf("key-%d", idx)] = string('a' + byte(idx%26))
}
}
})
}
}

func BenchmarkKVStore(b *testing.B) {
for keys := 1; keys <= 1000; keys *= 10 {
b.Run(fmt.Sprintf("keys=%d", keys), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
m := newKVStore()
for idx := 0; idx < 1000; idx++ {
m[fmt.Sprintf("key-%d", idx)] = string('a' + byte(idx%26))
}
m.recycle()
}
})
}
}
Loading

0 comments on commit fefc805

Please sign in to comment.