1
0
mirror of https://github.com/golang/go synced 2024-11-19 07:34:44 -07:00

cmd/link: grow rdBuf lazily

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>
This commit is contained in:
Shahar Kohanim 2016-04-04 22:36:43 +03:00
parent a9ea36afbb
commit bc94282306

View File

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