Commit bc942823 authored by Shahar Kohanim's avatar Shahar Kohanim

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/21509Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Shahar Kohanim <skohanim@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarDavid Crawshaw <crawshaw@golang.org>
parent a9ea36af
......@@ -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
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment