]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: track offset instead of using seek
authorDavid Crawshaw <crawshaw@golang.org>
Mon, 29 Feb 2016 17:00:46 +0000 (08:00 -0900)
committerDavid Crawshaw <crawshaw@golang.org>
Mon, 29 Feb 2016 19:14:20 +0000 (19:14 +0000)
The Cpos function is used frequently (at least once per symbol) and
it is implemented with the seek syscall. Instead, track current
output offset and use it.

Building the godoc binary with DWARF, best of ten:

tip:  real 0m1.287s user 0m1.573s
this: real 0m1.208s user 0m1.555s

Change-Id: I068148695cd6b4d32cd145db25e59e6f6bae6945
Reviewed-on: https://go-review.googlesource.com/20055
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/link/internal/ld/lib.go

index 461ebf8db1fd15bfdb61b805a1fdd24b9fc62673..93b2dab30471d52f4f7611851723fcf503efe0e9 100644 (file)
@@ -250,11 +250,26 @@ var (
        Bso obj.Biobuf
 )
 
-var coutbuf struct {
-       *bufio.Writer
-       f *os.File
+type outBuf struct {
+       w   *bufio.Writer
+       f   *os.File
+       off int64
 }
 
+func (w *outBuf) Write(p []byte) (n int, err error) {
+       n, err = w.w.Write(p)
+       w.off += int64(n)
+       return n, err
+}
+
+func (w *outBuf) WriteString(s string) (n int, err error) {
+       n, err = coutbuf.w.WriteString(s)
+       w.off += int64(n)
+       return n, err
+}
+
+var coutbuf outBuf
+
 const pkgname = "__.PKGDEF"
 
 var (
@@ -396,7 +411,7 @@ func libinit() {
                Exitf("cannot create %s: %v", outfile, err)
        }
 
-       coutbuf.Writer = bufio.NewWriter(f)
+       coutbuf.w = bufio.NewWriter(f)
        coutbuf.f = f
 
        if INITENTRY == "" {
@@ -759,9 +774,7 @@ func objfile(lib *Library) {
                fmt.Fprintf(&Bso, "%5.2f ldobj: %s (%s)\n", obj.Cputime(), lib.File, pkg)
        }
        Bso.Flush()
-       var err error
-       var f *obj.Biobuf
-       f, err = obj.Bopenr(lib.File)
+       f, err := obj.Bopenr(lib.File)
        if err != nil {
                Exitf("cannot open file %s: %v", lib.File, err)
        }
@@ -953,7 +966,7 @@ func hostlinksetup() {
                Exitf("cannot create %s: %v", p, err)
        }
 
-       coutbuf.Writer = bufio.NewWriter(f)
+       coutbuf.w = bufio.NewWriter(f)
        coutbuf.f = f
 }
 
@@ -1817,24 +1830,24 @@ func stkprint(ch *Chain, limit int) {
 }
 
 func Cflush() {
-       if err := coutbuf.Writer.Flush(); err != nil {
+       if err := coutbuf.w.Flush(); err != nil {
                Exitf("flushing %s: %v", coutbuf.f.Name(), err)
        }
 }
 
 func Cpos() int64 {
-       off, err := coutbuf.f.Seek(0, 1)
-       if err != nil {
-               Exitf("seeking in output [0, 1]: %v", err)
-       }
-       return off + int64(coutbuf.Buffered())
+       return coutbuf.off
 }
 
 func Cseek(p int64) {
+       if p == coutbuf.off {
+               return
+       }
        Cflush()
        if _, err := coutbuf.f.Seek(p, 0); err != nil {
                Exitf("seeking in output [0, 1]: %v", err)
        }
+       coutbuf.off = p
 }
 
 func Cwrite(p []byte) {
@@ -1842,7 +1855,8 @@ func Cwrite(p []byte) {
 }
 
 func Cput(c uint8) {
-       coutbuf.WriteByte(c)
+       coutbuf.w.WriteByte(c)
+       coutbuf.off++
 }
 
 func usage() {