]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: batch writing of bytes
authorShahar Kohanim <skohanim@gmail.com>
Mon, 29 Feb 2016 09:49:49 +0000 (11:49 +0200)
committerDavid Crawshaw <crawshaw@golang.org>
Mon, 29 Feb 2016 23:54:19 +0000 (23:54 +0000)
In best of 10, linking cmd/go shows a ~10% improvement.

tip:              real  0m1.152s user  0m1.005s
this:             real  0m1.065s user  0m0.924s

Change-Id: I303a20b94332feaedc1033c453247a0e4c05c843
Reviewed-on: https://go-review.googlesource.com/19978
Reviewed-by: David Crawshaw <crawshaw@golang.org>
src/cmd/link/internal/ld/data.go
src/cmd/link/internal/ld/lib.go

index ca8eabbccad1fc4e28000a1b8d04a64f084671da..67af9d5ba83f9f253b310cc0599b458f950f5e4d 100644 (file)
@@ -708,7 +708,6 @@ func blk(start *LSym, addr int64, size int64) {
        }
 
        eaddr := addr + size
-       var ep []byte
        var p []byte
        for ; sym != nil; sym = sym.Next {
                if sym.Type&obj.SSUB != 0 {
@@ -723,18 +722,16 @@ func blk(start *LSym, addr int64, size int64) {
                        errorexit()
                }
 
-               for ; addr < sym.Value; addr++ {
-                       Cput(0)
+               if addr < sym.Value {
+                       strnput("", int(sym.Value-addr))
+                       addr = sym.Value
                }
                p = sym.P
-               ep = p[len(sym.P):]
-               for -cap(p) < -cap(ep) {
-                       Cput(uint8(p[0]))
-                       p = p[1:]
-               }
+               Cwrite(p)
                addr += int64(len(sym.P))
-               for ; addr < sym.Value+sym.Size; addr++ {
-                       Cput(0)
+               if addr < sym.Value+sym.Size {
+                       strnput("", int(sym.Value+sym.Size-addr))
+                       addr = sym.Value + sym.Size
                }
                if addr != sym.Value+sym.Size {
                        Diag("phase error: addr=%#x value+size=%#x", int64(addr), int64(sym.Value)+sym.Size)
@@ -746,8 +743,8 @@ func blk(start *LSym, addr int64, size int64) {
                }
        }
 
-       for ; addr < eaddr; addr++ {
-               Cput(0)
+       if addr < eaddr {
+               strnput("", int(eaddr-addr))
        }
        Cflush()
 }
@@ -899,15 +896,26 @@ func Datblk(addr int64, size int64) {
        fmt.Fprintf(&Bso, "\t%.8x|\n", uint(eaddr))
 }
 
-func strnput(s string, n int) {
-       for ; n > 0 && s != ""; s = s[1:] {
-               Cput(uint8(s[0]))
-               n--
-       }
+var zeros [512]byte
 
-       for n > 0 {
-               Cput(0)
-               n--
+// strnput writes the first n bytes of s.
+// If n is larger then len(s),
+// it is padded with NUL bytes.
+func strnput(s string, n int) {
+       if len(s) >= n {
+               Cwritestring(s[:n])
+       } else {
+               Cwritestring(s)
+               n -= len(s)
+               for n > 0 {
+                       if len(zeros) >= n {
+                               Cwrite(zeros[:n])
+                               return
+                       } else {
+                               Cwrite(zeros[:])
+                               n -= len(zeros)
+                       }
+               }
        }
 }
 
index 93b2dab30471d52f4f7611851723fcf503efe0e9..27fef60f18c1e8983ddd30dbe5e7f41ceb68d9b3 100644 (file)
@@ -1850,6 +1850,10 @@ func Cseek(p int64) {
        coutbuf.off = p
 }
 
+func Cwritestring(s string) {
+       coutbuf.WriteString(s)
+}
+
 func Cwrite(p []byte) {
        coutbuf.Write(p)
 }