Skip to content

Commit

Permalink
trie: implements vlc coder
Browse files Browse the repository at this point in the history
  • Loading branch information
qianbin committed Dec 30, 2023
1 parent e5d353f commit 405f7e3
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions trie/vlc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package trie

import "encoding/binary"

type vlcScope struct{}

// vlc implements variable length coding.
//
// It's much simpler and a bit faster than RLP.
// Trie nodes stored in database are encoded using VLC.
var vlc vlcScope

// AppendUint appends VLC-encoded i to buf and returns the extended buffer.
func (vlcScope) AppendUint(buf []byte, i uint64) []byte {
return binary.AppendUvarint(buf, i)
}

// AppendString appends RLP-encoded str to buf and returns the extended buffer.
func (vlcScope) AppendString(buf, str []byte) []byte {
buf = binary.AppendUvarint(buf, uint64(len(str)))
return append(buf, str...)
}

// SplitString extracts a string and returns rest bytes.
// It'll panic if errored.
func (vlcScope) SplitString(buf []byte) (str []byte, rest []byte) {
size, n := binary.Uvarint(buf)
buf = buf[n:]
return buf[:size], buf[size:]
}

// SplitUint extracts uint64 and returns rest bytes.
// It'll panic if errored.
func (vlcScope) SplitUint(buf []byte) (uint64, []byte) {
v, n := binary.Uvarint(buf)
return v, buf[n:]
}

0 comments on commit 405f7e3

Please sign in to comment.