]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: grow rdBuf lazily
authorShahar Kohanim <skohanim@gmail.com>
Mon, 4 Apr 2016 19:36:43 +0000 (22:36 +0300)
committerShahar Kohanim <skohanim@gmail.com>
Mon, 4 Apr 2016 21:22:44 +0000 (21:22 +0000)
Counting the final buffer size usually doesn't result in the buffer growing,
so assume that it doesn't need to grow and only grow if necessary.

name       old secs    new secs    delta
LinkCmdGo   0.49 ± 4%   0.48 ± 3%  -1.31%   (p=0.000 n=95+95)

name       old MaxRSS  new MaxRSS  delta
LinkCmdGo   122k ± 4%   121k ± 5%    ~     (p=0.065 n=96+100)

Change-Id: I85e7f5688a61ef5ef2b1b7afe56507e71c5bd5b1
Reviewed-on: https://go-review.googlesource.com/21509
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Shahar Kohanim <skohanim@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
src/cmd/link/internal/ld/objfile.go

index b81dec6fd3765b34f0fd76392f761fae286f73d3..8a406d17a6515250aea21abe8a8ec86ddd417b4c 100644 (file)
@@ -507,8 +507,8 @@ func (r *objReader) readUint8() uint8 {
 
 func (r *objReader) readString() string {
        n := r.readInt()
-       if len(r.rdBuf) < n {
-               r.rdBuf = make([]byte, n)
+       if cap(r.rdBuf) < n {
+               r.rdBuf = make([]byte, 2*n)
        }
        r.readFull(r.rdBuf[:n])
        return string(r.rdBuf[:n])
@@ -533,11 +533,8 @@ func (r *objReader) readSymName() string {
        if err != nil {
                log.Fatalf("%s: unexpectedly long symbol name", r.pn)
        }
-       // Calculate needed scratch space, accounting for the growth
-       // of all the `"".` substrings to pkg+".":
-       need := len(origName) + maxInt(0, bytes.Count(origName, emptyPkg)*(len(pkg)+len(".")-len(emptyPkg)))
-       if len(r.rdBuf) < need {
-               r.rdBuf = make([]byte, need)
+       if cap(r.rdBuf) < n {
+               r.rdBuf = make([]byte, 2*n)
        }
        adjName := r.rdBuf[:0]
        for {
@@ -548,6 +545,7 @@ func (r *objReader) readSymName() string {
                        // using the rfBuf (also no longer used) as the scratch space.
                        // TODO: use bufio.Reader.Discard if available instead?
                        r.readFull(r.rdBuf[:n])
+                       r.rdBuf = adjName[:0] // in case 2*n wasn't enough
                        return s
                }
                adjName = append(adjName, origName[:i]...)
@@ -562,10 +560,3 @@ func (r *objReader) readSymIndex() *LSym {
        i := r.readInt()
        return r.refs[i]
 }
-
-func maxInt(a, b int) int {
-       if a > b {
-               return a
-       }
-       return b
-}