Skip to content

Commit

Permalink
perf: limit use of unsafe UnsafeBytesToStr
Browse files Browse the repository at this point in the history
Unsafe []byte to string conversion is not required for map lookups,
because the Go compiler does the conversion in a garbage-free way.

Limit the scope of the unsafe strings in the few places it is needed
(map deletion).

WriteDOTGraph doesn't seem performance sensitive, so tolerate the extra
garbage there.
  • Loading branch information
elias-orijtech authored and tac0turtle committed May 4, 2023
1 parent 17323ba commit 6aa391f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
11 changes: 5 additions & 6 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,15 @@ func New(maxElementCount int) Cache {
}

func (c *lruCache) Add(node Node) Node {
keyStr := ibytes.UnsafeBytesToStr(node.GetKey())
if e, exists := c.dict[keyStr]; exists {
if e, exists := c.dict[string(node.GetKey())]; exists {
c.ll.MoveToFront(e)
old := e.Value
e.Value = node
return old.(Node)
}

elem := c.ll.PushFront(node)
c.dict[keyStr] = elem
c.dict[string(node.GetKey())] = elem

if c.ll.Len() > c.maxElementCount {
oldest := c.ll.Back()
Expand All @@ -80,15 +79,15 @@ func (c *lruCache) Add(node Node) Node {
}

func (c *lruCache) Get(key []byte) Node {
if ele, hit := c.dict[ibytes.UnsafeBytesToStr(key)]; hit {
if ele, hit := c.dict[string(key)]; hit {
c.ll.MoveToFront(ele)
return ele.Value.(Node)
}
return nil
}

func (c *lruCache) Has(key []byte) bool {
_, exists := c.dict[ibytes.UnsafeBytesToStr(key)]
_, exists := c.dict[string(key)]
return exists
}

Expand All @@ -97,7 +96,7 @@ func (c *lruCache) Len() int {
}

func (c *lruCache) Remove(key []byte) Node {
if elem, exists := c.dict[ibytes.UnsafeBytesToStr(key)]; exists {
if elem, exists := c.dict[string(key)]; exists {
return c.remove(elem)
}
return nil
Expand Down
5 changes: 2 additions & 3 deletions cmd/iaviewer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
dbm "github.com/cosmos/cosmos-db"

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

// TODO: make this configurable?
Expand Down Expand Up @@ -102,8 +101,8 @@ func PrintDBStats(db dbm.DB) {

defer itr.Close()
for ; itr.Valid(); itr.Next() {
key := ibytes.UnsafeBytesToStr(itr.Key()[:1])
prefix[key]++
key := itr.Key()[:1]
prefix[string(key)]++
count++
}
if err := itr.Error(); err != nil {
Expand Down
7 changes: 3 additions & 4 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (tree *MutableTree) Get(key []byte) ([]byte, error) {
}

if !tree.skipFastStorageUpgrade {
if fastNode, ok := tree.unsavedFastNodeAdditions[ibytes.UnsafeBytesToStr(key)]; ok {
if fastNode, ok := tree.unsavedFastNodeAdditions[string(key)]; ok {
return fastNode.GetValue(), nil
}
// check if node was deleted
Expand Down Expand Up @@ -770,9 +770,8 @@ func (tree *MutableTree) getUnsavedFastNodeRemovals() map[string]interface{} {
}

func (tree *MutableTree) addUnsavedAddition(key []byte, node *fastnode.Node) {
skey := ibytes.UnsafeBytesToStr(key)
delete(tree.unsavedFastNodeRemovals, skey)
tree.unsavedFastNodeAdditions[skey] = node
delete(tree.unsavedFastNodeRemovals, ibytes.UnsafeBytesToStr(key))
tree.unsavedFastNodeAdditions[string(key)] = node
}

func (tree *MutableTree) saveFastNodeAdditions(batchCommmit bool) error {
Expand Down
5 changes: 2 additions & 3 deletions tree_dotgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"text/template"

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

Expand Down Expand Up @@ -61,12 +60,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(string(node.key), 16, "sans-serif")
graphNode.Label += mkLabel(shortHash, 10, "monospace")
graphNode.Label += mkLabel(fmt.Sprintf("nodeKey=%v", node.nodeKey), 10, "monospace")

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

if node.subtreeHeight == 0 {
Expand Down

0 comments on commit 6aa391f

Please sign in to comment.