From 6d0b55193104357255dacc9b3270c944374eed97 Mon Sep 17 00:00:00 2001 From: David Crawshaw Date: Mon, 29 Feb 2016 08:00:46 -0900 Subject: [PATCH] cmd/link: track offset instead of using seek 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 TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- src/cmd/link/internal/ld/lib.go | 44 ++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/cmd/link/internal/ld/lib.go b/src/cmd/link/internal/ld/lib.go index 461ebf8db1f..93b2dab3047 100644 --- a/src/cmd/link/internal/ld/lib.go +++ b/src/cmd/link/internal/ld/lib.go @@ -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() {