Skip to content

Commit c1d5165

Browse files
committed
faster hex-prefix codec and string -> []byte
1 parent 98100f4 commit c1d5165

5 files changed

Lines changed: 30 additions & 39 deletions

File tree

‎trie/encoding.go‎

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ package trie
1818

1919
import (
2020
"bytes"
21-
"encoding/hex"
22-
"strings"
2321
)
2422

25-
func CompactEncode(hexSlice []byte) string {
23+
func CompactEncode(hexSlice []byte) []byte {
2624
terminator := 0
2725
if hexSlice[len(hexSlice)-1] == 16 {
2826
terminator = 1
@@ -45,10 +43,10 @@ func CompactEncode(hexSlice []byte) string {
4543
buff.WriteByte(byte(16*hexSlice[i] + hexSlice[i+1]))
4644
}
4745

48-
return buff.String()
46+
return buff.Bytes()
4947
}
5048

51-
func CompactDecode(str string) []byte {
49+
func CompactDecode(str []byte) []byte {
5250
base := CompactHexDecode(str)
5351
base = base[:len(base)-1]
5452
if base[0] >= 2 {
@@ -63,30 +61,23 @@ func CompactDecode(str string) []byte {
6361
return base
6462
}
6563

66-
func CompactHexDecode(str string) []byte {
67-
base := "0123456789abcdef"
68-
var hexSlice []byte
64+
func CompactHexDecode(str []byte) []byte {
65+
var nibbles []byte
6966

70-
enc := hex.EncodeToString([]byte(str))
71-
for _, v := range enc {
72-
hexSlice = append(hexSlice, byte(strings.IndexByte(base, byte(v))))
67+
for _, b := range str {
68+
nibbles = append(nibbles, b/16)
69+
nibbles = append(nibbles, b%16)
7370
}
74-
hexSlice = append(hexSlice, 16)
75-
76-
return hexSlice
71+
nibbles = append(nibbles, 16)
72+
return nibbles
7773
}
7874

79-
func DecodeCompact(key []byte) string {
80-
const base = "0123456789abcdef"
81-
var str string
82-
83-
for _, v := range key {
84-
if v < 16 {
85-
str += string(base[v])
86-
}
75+
// assumes key is odd length
76+
func DecodeCompact(key []byte) []byte {
77+
var res []byte
78+
for i := 0; i < len(key)-1; i += 2 {
79+
v1, v0 := key[i], key[i+1]
80+
res = append(res, v1*16+v0)
8781
}
88-
89-
res, _ := hex.DecodeString(str)
90-
91-
return string(res)
82+
return res
9283
}

‎trie/encoding_test.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,28 @@ func (s *TrieEncodingSuite) TestCompactEncode(c *checker.C) {
4848

4949
func (s *TrieEncodingSuite) TestCompactHexDecode(c *checker.C) {
5050
exp := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
51-
res := CompactHexDecode("verb")
51+
res := CompactHexDecode([]byte("verb"))
5252
c.Assert(res, checker.DeepEquals, exp)
5353
}
5454

5555
func (s *TrieEncodingSuite) TestCompactDecode(c *checker.C) {
5656
// odd compact decode
5757
exp := []byte{1, 2, 3, 4, 5}
58-
res := CompactDecode("\x11\x23\x45")
58+
res := CompactDecode([]byte("\x11\x23\x45"))
5959
c.Assert(res, checker.DeepEquals, exp)
6060

6161
// even compact decode
6262
exp = []byte{0, 1, 2, 3, 4, 5}
63-
res = CompactDecode("\x00\x01\x23\x45")
63+
res = CompactDecode([]byte("\x00\x01\x23\x45"))
6464
c.Assert(res, checker.DeepEquals, exp)
6565

6666
// even terminated compact decode
6767
exp = []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
68-
res = CompactDecode("\x20\x0f\x1c\xb8")
68+
res = CompactDecode([]byte("\x20\x0f\x1c\xb8"))
6969
c.Assert(res, checker.DeepEquals, exp)
7070

7171
// even terminated compact decode
7272
exp = []byte{15, 1, 12, 11, 8 /*term*/, 16}
73-
res = CompactDecode("\x3f\x1c\xb8")
73+
res = CompactDecode([]byte("\x3f\x1c\xb8"))
7474
c.Assert(res, checker.DeepEquals, exp)
7575
}

‎trie/iterator.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (self *Iterator) Next() bool {
4141
self.Key = make([]byte, 32)
4242
}
4343

44-
key := RemTerm(CompactHexDecode(string(self.Key)))
44+
key := RemTerm(CompactHexDecode(self.Key))
4545
k := self.next(self.trie.root, key, isIterStart)
4646

4747
self.Key = []byte(DecodeCompact(k))

‎trie/shortnode.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type ShortNode struct {
2626
}
2727

2828
func NewShortNode(t *Trie, key []byte, value Node) *ShortNode {
29-
return &ShortNode{t, []byte(CompactEncode(key)), value, false}
29+
return &ShortNode{t, CompactEncode(key), value, false}
3030
}
3131
func (self *ShortNode) Value() Node {
3232
self.value = self.trie.trans(self.value)
@@ -49,7 +49,7 @@ func (self *ShortNode) Hash() interface{} {
4949
}
5050

5151
func (self *ShortNode) Key() []byte {
52-
return CompactDecode(string(self.key))
52+
return CompactDecode(self.key)
5353
}
5454

5555
func (self *ShortNode) setDirty(dirty bool) {

‎trie/trie.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (self *Trie) Iterator() *Iterator {
6969

7070
func (self *Trie) Copy() *Trie {
7171
cpy := make([]byte, 32)
72-
copy(cpy, self.roothash)
72+
copy(cpy, self.roothash) // NOTE: cpy isn't being used anywhere?
7373
trie := New(nil, nil)
7474
trie.cache = self.cache.Copy()
7575
if self.root != nil {
@@ -131,7 +131,7 @@ func (self *Trie) Update(key, value []byte) Node {
131131
self.mu.Lock()
132132
defer self.mu.Unlock()
133133

134-
k := CompactHexDecode(string(key))
134+
k := CompactHexDecode(key)
135135

136136
if len(value) != 0 {
137137
node := NewValueNode(self, value)
@@ -149,7 +149,7 @@ func (self *Trie) Get(key []byte) []byte {
149149
self.mu.Lock()
150150
defer self.mu.Unlock()
151151

152-
k := CompactHexDecode(string(key))
152+
k := CompactHexDecode(key)
153153

154154
n := self.get(self.root, k)
155155
if n != nil {
@@ -164,7 +164,7 @@ func (self *Trie) Delete(key []byte) Node {
164164
self.mu.Lock()
165165
defer self.mu.Unlock()
166166

167-
k := CompactHexDecode(string(key))
167+
k := CompactHexDecode(key)
168168
self.root = self.delete(self.root, k)
169169

170170
return self.root
@@ -336,7 +336,7 @@ func (self *Trie) mknode(value *common.Value) Node {
336336
case 2:
337337
// A value node may consists of 2 bytes.
338338
if value.Get(0).Len() != 0 {
339-
key := CompactDecode(string(value.Get(0).Bytes()))
339+
key := CompactDecode(value.Get(0).Bytes())
340340
if key[len(key)-1] == 16 {
341341
return NewShortNode(self, key, NewValueNode(self, value.Get(1).Bytes()))
342342
} else {

0 commit comments

Comments
 (0)