From 2fc9c6028cac5d2efbf7214355fbf747ce8410c4 Mon Sep 17 00:00:00 2001 From: Hellojack <106379370+H1JK@users.noreply.github.com> Date: Fri, 21 Apr 2023 10:47:17 +0800 Subject: [PATCH] Improve UVariantLen --- common/rw/{varinat.go => varint.go} | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) rename common/rw/{varinat.go => varint.go} (77%) diff --git a/common/rw/varinat.go b/common/rw/varint.go similarity index 77% rename from common/rw/varinat.go rename to common/rw/varint.go index 9f1be44..cecd7ec 100644 --- a/common/rw/varinat.go +++ b/common/rw/varint.go @@ -27,12 +27,28 @@ func ReadUVariant(reader io.Reader) (uint64, error) { } func UVariantLen(x uint64) int { - i := 0 - for x >= 0x80 { - x >>= 7 - i++ + switch { + case x < 1<<(7*1): + return 1 + case x < 1<<(7*2): + return 2 + case x < 1<<(7*3): + return 3 + case x < 1<<(7*4): + return 4 + case x < 1<<(7*5): + return 5 + case x < 1<<(7*6): + return 6 + case x < 1<<(7*7): + return 7 + case x < 1<<(7*8): + return 8 + case x < 1<<(7*9): + return 9 + default: + return 10 } - return i + 1 } func WriteUVariant(writer io.Writer, value uint64) error {