From bc942823069fd004166360164e13821ded38f11c Mon Sep 17 00:00:00 2001 From: Shahar Kohanim Date: Mon, 4 Apr 2016 22:36:43 +0300 Subject: [PATCH] cmd/link: grow rdBuf lazily MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 Run-TryBot: Shahar Kohanim TryBot-Result: Gobot Gobot Reviewed-by: David Crawshaw --- src/cmd/link/internal/ld/objfile.go | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/cmd/link/internal/ld/objfile.go b/src/cmd/link/internal/ld/objfile.go index b81dec6fd3..8a406d17a6 100644 --- a/src/cmd/link/internal/ld/objfile.go +++ b/src/cmd/link/internal/ld/objfile.go @@ -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 -} -- 2.48.1