Skip to content

Commit 78b96db

Browse files
committed
[dev.link] cmd/link: reuse slice memory in deadcode pass
Reuse slice memory in the deadcode pass, reduce allocations. Linking cmd/compile, name old alloc/op new alloc/op delta Deadcode_GC 2.10MB ± 0% 1.41MB ± 0% -32.61% (p=0.008 n=5+5) name old allocs/op new allocs/op delta Deadcode_GC 8.46k ± 0% 5.55k ± 0% -34.45% (p=0.008 n=5+5) Change-Id: Ib9ba0928d68a65879007218697712b53acd3c5c5 Reviewed-on: https://go-review.googlesource.com/c/go/+/236566 Run-TryBot: Cherry Zhang <cherryyz@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Jeremy Faller <jeremy@golang.org> Reviewed-by: Than McIntosh <thanm@google.com>
1 parent 7179e42 commit 78b96db

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type deadcodePass struct {
2424
ifaceMethod map[methodsig]bool // methods declared in reached interfaces
2525
markableMethods []methodref // methods of reached types
2626
reflectSeen bool // whether we have seen a reflect method call
27+
28+
methodsigstmp []methodsig // scratch buffer for decoding method signatures
2729
}
2830

2931
func (d *deadcodePass) init() {
@@ -92,6 +94,7 @@ func (d *deadcodePass) init() {
9294
}
9395

9496
func (d *deadcodePass) flood() {
97+
var methods []methodref
9598
for !d.wq.empty() {
9699
symIdx := d.wq.pop()
97100

@@ -112,7 +115,7 @@ func (d *deadcodePass) flood() {
112115
}
113116
}
114117

115-
var methods []methodref
118+
methods = methods[:0]
116119
for i := 0; i < relocs.Count(); i++ {
117120
r := relocs.At2(i)
118121
t := r.Type()
@@ -330,7 +333,10 @@ func (m methodref) isExported() bool {
330333
//
331334
// Conveniently this is the layout of both runtime.method and runtime.imethod.
332335
func (d *deadcodePass) decodeMethodSig(ldr *loader.Loader, arch *sys.Arch, symIdx loader.Sym, relocs *loader.Relocs, off, size, count int) []methodsig {
333-
var methods = make([]methodsig, count)
336+
if cap(d.methodsigstmp) < count {
337+
d.methodsigstmp = append(d.methodsigstmp[:0], make([]methodsig, count)...)
338+
}
339+
var methods = d.methodsigstmp[:count]
334340
for i := 0; i < count; i++ {
335341
methods[i].name = decodetypeName(ldr, symIdx, relocs, off)
336342
methods[i].typ = decodeRelocSym(ldr, symIdx, relocs, int32(off+4))

0 commit comments

Comments
 (0)