]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime: speed up readvarintUnsafe
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Mon, 16 Oct 2023 16:07:49 +0000 (23:07 +0700)
committerGopher Robot <gobot@golang.org>
Wed, 18 Oct 2023 16:32:15 +0000 (16:32 +0000)
The documentation of readvarintUnsafe claims itself and readvarint are
duplicated. However, two implementation are not in synced, since when
readvarint got some minor improvements in CL 43150.

Updating readvarintUnsafe to match readvarint implementation to gain a
bit of speed. While at it, also updating its documentation to clarify
the main difference.

name                    time/op
ReadvarintUnsafe/old-8  6.04ns ± 2%
ReadvarintUnsafe/new-8  5.31ns ± 3%

Change-Id: Ie1805d0747544f69de88f6ba9d1b3960f80f00e8
Reviewed-on: https://go-review.googlesource.com/c/go/+/535815
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/runtime/panic.go

index 93f03400a5d2e5a441aac51b4a1b351d7e43c38f..5f54ee4b012d7bd145f82f46edcf278fc23aad8d 100644 (file)
@@ -670,10 +670,8 @@ func printpanics(p *_panic) {
 // readvarintUnsafe reads the uint32 in varint format starting at fd, and returns the
 // uint32 and a pointer to the byte following the varint.
 //
-// There is a similar function runtime.readvarint, which takes a slice of bytes,
-// rather than an unsafe pointer. These functions are duplicated, because one of
-// the two use cases for the functions would get slower if the functions were
-// combined.
+// The implementation is the same with runtime.readvarint, except that this function
+// uses unsafe.Pointer for speed.
 func readvarintUnsafe(fd unsafe.Pointer) (uint32, unsafe.Pointer) {
        var r uint32
        var shift int
@@ -683,7 +681,7 @@ func readvarintUnsafe(fd unsafe.Pointer) (uint32, unsafe.Pointer) {
                if b < 128 {
                        return r + uint32(b)<<shift, fd
                }
-               r += ((uint32(b) &^ 128) << shift)
+               r += uint32(b&0x7F) << (shift & 31)
                shift += 7
                if shift > 28 {
                        panic("Bad varint")