aboutsummaryrefslogtreecommitdiffstats
path: root/trie/encoding.go
diff options
context:
space:
mode:
authorEthan Buchman <ethan@coinculture.info>2015-08-06 15:11:10 +0800
committerEthan Buchman <ethan@coinculture.info>2015-08-06 15:17:59 +0800
commitc1d516546d221de898ddeb12a77477d992d125df (patch)
tree9f2208127d4325546ac524b9e56ae8cd525abb92 /trie/encoding.go
parent98100f472c3bee473ea32044e47e82835b9cfadb (diff)
downloadgo-tangerine-c1d516546d221de898ddeb12a77477d992d125df.tar
go-tangerine-c1d516546d221de898ddeb12a77477d992d125df.tar.gz
go-tangerine-c1d516546d221de898ddeb12a77477d992d125df.tar.bz2
go-tangerine-c1d516546d221de898ddeb12a77477d992d125df.tar.lz
go-tangerine-c1d516546d221de898ddeb12a77477d992d125df.tar.xz
go-tangerine-c1d516546d221de898ddeb12a77477d992d125df.tar.zst
go-tangerine-c1d516546d221de898ddeb12a77477d992d125df.zip
faster hex-prefix codec and string -> []byte
Diffstat (limited to 'trie/encoding.go')
-rw-r--r--trie/encoding.go43
1 files changed, 17 insertions, 26 deletions
diff --git a/trie/encoding.go b/trie/encoding.go
index 524807f06..8daf503a3 100644
--- a/trie/encoding.go
+++ b/trie/encoding.go
@@ -18,11 +18,9 @@ package trie
import (
"bytes"
- "encoding/hex"
- "strings"
)
-func CompactEncode(hexSlice []byte) string {
+func CompactEncode(hexSlice []byte) []byte {
terminator := 0
if hexSlice[len(hexSlice)-1] == 16 {
terminator = 1
@@ -45,10 +43,10 @@ func CompactEncode(hexSlice []byte) string {
buff.WriteByte(byte(16*hexSlice[i] + hexSlice[i+1]))
}
- return buff.String()
+ return buff.Bytes()
}
-func CompactDecode(str string) []byte {
+func CompactDecode(str []byte) []byte {
base := CompactHexDecode(str)
base = base[:len(base)-1]
if base[0] >= 2 {
@@ -63,30 +61,23 @@ func CompactDecode(str string) []byte {
return base
}
-func CompactHexDecode(str string) []byte {
- base := "0123456789abcdef"
- var hexSlice []byte
+func CompactHexDecode(str []byte) []byte {
+ var nibbles []byte
- enc := hex.EncodeToString([]byte(str))
- for _, v := range enc {
- hexSlice = append(hexSlice, byte(strings.IndexByte(base, byte(v))))
+ for _, b := range str {
+ nibbles = append(nibbles, b/16)
+ nibbles = append(nibbles, b%16)
}
- hexSlice = append(hexSlice, 16)
-
- return hexSlice
+ nibbles = append(nibbles, 16)
+ return nibbles
}
-func DecodeCompact(key []byte) string {
- const base = "0123456789abcdef"
- var str string
-
- for _, v := range key {
- if v < 16 {
- str += string(base[v])
- }
+// assumes key is odd length
+func DecodeCompact(key []byte) []byte {
+ var res []byte
+ for i := 0; i < len(key)-1; i += 2 {
+ v1, v0 := key[i], key[i+1]
+ res = append(res, v1*16+v0)
}
-
- res, _ := hex.DecodeString(str)
-
- return string(res)
+ return res
}