Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optimize decoding #15

Merged
merged 1 commit into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the only use of MaxLenUvarint63 - should we have the constant be 63, rather than a 7 here and 9 as the constant?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a public constant and useful for checking buffer sizes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(also used by go-msgio, which we really need to kill)

// 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")
}
}
}