From df6764553d837ee21946775169119d24342ced99 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 22 Nov 2022 16:19:00 -0800 Subject: [PATCH] feat: optimize decoding (#15) This speeds up decoding by about 20%. Suggested by @jwinkler2083233 in Fixes #14. --- varint.go | 7 +++---- varint_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/varint.go b/varint.go index 47340d9..f0e85d7 100644 --- a/varint.go +++ b/varint.go @@ -76,10 +76,10 @@ 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. @@ -87,7 +87,7 @@ func ReadUvarint(r io.ByteReader) (uint64, error) { } 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. @@ -100,7 +100,6 @@ func ReadUvarint(r io.ByteReader) (uint64, error) { return x | uint64(b)<