1
0
mirror of https://github.com/golang/go synced 2024-11-24 19:40:09 -07:00

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 <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
David Crawshaw 2016-02-29 08:00:46 -09:00
parent ed1a5e5da6
commit 6d0b551931

View File

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