Skip to content

Commit

Permalink
feat: save some memory allocations on un-used cache store
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Apr 2, 2024
1 parent ec314e8 commit 9a96a0e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (bank) [#237](https://github.com/crypto-org-chain/cosmos-sdk/pull/237) Support virtual accounts in sending coins.
* (x/bank) [#239](https://github.com/crypto-org-chain/cosmos-sdk/pull/239) Add low level `AddBalance`,`SubBalance` APIs to bank keeper.
* [#243](https://github.com/crypto-org-chain/cosmos-sdk/pull/243) Support `RunAtomic` API in `Context` to use new CoW branched cache store.
* [#244](https://github.com/crypto-org-chain/cosmos-sdk/pull/244) Init btree store lazily to save allocations when no content insert into it.

## [Unreleased-Upstream]

Expand Down
34 changes: 27 additions & 7 deletions store/internal/btree/btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,36 @@ type BTree[V any] struct {

// NewBTree creates a wrapper around `btree.BTreeG`.
func NewBTree[V any]() BTree[V] {
return BTree[V]{
tree: btree.NewBTreeGOptions(byKeys[V], btree.Options{
Degree: bTreeDegree,
NoLocks: false,
}),
return BTree[V]{}
}

func (bt *BTree[V]) init() {
if bt.tree != nil {
return
}
bt.tree = btree.NewBTreeGOptions(byKeys[V], btree.Options{
Degree: bTreeDegree,
NoLocks: false,
})
}

// Set supports nil as value when used as overlay
func (bt BTree[V]) Set(key []byte, value V) {
func (bt *BTree[V]) Set(key []byte, value V) {
bt.init()
bt.tree.Set(newItem(key, value))
}

func (bt BTree[V]) Get(key []byte) (V, bool) {
if bt.tree == nil {
var zero V
return zero, false
}
i, found := bt.tree.Get(newItemWithKey[V](key))
return i.value, found
}

func (bt BTree[V]) Delete(key []byte) {
func (bt *BTree[V]) Delete(key []byte) {
bt.init()
bt.tree.Delete(newItemWithKey[V](key))
}

Expand All @@ -67,16 +78,25 @@ func (bt BTree[V]) ReverseIterator(start, end []byte) (types.GIterator[V], error
// Copy the tree. This is a copy-on-write operation and is very fast because
// it only performs a shadowed copy.
func (bt BTree[V]) Copy() BTree[V] {
if bt.tree == nil {
return BTree[V]{}
}
return BTree[V]{
tree: bt.tree.Copy(),
}
}

func (bt BTree[V]) Clear() {
if bt.tree == nil {
return
}
bt.tree.Clear()
}

func (bt BTree[V]) Scan(cb func(key []byte, value V) bool) {
if bt.tree == nil {
return
}
bt.tree.Scan(func(i item[V]) bool {
return cb(i.key, i.value)
})
Expand Down
9 changes: 9 additions & 0 deletions store/internal/btree/memiterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ type memIterator[V any] struct {
}

func newMemIterator[V any](start, end []byte, items BTree[V], ascending bool) *memIterator[V] {
if items.tree == nil {
return &memIterator[V]{
start: start,
end: end,
ascending: ascending,
valid: false,
}
}

var (
valid bool
empty V
Expand Down

0 comments on commit 9a96a0e

Please sign in to comment.