[dev.link] cmd/link: reduce allocations in Preload

Don't create loader.Syms until LoadFull (it will be gone soon
anyway.)

Preallocate loader.objSym array.

Don't create loader.values until preloading is done.

Linking cmd/compile:

name           old alloc/op   new alloc/op   delta
Loadlib_GC       36.2MB ± 0%    20.0MB ± 0%  -44.91%  (p=0.016 n=5+4)

Change-Id: I82eddcfa7fb8fc4e84e8174a47e59cf1183dd83b
Reviewed-on: https://go-review.googlesource.com/c/go/+/233341
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
This commit is contained in:
Cherry Zhang 2020-05-11 15:11:36 -04:00
parent 25e9417b98
commit 4098ab3ff4

View file

@ -339,7 +339,7 @@ func NewLoader(flags uint32, elfsetstring elfsetstringFunc, reporter *ErrorRepor
ldr := &Loader{ ldr := &Loader{
start: make(map[*oReader]Sym), start: make(map[*oReader]Sym),
objs: []objIdx{{}}, // reserve index 0 for nil symbol objs: []objIdx{{}}, // reserve index 0 for nil symbol
objSyms: []objSym{{}}, // reserve index 0 for nil symbol objSyms: make([]objSym, 1, 100000), // reserve index 0 for nil symbol
extReader: &oReader{}, extReader: &oReader{},
symsByName: [2]map[string]Sym{make(map[string]Sym, 100000), make(map[string]Sym, 50000)}, // preallocate ~2MB for ABI0 and ~1MB for ABI1 symbols symsByName: [2]map[string]Sym{make(map[string]Sym, 100000), make(map[string]Sym, 50000)}, // preallocate ~2MB for ABI0 and ~1MB for ABI1 symbols
objByPkg: make(map[string]*oReader), objByPkg: make(map[string]*oReader),
@ -463,7 +463,8 @@ func (l *Loader) newExtSym(name string, ver int) Sym {
if l.extStart == 0 { if l.extStart == 0 {
l.extStart = i l.extStart = i
} }
l.growSyms(int(i)) l.growValues(int(i) + 1)
l.growAttrBitmaps(int(i) + 1)
pi := l.newPayload(name, ver) pi := l.newPayload(name, ver)
l.objSyms = append(l.objSyms, objSym{l.extReader, int(pi)}) l.objSyms = append(l.objSyms, objSym{l.extReader, int(pi)})
l.extReader.syms = append(l.extReader.syms, i) l.extReader.syms = append(l.extReader.syms, i)
@ -1180,7 +1181,7 @@ func (l *Loader) SymSect(i Sym) *sym.Section {
return l.sects[l.symSects[i]] return l.sects[l.symSects[i]]
} }
// SetSymValue sets the section of the i-th symbol. i is global index. // SetSymSect sets the section of the i-th symbol. i is global index.
func (l *Loader) SetSymSect(i Sym, sect *sym.Section) { func (l *Loader) SetSymSect(i Sym, sect *sym.Section) {
if int(i) >= len(l.symSects) { if int(i) >= len(l.symSects) {
l.symSects = append(l.symSects, make([]uint16, l.NSym()-len(l.symSects))...) l.symSects = append(l.symSects, make([]uint16, l.NSym()-len(l.symSects))...)
@ -1960,7 +1961,6 @@ func (l *Loader) preloadSyms(r *oReader, kind int) {
default: default:
panic("preloadSyms: bad kind") panic("preloadSyms: bad kind")
} }
l.growSyms(len(l.objSyms) + end - start)
l.growAttrBitmaps(len(l.objSyms) + end - start) l.growAttrBitmaps(len(l.objSyms) + end - start)
for i := start; i < end; i++ { for i := start; i < end; i++ {
osym := r.Sym(i) osym := r.Sym(i)
@ -2007,6 +2007,7 @@ func (l *Loader) LoadNonpkgSyms(arch *sys.Arch) {
for _, o := range l.objs[1:] { for _, o := range l.objs[1:] {
loadObjRefs(l, o.r, arch) loadObjRefs(l, o.r, arch)
} }
l.values = make([]int64, l.NSym(), l.NSym()+1000) // +1000 make some room for external symbols
} }
func loadObjRefs(l *Loader, r *oReader, arch *sys.Arch) { func loadObjRefs(l *Loader, r *oReader, arch *sys.Arch) {
@ -2535,12 +2536,10 @@ func loadObjSyms(l *Loader, syms *sym.Symbols, r *oReader, needReloc, needExtRel
// a symbol originally discovered as part of an object file, it's // a symbol originally discovered as part of an object file, it's
// easier to do this if we make the updates to an external symbol // easier to do this if we make the updates to an external symbol
// payload. // payload.
// XXX maybe rename? makeExtPayload?
func (l *Loader) cloneToExternal(symIdx Sym) { func (l *Loader) cloneToExternal(symIdx Sym) {
if l.IsExternal(symIdx) { if l.IsExternal(symIdx) {
panic("sym is already external, no need for clone") panic("sym is already external, no need for clone")
} }
l.growSyms(int(symIdx))
// Read the particulars from object. // Read the particulars from object.
r, li := l.toLocal(symIdx) r, li := l.toLocal(symIdx)