Skip to content

Commit d3b00a8

Browse files
skohanimcrawshaw
authored andcommitted
cmd/link: batch writing of bytes
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>
1 parent 07ccc21 commit d3b00a8

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

src/cmd/link/internal/ld/data.go

+28-20
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,6 @@ func blk(start *LSym, addr int64, size int64) {
708708
}
709709

710710
eaddr := addr + size
711-
var ep []byte
712711
var p []byte
713712
for ; sym != nil; sym = sym.Next {
714713
if sym.Type&obj.SSUB != 0 {
@@ -723,18 +722,16 @@ func blk(start *LSym, addr int64, size int64) {
723722
errorexit()
724723
}
725724

726-
for ; addr < sym.Value; addr++ {
727-
Cput(0)
725+
if addr < sym.Value {
726+
strnput("", int(sym.Value-addr))
727+
addr = sym.Value
728728
}
729729
p = sym.P
730-
ep = p[len(sym.P):]
731-
for -cap(p) < -cap(ep) {
732-
Cput(uint8(p[0]))
733-
p = p[1:]
734-
}
730+
Cwrite(p)
735731
addr += int64(len(sym.P))
736-
for ; addr < sym.Value+sym.Size; addr++ {
737-
Cput(0)
732+
if addr < sym.Value+sym.Size {
733+
strnput("", int(sym.Value+sym.Size-addr))
734+
addr = sym.Value + sym.Size
738735
}
739736
if addr != sym.Value+sym.Size {
740737
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) {
746743
}
747744
}
748745

749-
for ; addr < eaddr; addr++ {
750-
Cput(0)
746+
if addr < eaddr {
747+
strnput("", int(eaddr-addr))
751748
}
752749
Cflush()
753750
}
@@ -899,15 +896,26 @@ func Datblk(addr int64, size int64) {
899896
fmt.Fprintf(&Bso, "\t%.8x|\n", uint(eaddr))
900897
}
901898

902-
func strnput(s string, n int) {
903-
for ; n > 0 && s != ""; s = s[1:] {
904-
Cput(uint8(s[0]))
905-
n--
906-
}
899+
var zeros [512]byte
907900

908-
for n > 0 {
909-
Cput(0)
910-
n--
901+
// strnput writes the first n bytes of s.
902+
// If n is larger then len(s),
903+
// it is padded with NUL bytes.
904+
func strnput(s string, n int) {
905+
if len(s) >= n {
906+
Cwritestring(s[:n])
907+
} else {
908+
Cwritestring(s)
909+
n -= len(s)
910+
for n > 0 {
911+
if len(zeros) >= n {
912+
Cwrite(zeros[:n])
913+
return
914+
} else {
915+
Cwrite(zeros[:])
916+
n -= len(zeros)
917+
}
918+
}
911919
}
912920
}
913921

src/cmd/link/internal/ld/lib.go

+4
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,10 @@ func Cseek(p int64) {
18501850
coutbuf.off = p
18511851
}
18521852

1853+
func Cwritestring(s string) {
1854+
coutbuf.WriteString(s)
1855+
}
1856+
18531857
func Cwrite(p []byte) {
18541858
coutbuf.Write(p)
18551859
}

0 commit comments

Comments
 (0)