Skip to content

Commit 1c80a47

Browse files
author
yihuang
authored
feat: save some memory allocations on un-used cache store (#248)
* feat: save some memory allocations on un-used cache store * Update CHANGELOG.md Signed-off-by: yihuang <huang@crypto.com> * Update store/internal/btree/btree.go Signed-off-by: yihuang <huang@crypto.com> --------- Signed-off-by: yihuang <huang@crypto.com>
1 parent 829c542 commit 1c80a47

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4444
* (baseapp) [#206](https://github.com/crypto-org-chain/cosmos-sdk/pull/206) Support mount object store in baseapp, add `ObjectStore` api in context.
4545
* (bank) [#237](https://github.com/crypto-org-chain/cosmos-sdk/pull/237) Support virtual accounts in sending coins.
4646
* [#243](https://github.com/crypto-org-chain/cosmos-sdk/pull/243) Support `RunAtomic` API in `Context` to use new CoW branched cache store.
47+
* [#248](https://github.com/crypto-org-chain/cosmos-sdk/pull/248) Init btree store lazily to save allocations when no content insert into it.
4748

4849
## [Unreleased-Upstream]
4950

store/internal/btree/btree.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,38 @@ type BTree[V any] struct {
2828

2929
// NewBTree creates a wrapper around `btree.BTreeG`.
3030
func NewBTree[V any]() BTree[V] {
31-
return BTree[V]{
32-
tree: btree.NewBTreeGOptions(byKeys[V], btree.Options{
33-
Degree: bTreeDegree,
34-
NoLocks: false,
35-
}),
31+
return BTree[V]{}
32+
}
33+
34+
func (bt *BTree[V]) init() {
35+
if bt.tree != nil {
36+
return
3637
}
38+
bt.tree = btree.NewBTreeGOptions(byKeys[V], btree.Options{
39+
Degree: bTreeDegree,
40+
NoLocks: false,
41+
})
3742
}
3843

3944
// Set supports nil as value when used as overlay
40-
func (bt BTree[V]) Set(key []byte, value V) {
45+
func (bt *BTree[V]) Set(key []byte, value V) {
46+
bt.init()
4147
bt.tree.Set(newItem(key, value))
4248
}
4349

4450
func (bt BTree[V]) Get(key []byte) (V, bool) {
51+
if bt.tree == nil {
52+
var zero V
53+
return zero, false
54+
}
4555
i, found := bt.tree.Get(newItemWithKey[V](key))
4656
return i.value, found
4757
}
4858

49-
func (bt BTree[V]) Delete(key []byte) {
59+
func (bt *BTree[V]) Delete(key []byte) {
60+
if bt.tree == nil {
61+
return
62+
}
5063
bt.tree.Delete(newItemWithKey[V](key))
5164
}
5265

@@ -67,16 +80,25 @@ func (bt BTree[V]) ReverseIterator(start, end []byte) (types.GIterator[V], error
6780
// Copy the tree. This is a copy-on-write operation and is very fast because
6881
// it only performs a shadowed copy.
6982
func (bt BTree[V]) Copy() BTree[V] {
83+
if bt.tree == nil {
84+
return BTree[V]{}
85+
}
7086
return BTree[V]{
7187
tree: bt.tree.Copy(),
7288
}
7389
}
7490

7591
func (bt BTree[V]) Clear() {
92+
if bt.tree == nil {
93+
return
94+
}
7695
bt.tree.Clear()
7796
}
7897

7998
func (bt BTree[V]) Scan(cb func(key []byte, value V) bool) {
99+
if bt.tree == nil {
100+
return
101+
}
80102
bt.tree.Scan(func(i item[V]) bool {
81103
return cb(i.key, i.value)
82104
})

store/internal/btree/memiterator.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ type memIterator[V any] struct {
2424
}
2525

2626
func newMemIterator[V any](start, end []byte, items BTree[V], ascending bool) *memIterator[V] {
27+
if items.tree == nil {
28+
return &memIterator[V]{
29+
start: start,
30+
end: end,
31+
ascending: ascending,
32+
valid: false,
33+
}
34+
}
35+
2736
var (
2837
valid bool
2938
empty V

0 commit comments

Comments
 (0)