Skip to content

Commit

Permalink
updated Node.aminoSize() to encodedSize()
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Jun 28, 2020
1 parent 61433bf commit 659e325
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/node/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ A node's hash is calculated by hashing the height, size, and version of the node
// Writes the node's hash to the given io.Writer. This function expects
// child hashes to be already set.
func (node *Node) writeHashBytes(w io.Writer) error {
err := amino.encodeVarint(w, node.height)
err := encodeVarint(w, node.height)
if err != nil {
return errors.Wrap(err, "writing height")
}
Expand Down
20 changes: 20 additions & 0 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"math/bits"
)

// decodeBytes decodes a varint length-prefixed byte slice, returning it along with the number
Expand Down Expand Up @@ -67,6 +68,11 @@ func encodeBytes(w io.Writer, bz []byte) error {
return err
}

// encodeBytesSize returns the byte size of the given slice including length-prefixing.
func encodeBytesSize(bz []byte) int {
return encodeUvarintSize(uint64(len(bz))) + len(bz)
}

// encodeUvarint writes a varint-encoded unsigned integer to an io.Writer.
func encodeUvarint(w io.Writer, u uint64) error {
var buf [binary.MaxVarintLen64]byte
Expand All @@ -75,10 +81,24 @@ func encodeUvarint(w io.Writer, u uint64) error {
return err
}

// encodeUvarintSize returns the byte size of the given integer as a varint.
func encodeUvarintSize(u uint64) int {
if u == 0 {
return 1
}
return (bits.Len64(u) + 6) / 7
}

// encodeVarint writes a varint-encoded integer to an io.Writer.
func encodeVarint(w io.Writer, i int64) error {
var buf [binary.MaxVarintLen64]byte
n := binary.PutVarint(buf[:], i)
_, err := w.Write(buf[0:n])
return err
}

// encodeVarintSize returns the byte size of the given integer as a varint.
func encodeVarintSize(i int64) int {
var buf [binary.MaxVarintLen64]byte
return binary.PutVarint(buf[:], i)
}
15 changes: 7 additions & 8 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"math"

"github.com/pkg/errors"
amino "github.com/tendermint/go-amino"
)

// Node represents a node in a Tree.
Expand Down Expand Up @@ -347,16 +346,16 @@ func (node *Node) writeHashBytesRecursively(w io.Writer) (hashCount int64, err e
return
}

func (node *Node) aminoSize() int {
func (node *Node) encodedSize() int {
n := 1 +
amino.VarintSize(node.size) +
amino.VarintSize(node.version) +
amino.ByteSliceSize(node.key)
encodeVarintSize(node.size) +
encodeVarintSize(node.version) +
encodeBytesSize(node.key)
if node.isLeaf() {
n += amino.ByteSliceSize(node.value)
n += encodeBytesSize(node.value)
} else {
n += amino.ByteSliceSize(node.leftHash) +
amino.ByteSliceSize(node.rightHash)
n += encodeBytesSize(node.leftHash) +
encodeBytesSize(node.rightHash)
}
return n
}
Expand Down
12 changes: 6 additions & 6 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestNode_aminoSize(t *testing.T) {
func TestNode_encodedSize(t *testing.T) {
node := &Node{
key: randBytes(10),
value: randBytes(10),
Expand All @@ -25,11 +25,11 @@ func TestNode_aminoSize(t *testing.T) {
}

// leaf node
require.Equal(t, 26, node.aminoSize())
require.Equal(t, 26, node.encodedSize())

// non-leaf node
node.height = 1
require.Equal(t, 57, node.aminoSize())
require.Equal(t, 57, node.encodedSize())
}

func TestNode_validate(t *testing.T) {
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestNode_validate(t *testing.T) {
}
}

func BenchmarkNode_aminoSize(b *testing.B) {
func BenchmarkNode_encodedSize(b *testing.B) {
node := &Node{
key: randBytes(25),
value: randBytes(100),
Expand All @@ -93,7 +93,7 @@ func BenchmarkNode_aminoSize(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
node.aminoSize()
node.encodedSize()
}
}

Expand All @@ -120,7 +120,7 @@ func BenchmarkNode_WriteBytes(b *testing.B) {
sub.ReportAllocs()
for i := 0; i < sub.N; i++ {
var buf bytes.Buffer
buf.Grow(node.aminoSize())
buf.Grow(node.encodedSize())
_ = node.writeBytes(&buf)
}
})
Expand Down
2 changes: 1 addition & 1 deletion nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (ndb *nodeDB) SaveNode(node *Node) {

// Save node bytes to db.
var buf bytes.Buffer
buf.Grow(node.aminoSize())
buf.Grow(node.encodedSize())

if err := node.writeBytes(&buf); err != nil {
panic(err)
Expand Down

0 comments on commit 659e325

Please sign in to comment.