diff options
author | Ethan Buchman <ethan@coinculture.info> | 2015-08-07 00:39:07 +0800 |
---|---|---|
committer | Ethan Buchman <ethan@coinculture.info> | 2015-08-07 02:04:16 +0800 |
commit | 46c95940812ccd3f474dc5fab7a5351351e8f105 (patch) | |
tree | 8e240a6caecd342d4298e16856753690c5291b18 /trie/encoding.go | |
parent | 7baa5977c8c4b1608c2a23615143da414a638b5c (diff) | |
download | go-tangerine-46c95940812ccd3f474dc5fab7a5351351e8f105.tar go-tangerine-46c95940812ccd3f474dc5fab7a5351351e8f105.tar.gz go-tangerine-46c95940812ccd3f474dc5fab7a5351351e8f105.tar.bz2 go-tangerine-46c95940812ccd3f474dc5fab7a5351351e8f105.tar.lz go-tangerine-46c95940812ccd3f474dc5fab7a5351351e8f105.tar.xz go-tangerine-46c95940812ccd3f474dc5fab7a5351351e8f105.tar.zst go-tangerine-46c95940812ccd3f474dc5fab7a5351351e8f105.zip |
trie: run codec tests, add benchmarks, faster
Diffstat (limited to 'trie/encoding.go')
-rw-r--r-- | trie/encoding.go | 36 |
1 files changed, 16 insertions, 20 deletions
diff --git a/trie/encoding.go b/trie/encoding.go index 8daf503a3..9c862d78f 100644 --- a/trie/encoding.go +++ b/trie/encoding.go @@ -16,10 +16,6 @@ package trie -import ( - "bytes" -) - func CompactEncode(hexSlice []byte) []byte { terminator := 0 if hexSlice[len(hexSlice)-1] == 16 { @@ -38,12 +34,12 @@ func CompactEncode(hexSlice []byte) []byte { hexSlice = append([]byte{flags, 0}, hexSlice...) } - var buff bytes.Buffer - for i := 0; i < len(hexSlice); i += 2 { - buff.WriteByte(byte(16*hexSlice[i] + hexSlice[i+1])) + l := len(hexSlice) / 2 + var buf = make([]byte, l) + for i := 0; i < l; i++ { + buf[i] = 16*hexSlice[2*i] + hexSlice[2*i+1] } - - return buff.Bytes() + return buf } func CompactDecode(str []byte) []byte { @@ -62,22 +58,22 @@ func CompactDecode(str []byte) []byte { } func CompactHexDecode(str []byte) []byte { - var nibbles []byte - - for _, b := range str { - nibbles = append(nibbles, b/16) - nibbles = append(nibbles, b%16) + l := len(str)*2 + 1 + var nibbles = make([]byte, l) + for i, b := range str { + nibbles[i*2] = b / 16 + nibbles[i*2+1] = b % 16 } - nibbles = append(nibbles, 16) + nibbles[l-1] = 16 return nibbles } -// 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) + l := len(key) / 2 + var res = make([]byte, l) + for i := 0; i < l; i++ { + v1, v0 := key[2*i], key[2*i+1] + res[i] = v1*16 + v0 } return res } |