Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[db] ResetSnapshots() performance improvement #4153

Merged
merged 6 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions db/batch/batch_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
tag int // latest snapshot + 1
batchShots []int // snapshots of batch are merely size of write queue at time of snapshot
caches []KVStoreCache // snapshots of cache
keyTags map[kvCacheKey][]int
keyTags map[kvCacheKey]*kvCacheValue
tagKeys [][]kvCacheKey
}
)
Expand Down Expand Up @@ -254,19 +254,19 @@
cb.tag = 0
cb.batchShots = make([]int, 0)
cb.caches = []KVStoreCache{NewKVCache()}
cb.keyTags = map[kvCacheKey][]int{}
cb.keyTags = map[kvCacheKey]*kvCacheValue{}
cb.tagKeys = [][]kvCacheKey{{}}
}

func (cb *cachedBatch) touchKey(h kvCacheKey) {
tags, ok := cb.keyTags[h]
if !ok {
cb.keyTags[h] = []int{cb.tag}
cb.keyTags[h] = newkvCacheValue([]int{cb.tag})
cb.tagKeys[cb.tag] = append(cb.tagKeys[cb.tag], h)
return
}
if tags[len(tags)-1] != cb.tag {
cb.keyTags[h] = append(tags, cb.tag)
if tags.last() != cb.tag {
tags.append(cb.tag)
cb.tagKeys[cb.tag] = append(cb.tagKeys[cb.tag], h)
}
}
Expand Down Expand Up @@ -306,8 +306,8 @@
var v []byte
err := ErrNotExist
if tags, ok := cb.keyTags[h]; ok {
for i := len(tags) - 1; i >= 0; i-- {
v, err = cb.caches[tags[i]].Read(&h)
for i := tags.len() - 1; i >= 0; i-- {
v, err = cb.caches[tags.getAt(i)].Read(&h)
if errors.Cause(err) == ErrNotExist {
continue
}
Expand Down Expand Up @@ -344,8 +344,9 @@
for tag := cb.tag; tag < len(cb.tagKeys); tag++ {
keys := cb.tagKeys[tag]
for _, key := range keys {
cb.keyTags[key] = cb.keyTags[key][:len(cb.keyTags[key])-1]
if len(cb.keyTags[key]) == 0 {
kv := cb.keyTags[key]
kv.reverse()
millken marked this conversation as resolved.
Show resolved Hide resolved
if kv.len() == 0 {
delete(cb.keyTags, key)
}
}
Expand All @@ -369,12 +370,10 @@
cb.caches = cb.caches[:1]
}
keys := make([]kvCacheKey, 0, len(cb.keyTags))
for key := range cb.keyTags {
for key, v := range cb.keyTags {
v.reset()

Check warning on line 374 in db/batch/batch_impl.go

View check run for this annotation

Codecov / codecov/patch

db/batch/batch_impl.go#L373-L374

Added lines #L373 - L374 were not covered by tests
keys = append(keys, key)
}
for _, key := range keys {
cb.keyTags[key] = []int{0}
}
cb.tagKeys = [][]kvCacheKey{keys}
}

Expand Down
33 changes: 33 additions & 0 deletions db/batch/kv_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type (
key1 string
key2 string
}
kvCacheValue []int

node struct {
value []byte
Expand All @@ -39,6 +40,38 @@ type (
}
)

func newkvCacheValue(v []int) *kvCacheValue {
return (*kvCacheValue)(&v)
}

func (kv *kvCacheValue) reset() {
([]int)(*kv)[0] = 0
*kv = (*kv)[0:1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[:1]

}

func (kv *kvCacheValue) reverse() {
*kv = (*kv)[0 : kv.len()-1]
millken marked this conversation as resolved.
Show resolved Hide resolved
}
func (kv *kvCacheValue) get() []int {
return ([]int)(*kv)
}

func (kv *kvCacheValue) getAt(i int) int {
return ([]int)(*kv)[i]
}

func (kv *kvCacheValue) append(v int) {
*kv = append(*kv, v)
}

func (kv *kvCacheValue) len() int {
return len(*kv)
}

func (kv *kvCacheValue) last() int {
return (*kv)[len(*kv)-1]
}

// NewKVCache returns a KVCache
func NewKVCache() KVStoreCache {
return &kvCache{
Expand Down
54 changes: 54 additions & 0 deletions db/batch/kv_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ func TestKvCache(t *testing.T) {
require.Equal(v, v3)
}

func TestKvCacheValue(t *testing.T) {
require := require.New(t)

c := newkvCacheValue([]int{1})
require.Equal([]int{1}, c.get())
require.Equal(1, c.len())

c.reset()
require.Equal([]int{0}, c.get())

c.append(3)
require.Equal([]int{0, 3}, c.get())
require.Equal(0, c.getAt(0))
require.Equal(3, c.last())
require.Equal(2, c.len())
c.reverse()
require.Equal([]int{0}, c.get())
require.Equal(1, c.len())
}

func TestWriteIfNotExist(t *testing.T) {
require := require.New(t)

Expand All @@ -114,3 +134,37 @@ func TestWriteIfNotExist(t *testing.T) {
err = c.WriteIfNotExist(k1, v1)
require.NoError(err)
}

func BenchmarkMapKey_ValueOperate(b *testing.B) {
keyTags := map[kvCacheKey]*kvCacheValue{}
keyTags2 := map[kvCacheKey][]int{}
for i := 0; i < 10000; i++ {
key := kvCacheKey{
key1: string(make([]byte, 5)),
key2: string(make([]byte, 32)),
}
keyTags[key] = newkvCacheValue([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
keyTags2[key] = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
}
b.Run("reset by map key", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for k := range keyTags {
keyTags[k].reset()
}
}
})
b.Run("reset by map value, 2x faster", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, v := range keyTags {
v.reset()
}
}
})
b.Run("orign map assign", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for k := range keyTags2 {
keyTags2[k] = []int{0}
}
}
})
}
Loading