Skip to content

Commit

Permalink
feat: optimize decoding (#15)
Browse files Browse the repository at this point in the history
This speeds up decoding by about 20%. Suggested by @jwinkler2083233 in

Fixes #14.
  • Loading branch information
Stebalien authored Nov 23, 2022
1 parent 881f9a5 commit df67645
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
7 changes: 3 additions & 4 deletions varint.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ func ReadUvarint(r io.ByteReader) (uint64, error) {
// released under the BSD License.
var x uint64
var s uint
for i := 0; ; i++ {
for s = 0; ; s += 7 {
b, err := r.ReadByte()
if err != nil {
if err == io.EOF && i != 0 {
if err == io.EOF && s != 0 {
// "eof" will look like a success.
// If we've read part of a value, this is not a
// success.
err = io.ErrUnexpectedEOF
}
return 0, err
}
if (i == 8 && b >= 0x80) || i >= MaxLenUvarint63 {
if (s == 56 && b >= 0x80) || s >= (7*MaxLenUvarint63) {
// this is the 9th and last byte we're willing to read, but it
// signals there's more (1 in MSB).
// or this is the >= 10th byte, and for some reason we're still here.
Expand All @@ -100,7 +100,6 @@ func ReadUvarint(r io.ByteReader) (uint64, error) {
return x | uint64(b)<<s, nil
}
x |= uint64(b&0x7f) << s
s += 7
}
}

Expand Down
25 changes: 25 additions & 0 deletions varint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,28 @@ func TestUnexpectedEOF(t *testing.T) {
t.Error("expected n = 0")
}
}

func BenchmarkReadUvarint(t *testing.B) {
var expected uint64 = 0xffff12
reader := bytes.NewReader(ToUvarint(expected))
t.ResetTimer()
for i := 0; i < t.N; i++ {
result, _ := ReadUvarint(reader)
if result != expected {
t.Fatal("invalid result")
}
reader.Seek(0, 0)
}
}

func BenchmarkFromUvarint(t *testing.B) {
var expected uint64 = 0xffff12
uvarint := ToUvarint(expected)
t.ResetTimer()
for i := 0; i < t.N; i++ {
result, _, _ := FromUvarint(uvarint)
if result != expected {
t.Fatal("invalid result")
}
}
}

0 comments on commit df67645

Please sign in to comment.