Skip to content

Commit 83ed734

Browse files
committed
cmd/link: pre-resolve package reference
Pre-resolve package index references, so it doesn't need to do a map lookup in every cross-package symbol reference resolution. It increases the memory usage very slightly (O(# imported packages)). Change-Id: Ia76c97ac51f1c2c2d5ea7ae34853850ec69ef0a8 Reviewed-on: https://go-review.googlesource.com/c/go/+/253604 Run-TryBot: Cherry Zhang <[email protected]> Reviewed-by: Than McIntosh <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent bdad428 commit 83ed734

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/cmd/link/internal/loader/loader.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,12 @@ type oReader struct {
9393
version int // version of static symbol
9494
flags uint32 // read from object file
9595
pkgprefix string
96-
syms []Sym // Sym's global index, indexed by local index
97-
ndef int // cache goobj.Reader.NSym()
98-
nhashed64def int // cache goobj.Reader.NHashed64Def()
99-
nhasheddef int // cache goobj.Reader.NHashedDef()
100-
objidx uint32 // index of this reader in the objs slice
96+
syms []Sym // Sym's global index, indexed by local index
97+
pkg []uint32 // indices of referenced package by PkgIdx (index into loader.objs array)
98+
ndef int // cache goobj.Reader.NSym()
99+
nhashed64def int // cache goobj.Reader.NHashed64Def()
100+
nhasheddef int // cache goobj.Reader.NHashedDef()
101+
objidx uint32 // index of this reader in the objs slice
101102
}
102103

103104
// Total number of defined symbols (package symbols, hashed symbols, and
@@ -219,7 +220,7 @@ type Loader struct {
219220

220221
deferReturnTramp map[Sym]bool // whether the symbol is a trampoline of a deferreturn call
221222

222-
objByPkg map[string]*oReader // map package path to its Go object reader
223+
objByPkg map[string]uint32 // map package path to the index of its Go object reader
223224

224225
anonVersion int // most recently assigned ext static sym pseudo-version
225226

@@ -331,7 +332,7 @@ func NewLoader(flags uint32, elfsetstring elfsetstringFunc, reporter *ErrorRepor
331332
objSyms: make([]objSym, 1, 100000), // reserve index 0 for nil symbol
332333
extReader: extReader,
333334
symsByName: [2]map[string]Sym{make(map[string]Sym, 80000), make(map[string]Sym, 50000)}, // preallocate ~2MB for ABI0 and ~1MB for ABI1 symbols
334-
objByPkg: make(map[string]*oReader),
335+
objByPkg: make(map[string]uint32),
335336
outer: make(map[Sym]Sym),
336337
sub: make(map[Sym]Sym),
337338
dynimplib: make(map[Sym]string),
@@ -370,7 +371,7 @@ func (l *Loader) addObj(pkg string, r *oReader) Sym {
370371
}
371372
pkg = objabi.PathToPrefix(pkg) // the object file contains escaped package path
372373
if _, ok := l.objByPkg[pkg]; !ok {
373-
l.objByPkg[pkg] = r
374+
l.objByPkg[pkg] = r.objidx
374375
}
375376
i := Sym(len(l.objSyms))
376377
l.start[r] = i
@@ -635,12 +636,7 @@ func (l *Loader) resolve(r *oReader, s goobj.SymRef) Sym {
635636
case goobj.PkgIdxSelf:
636637
rr = r
637638
default:
638-
pkg := r.Pkg(int(p))
639-
var ok bool
640-
rr, ok = l.objByPkg[pkg]
641-
if !ok {
642-
log.Fatalf("reference of nonexisted package %s, from %v", pkg, r.unit.Lib)
643-
}
639+
rr = l.objs[r.pkg[p]].r
644640
}
645641
return l.toGlobal(rr, s.SymIdx)
646642
}
@@ -2195,6 +2191,18 @@ func loadObjRefs(l *Loader, r *oReader, arch *sys.Arch) {
21952191
}
21962192
}
21972193

2194+
// referenced packages
2195+
npkg := r.NPkg()
2196+
r.pkg = make([]uint32, npkg)
2197+
for i := 1; i < npkg; i++ { // PkgIdx 0 is a dummy invalid package
2198+
pkg := r.Pkg(i)
2199+
objidx, ok := l.objByPkg[pkg]
2200+
if !ok {
2201+
log.Fatalf("reference of nonexisted package %s, from %v", pkg, r.unit.Lib)
2202+
}
2203+
r.pkg[i] = objidx
2204+
}
2205+
21982206
// load flags of package refs
21992207
for i, n := 0, r.NRefFlags(); i < n; i++ {
22002208
rf := r.RefFlags(i)

0 commit comments

Comments
 (0)