]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/obj: reuse the varint encoding buffer
authorJosh Bleecher Snyder <josharian@gmail.com>
Mon, 20 Apr 2015 17:20:36 +0000 (10:20 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Tue, 21 Apr 2015 17:20:52 +0000 (17:20 +0000)
This reduces the number of allocations in the compiler
while building the stdlib by 15.66%.

No functional changes. Passes toolstash -cmp.

Change-Id: Ia21b37134a8906a4e23d53fdc15235b4aa7bbb34
Reviewed-on: https://go-review.googlesource.com/9085
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/internal/obj/objfile.go

index 461a926fe774083109e88a9c2bc83fdc2b60c9a2..62426a5d7304d72bae610ebcd95096b2f469bb95 100644 (file)
@@ -463,18 +463,21 @@ func writesym(ctxt *Link, b *Biobuf, s *LSym) {
        }
 }
 
+// Reusable buffer to avoid allocations.
+// This buffer was responsible for 15% of gc's allocations.
+var varintbuf [10]uint8
+
 func wrint(b *Biobuf, sval int64) {
        var v uint64
-       var buf [10]uint8
        uv := (uint64(sval) << 1) ^ uint64(int64(sval>>63))
-       p := buf[:]
+       p := varintbuf[:]
        for v = uv; v >= 0x80; v >>= 7 {
                p[0] = uint8(v | 0x80)
                p = p[1:]
        }
        p[0] = uint8(v)
        p = p[1:]
-       Bwrite(b, buf[:len(buf)-len(p)])
+       Bwrite(b, varintbuf[:len(varintbuf)-len(p)])
 }
 
 func wrstring(b *Biobuf, s string) {