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

perf: use fast unsafe bytes->string convertion #525

Merged
merged 9 commits into from
Aug 3, 2022
Prev Previous commit
Next Next commit
code style
  • Loading branch information
robert-zaremba committed Jul 20, 2022
commit e334dd3f40554f32bebadb43b51bd4762a177bf3
13 changes: 6 additions & 7 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import (
"sync"
"time"

"github.com/cosmos/iavl/internal/logger"
"github.com/pkg/errors"
dbm "github.com/tendermint/tm-db"

ibytes "github.com/cosmos/iavl/internal/bytes"
"github.com/cosmos/iavl/internal/logger"
)

// ErrVersionDoesNotExist is returned if a requested version does not exist.
Expand Down Expand Up @@ -147,7 +146,7 @@ func (tree *MutableTree) Get(key []byte) ([]byte, error) {
return nil, nil
}

if fastNode, ok := tree.unsavedFastNodeAdditions[ibytes.UnsafeBytesToStr(key)]; ok {
if fastNode, ok := tree.unsavedFastNodeAdditions[unsafeToStr(key)]; ok {
return fastNode.value, nil
}

Expand Down Expand Up @@ -916,7 +915,7 @@ func (tree *MutableTree) getUnsavedFastNodeRemovals() map[string]interface{} {
}

func (tree *MutableTree) addUnsavedAddition(key []byte, node *FastNode) {
skey := ibytes.UnsafeBytesToStr(key)
skey := unsafeToStr(key)
delete(tree.unsavedFastNodeRemovals, skey)
tree.unsavedFastNodeAdditions[skey] = node
}
Expand All @@ -937,7 +936,7 @@ func (tree *MutableTree) saveFastNodeAdditions() error {
}

func (tree *MutableTree) addUnsavedRemoval(key []byte) {
skey := ibytes.UnsafeBytesToStr(key)
skey := unsafeToStr(key)
delete(tree.unsavedFastNodeAdditions, skey)
tree.unsavedFastNodeRemovals[skey] = true
}
Expand All @@ -950,7 +949,7 @@ func (tree *MutableTree) saveFastNodeRemovals() error {
sort.Strings(keysToSort)

for _, key := range keysToSort {
if err := tree.ndb.DeleteFastNode(ibytes.UnsafeStrToBytes(key)); err != nil {
if err := tree.ndb.DeleteFastNode(unsafeToBz(key)); err != nil {
return err
}
}
Expand Down Expand Up @@ -1230,7 +1229,7 @@ func (tree *MutableTree) addOrphans(orphans []*Node) error {
if len(node.hash) == 0 {
return fmt.Errorf("expected to find node hash, but was empty")
}
tree.orphans[ibytes.UnsafeBytesToStr(node.hash)] = node.version
tree.orphans[unsafeToStr(node.hash)] = node.version
}
return nil
}
19 changes: 9 additions & 10 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/pkg/errors"
dbm "github.com/tendermint/tm-db"

ibytes "github.com/cosmos/iavl/internal/bytes"
"github.com/cosmos/iavl/internal/logger"
)

Expand Down Expand Up @@ -93,7 +92,7 @@ func newNodeDB(db dbm.DB, cacheSize int, opts *Options) *nodeDB {
opts = &o
}

storeVersion, err := db.Get(metadataKeyFormat.Key(ibytes.UnsafeStrToBytes(storageVersionKey)))
storeVersion, err := db.Get(metadataKeyFormat.Key(unsafeToBz(storageVersionKey)))

if err != nil || storeVersion == nil {
storeVersion = []byte(defaultStorageVersionValue)
Expand Down Expand Up @@ -126,7 +125,7 @@ func (ndb *nodeDB) GetNode(hash []byte) (*Node, error) {
}

// Check the cache.
if elem, ok := ndb.nodeCache[ibytes.UnsafeBytesToStr(hash)]; ok {
if elem, ok := ndb.nodeCache[unsafeToStr(hash)]; ok {
if ndb.opts.Stat != nil {
ndb.opts.Stat.IncCacheHitCnt()
}
Expand Down Expand Up @@ -173,7 +172,7 @@ func (ndb *nodeDB) GetFastNode(key []byte) (*FastNode, error) {
}

// Check the cache.
if elem, ok := ndb.fastNodeCache[ibytes.UnsafeBytesToStr(key)]; ok {
if elem, ok := ndb.fastNodeCache[unsafeToStr(key)]; ok {
if ndb.opts.Stat != nil {
ndb.opts.Stat.IncFastCacheHitCnt()
}
Expand Down Expand Up @@ -887,7 +886,7 @@ func (ndb *nodeDB) getFastIterator(start, end []byte, ascending bool) (dbm.Itera
}

func (ndb *nodeDB) uncacheNode(hash []byte) {
key := ibytes.UnsafeBytesToStr(hash)
key := unsafeToStr(hash)
if elem, ok := ndb.nodeCache[key]; ok {
ndb.nodeCacheQueue.Remove(elem)
delete(ndb.nodeCache, key)
Expand All @@ -898,17 +897,17 @@ func (ndb *nodeDB) uncacheNode(hash []byte) {
// reached the cache size limit.
func (ndb *nodeDB) cacheNode(node *Node) {
elem := ndb.nodeCacheQueue.PushBack(node)
ndb.nodeCache[ibytes.UnsafeBytesToStr(node.hash)] = elem
ndb.nodeCache[unsafeToStr(node.hash)] = elem

if ndb.nodeCacheQueue.Len() > ndb.nodeCacheSize {
oldest := ndb.nodeCacheQueue.Front()
hash := ndb.nodeCacheQueue.Remove(oldest).(*Node).hash
delete(ndb.nodeCache, ibytes.UnsafeBytesToStr(hash))
delete(ndb.nodeCache, unsafeToStr(hash))
}
}

func (ndb *nodeDB) uncacheFastNode(key []byte) {
skey := ibytes.UnsafeBytesToStr(key)
skey := unsafeToStr(key)
if elem, ok := ndb.fastNodeCache[skey]; ok {
ndb.fastNodeCacheQueue.Remove(elem)
delete(ndb.fastNodeCache, skey)
Expand All @@ -919,12 +918,12 @@ func (ndb *nodeDB) uncacheFastNode(key []byte) {
// reached the cache size limit.
func (ndb *nodeDB) cacheFastNode(node *FastNode) {
elem := ndb.fastNodeCacheQueue.PushBack(node)
ndb.fastNodeCache[ibytes.UnsafeBytesToStr(node.key)] = elem
ndb.fastNodeCache[unsafeToStr(node.key)] = elem

if ndb.fastNodeCacheQueue.Len() > ndb.fastNodeCacheSize {
oldest := ndb.fastNodeCacheQueue.Front()
key := ndb.fastNodeCacheQueue.Remove(oldest).(*FastNode).key
delete(ndb.fastNodeCache, ibytes.UnsafeBytesToStr(key))
delete(ndb.fastNodeCache, unsafeToStr(key))
}
}

Expand Down
6 changes: 2 additions & 4 deletions tree_dotgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"io"
"text/template"

ibytes "github.com/cosmos/iavl/internal/bytes"
)

type graphEdge struct {
Expand Down Expand Up @@ -58,12 +56,12 @@ func WriteDOTGraph(w io.Writer, tree *ImmutableTree, paths []PathToLeaf) {
}
shortHash := graphNode.Hash[:7]

graphNode.Label = mkLabel(ibytes.UnsafeBytesToStr(node.key), 16, "sans-serif")
graphNode.Label = mkLabel(unsafeToStr(node.key), 16, "sans-serif")
graphNode.Label += mkLabel(shortHash, 10, "monospace")
graphNode.Label += mkLabel(fmt.Sprintf("version=%d", node.version), 10, "monospace")

if node.value != nil {
graphNode.Label += mkLabel(ibytes.UnsafeBytesToStr(node.value), 10, "sans-serif")
graphNode.Label += mkLabel(unsafeToStr(node.value), 10, "sans-serif")
}

if node.height == 0 {
Expand Down
8 changes: 8 additions & 0 deletions unsafe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package iavl

import ibytes "github.com/cosmos/iavl/internal/bytes"

var (
unsafeToStr = ibytes.UnsafeBytesToStr
unsafeToBz = ibytes.UnsafeStrToBytes
)
6 changes: 2 additions & 4 deletions unsaved_fast_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"sort"

dbm "github.com/tendermint/tm-db"

ibytes "github.com/cosmos/iavl/internal/bytes"
)

var (
Expand Down Expand Up @@ -63,7 +61,7 @@ func NewUnsavedFastIterator(start, end []byte, ascending bool, ndb *nodeDB, unsa
continue
}

iter.unsavedFastNodesToSort = append(iter.unsavedFastNodesToSort, ibytes.UnsafeBytesToStr(fastNode.key))
iter.unsavedFastNodesToSort = append(iter.unsavedFastNodesToSort, unsafeToStr(fastNode.key))
}

sort.Slice(iter.unsavedFastNodesToSort, func(i, j int) bool {
Expand Down Expand Up @@ -134,7 +132,7 @@ func (iter *UnsavedFastIterator) Next() {
return
}

diskKeyStr := ibytes.UnsafeBytesToStr(iter.fastIterator.Key())
diskKeyStr := unsafeToStr(iter.fastIterator.Key())
if iter.fastIterator.Valid() && iter.nextUnsavedNodeIdx < len(iter.unsavedFastNodesToSort) {

if iter.unsavedFastNodeRemovals[diskKeyStr] != nil {
Expand Down