Skip to content

Commit

Permalink
feat: add GetValueToMap that sets value to the input map (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppzqh authored Jul 11, 2024
1 parent 5df24c0 commit a03554c
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cloud/metainfo/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,28 @@ func GetAllValues(ctx context.Context) (m map[string]string) {
return
}

// GetValueToMap retrieves the value set into the context by the given key and set the value to the input map.
// Only use this function when you want to get a small set of values instead of GetAllValues.
// The logic of getting value follows GetAllValues, transient value has higher priority if key is same.
func GetValueToMap(ctx context.Context, m map[string]string, keys ...string) {
if m == nil || len(keys) == 0 {
return
}
n := getNode(ctx)
if n == nil {
return
}
for _, k := range keys {
if idx, ok := search(n.transient, k); ok {
m[k] = n.transient[idx].val
continue
}
if idx, ok := search(n.stale, k); ok {
m[k] = n.stale[idx].val
}
}
}

// RangeValues calls f sequentially for each transient kv.
// If f returns false, range stops the iteration.
func RangeValues(ctx context.Context, f func(k, v string) bool) {
Expand Down
69 changes: 69 additions & 0 deletions cloud/metainfo/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package metainfo_test
import (
"context"
"fmt"
"math"
"testing"

"github.com/bytedance/gopkg/cloud/metainfo"
Expand Down Expand Up @@ -461,12 +462,21 @@ func benchmark(b *testing.B, api string, count int) {
for i := 0; i < b.N; i++ {
_, _ = metainfo.GetValue(ctx, keys[i%len(keys)])
}
case "GetValueToMap":
b.ReportAllocs()
b.ResetTimer()
m := make(map[string]string, len(keys))
for i := 0; i < b.N; i++ {
metainfo.GetValueToMap(ctx, m, keys...)
}
case "GetAllValues":
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = metainfo.GetAllValues(ctx)
}
case "GetValueWithKeys":
benchmarkGetValueWithKeys(b, ctx, keys)
case "RangeValues":
b.ReportAllocs()
b.ResetTimer()
Expand Down Expand Up @@ -563,6 +573,42 @@ func benchmark(b *testing.B, api string, count int) {
}
}

func benchmarkGetValueWithKeys(b *testing.B, ctx context.Context, keys []string) {
selectedRatio := 0.1
selectedKeyLength := uint64(math.Round(selectedRatio * float64(len(keys))))
selectedKeys := keys[:selectedKeyLength]

b.Run("GetValue", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
m := make(map[string]string, len(selectedKeys))
for i := 0; i < b.N; i++ {
for j := 0; j < len(selectedKeys); j++ {
key := selectedKeys[j]
v, _ := metainfo.GetValue(ctx, key)
m[key] = v
}
}
})

b.Run("GetValueToMap", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
m := make(map[string]string, len(selectedKeys))
for i := 0; i < b.N; i++ {
metainfo.GetValueToMap(ctx, m, selectedKeys...)
}
})

b.Run("GetAllValue", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = metainfo.GetAllValues(ctx)
}
})
}

func benchmarkParallel(b *testing.B, api string, count int) {
ctx, keys, vals := initMetaInfo(count)
switch api {
Expand All @@ -584,6 +630,17 @@ func benchmarkParallel(b *testing.B, api string, count int) {
i++
}
})
case "GetValueToMap":
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
m := make(map[string]string, len(keys))
for i := 0; i < b.N; i++ {
metainfo.GetValueToMap(ctx, m, keys...)
}
}
})
case "GetAllValues":
b.ReportAllocs()
b.ResetTimer()
Expand Down Expand Up @@ -727,7 +784,9 @@ func BenchmarkAll(b *testing.B) {
APIs := []string{
"TransferForward",
"GetValue",
"GetValueToMap",
"GetAllValues",
"GetValueWithKeys",
"WithValue",
"WithValues",
"WithValueAcc",
Expand All @@ -754,6 +813,7 @@ func BenchmarkAllParallel(b *testing.B) {
APIs := []string{
"TransferForward",
"GetValue",
"GetValueToMap",
"GetAllValues",
"WithValue",
"WithValues",
Expand Down Expand Up @@ -858,3 +918,12 @@ func TestPersistentValuesCount(t *testing.T) {
})
}
}

func TestGetValueToMap(t *testing.T) {
ctx := context.Background()
k, v := "key", "value"
ctx = metainfo.WithValue(ctx, k, v)
m := make(map[string]string, 1)
metainfo.GetValueToMap(ctx, m, k)
assert(t, m[k] == v)
}

0 comments on commit a03554c

Please sign in to comment.