Skip to content

Commit 99b4858

Browse files
feat(stf/branch): simplify merged iterator (#22131)
Co-authored-by: marbar3778 <[email protected]>
1 parent ffa74d1 commit 99b4858

File tree

14 files changed

+283
-216
lines changed

14 files changed

+283
-216
lines changed

runtime/v2/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.23
55
// server v2 integration
66
replace (
77
cosmossdk.io/api => ../../api
8+
cosmossdk.io/core/testing => ../../core/testing
89
cosmossdk.io/server/v2/appmanager => ../../server/v2/appmanager
910
cosmossdk.io/server/v2/stf => ../../server/v2/stf
1011
cosmossdk.io/store/v2 => ../../store/v2
@@ -30,7 +31,7 @@ require (
3031
require (
3132
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.35.1-20240701160653-fedbb9acfd2f.1 // indirect
3233
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.1-20240130113600-88ef6483f90f.1 // indirect
33-
cosmossdk.io/core/testing v0.0.0-20240923163230-04da382a9f29 // indirect
34+
cosmossdk.io/core/testing v0.0.0 // indirect
3435
cosmossdk.io/errors/v2 v2.0.0-20240731132947-df72853b3ca5 // indirect
3536
github.com/DataDog/zstd v1.5.5 // indirect
3637
github.com/beorn7/perks v1.0.1 // indirect

runtime/v2/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.1-20240130113600-88e
44
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.1-20240130113600-88ef6483f90f.1/go.mod h1:zqi/LZjZhyvjCMTEVIwAf5VRlkLduuCfqmZxgoormq0=
55
cosmossdk.io/core v1.0.0-alpha.5 h1:McjYXAQ6XcT20v2uHyH7PhoWH8V+mebzfVFqT3GinsI=
66
cosmossdk.io/core v1.0.0-alpha.5/go.mod h1:3u9cWq1FAVtiiCrDPpo4LhR+9V6k/ycSG4/Y/tREWCY=
7-
cosmossdk.io/core/testing v0.0.0-20240923163230-04da382a9f29 h1:NxxUo0GMJUbIuVg0R70e3cbn9eFTEuMr7ev1AFvypdY=
8-
cosmossdk.io/core/testing v0.0.0-20240923163230-04da382a9f29/go.mod h1:8s2tPeJtSiQuoyPmr2Ag7meikonISO4Fv4MoO8+ORrs=
97
cosmossdk.io/depinject v1.1.0 h1:wLan7LG35VM7Yo6ov0jId3RHWCGRhe8E8bsuARorl5E=
108
cosmossdk.io/depinject v1.1.0/go.mod h1:kkI5H9jCGHeKeYWXTqYdruogYrEeWvBQCw1Pj4/eCFI=
119
cosmossdk.io/errors/v2 v2.0.0-20240731132947-df72853b3ca5 h1:IQNdY2kB+k+1OM2DvqFG1+UgeU1JzZrWtwuWzI3ZfwA=

server/v2/cometbft/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.23.1
44

55
replace (
66
cosmossdk.io/api => ../../../api
7+
cosmossdk.io/core/testing => ../../../core/testing
78
cosmossdk.io/server/v2 => ../
89
cosmossdk.io/server/v2/appmanager => ../appmanager
910
cosmossdk.io/server/v2/stf => ../stf
@@ -43,7 +44,7 @@ require (
4344
require (
4445
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.35.1-20240701160653-fedbb9acfd2f.1 // indirect
4546
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.1-20240130113600-88ef6483f90f.1 // indirect
46-
cosmossdk.io/core/testing v0.0.0-20240923163230-04da382a9f29 // indirect
47+
cosmossdk.io/core/testing v0.0.0 // indirect
4748
cosmossdk.io/depinject v1.1.0 // indirect
4849
cosmossdk.io/errors v1.0.1 // indirect
4950
cosmossdk.io/math v1.3.0 // indirect

server/v2/cometbft/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ cosmossdk.io/collections v0.4.0 h1:PFmwj2W8szgpD5nOd8GWH6AbYNi1f2J6akWXJ7P5t9s=
88
cosmossdk.io/collections v0.4.0/go.mod h1:oa5lUING2dP+gdDquow+QjlF45eL1t4TJDypgGd+tv0=
99
cosmossdk.io/core v1.0.0-alpha.5 h1:McjYXAQ6XcT20v2uHyH7PhoWH8V+mebzfVFqT3GinsI=
1010
cosmossdk.io/core v1.0.0-alpha.5/go.mod h1:3u9cWq1FAVtiiCrDPpo4LhR+9V6k/ycSG4/Y/tREWCY=
11-
cosmossdk.io/core/testing v0.0.0-20240923163230-04da382a9f29 h1:NxxUo0GMJUbIuVg0R70e3cbn9eFTEuMr7ev1AFvypdY=
12-
cosmossdk.io/core/testing v0.0.0-20240923163230-04da382a9f29/go.mod h1:8s2tPeJtSiQuoyPmr2Ag7meikonISO4Fv4MoO8+ORrs=
1311
cosmossdk.io/depinject v1.1.0 h1:wLan7LG35VM7Yo6ov0jId3RHWCGRhe8E8bsuARorl5E=
1412
cosmossdk.io/depinject v1.1.0/go.mod h1:kkI5H9jCGHeKeYWXTqYdruogYrEeWvBQCw1Pj4/eCFI=
1513
cosmossdk.io/errors v1.0.1 h1:bzu+Kcr0kS/1DuPBtUFdWjzLqyUuCiyHjyJB6srBV/0=

server/v2/stf/branch/bench_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package branch
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"cosmossdk.io/core/store"
8+
coretesting "cosmossdk.io/core/testing"
9+
)
10+
11+
var (
12+
stackSizes = []int{1, 10, 100}
13+
elemsInStack = 10
14+
)
15+
16+
func Benchmark_CacheStack_Set(b *testing.B) {
17+
for _, stackSize := range stackSizes {
18+
b.Run(fmt.Sprintf("StackSize%d", stackSize), func(b *testing.B) {
19+
bs := makeBranchStack(b, stackSize)
20+
b.ResetTimer()
21+
b.ReportAllocs()
22+
for i := 0; i < b.N; i++ {
23+
_ = bs.Set([]byte{0}, []byte{0})
24+
}
25+
})
26+
}
27+
}
28+
29+
func Benchmark_Get(b *testing.B) {
30+
for _, stackSize := range stackSizes {
31+
b.Run(fmt.Sprintf("StackSize%d", stackSize), func(b *testing.B) {
32+
bs := makeBranchStack(b, stackSize)
33+
b.ResetTimer()
34+
b.ReportAllocs()
35+
for i := 0; i < b.N; i++ {
36+
_, _ = bs.Get([]byte{0})
37+
}
38+
})
39+
}
40+
}
41+
42+
func Benchmark_Iterate(b *testing.B) {
43+
var keySink, valueSink any
44+
45+
for _, stackSize := range stackSizes {
46+
b.Run(fmt.Sprintf("StackSize%d", stackSize), func(b *testing.B) {
47+
bs := makeBranchStack(b, stackSize)
48+
b.ResetTimer()
49+
b.ReportAllocs()
50+
for i := 0; i < b.N; i++ {
51+
iter, _ := bs.Iterator(nil, nil)
52+
for iter.Valid() {
53+
keySink = iter.Key()
54+
valueSink = iter.Value()
55+
iter.Next()
56+
}
57+
_ = iter.Close()
58+
}
59+
})
60+
}
61+
62+
_ = keySink
63+
_ = valueSink
64+
}
65+
66+
// makeBranchStack creates a branch stack of the given size and initializes it with unique key-value pairs.
67+
func makeBranchStack(b *testing.B, stackSize int) Store[store.KVStore] {
68+
parent := coretesting.NewMemKV()
69+
branch := NewStore[store.KVStore](parent)
70+
for i := 1; i < stackSize; i++ {
71+
branch = NewStore[store.KVStore](branch)
72+
for j := 0; j < elemsInStack; j++ {
73+
// create unique keys by including the branch index.
74+
key := []byte{byte(i), byte(j)}
75+
value := []byte{byte(j)}
76+
err := branch.Set(key, value)
77+
if err != nil {
78+
b.Fatal(err)
79+
}
80+
}
81+
}
82+
return branch
83+
}

server/v2/stf/branch/changeset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const (
1515

1616
var errKeyEmpty = errors.New("key cannot be empty")
1717

18-
// changeSet implements the sorted cache for cachekv store,
18+
// changeSet implements the sorted tree for cachekv store,
1919
// we don't use MemDB here because cachekv is used extensively in sdk core path,
2020
// we need it to be as fast as possible, while `MemDB` is mainly used as a mocking db in unit tests.
2121
//

0 commit comments

Comments
 (0)