Skip to content

Commit 8524369

Browse files
committed
perf: fix accidentally undone Go map[string(byteslice)] lookup optimization
This change brings back an optimization that go innocently removed while trying to reduce the number of invocations to node.GetKey() in PR cosmos#890. The Go compiler considers the special case of lookups per: value, ok := map[string(byteslice)] or value := map[string(byteslice)] and performs a zero byteslice->string conversion because it knows that there is no storing in the map hence a read-only usage of the byteslice's memory.
1 parent 0d441f5 commit 8524369

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

cache/cache.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,16 @@ func New(maxElementCount int) Cache {
6161
}
6262

6363
func (c *lruCache) Add(node Node) Node {
64-
key := string(node.GetKey())
65-
if e, exists := c.dict[key]; exists {
64+
key := node.GetKey()
65+
if e, exists := c.dict[string(key)]; exists {
6666
c.ll.MoveToFront(e)
6767
old := e.Value
6868
e.Value = node
6969
return old.(Node)
7070
}
7171

7272
elem := c.ll.PushFront(node)
73-
c.dict[key] = elem
73+
c.dict[string(key)] = elem
7474

7575
if c.ll.Len() > c.maxElementCount {
7676
oldest := c.ll.Back()
@@ -97,9 +97,8 @@ func (c *lruCache) Len() int {
9797
}
9898

9999
func (c *lruCache) Remove(key []byte) Node {
100-
keyS := string(key)
101-
if elem, exists := c.dict[keyS]; exists {
102-
return c.removeWithKey(elem, keyS)
100+
if elem, exists := c.dict[string(key)]; exists {
101+
return c.removeWithKey(elem, string(key))
103102
}
104103
return nil
105104
}

0 commit comments

Comments
 (0)