Skip to content

Commit 8a074fa

Browse files
committed
[dev.link] cmd/internal/goobj2, cmd/link: experiment new aux symbol accessors
Following the previous CLs, do the same for aux symbols. This has some small speedup: (linking cmd/compile) Dostkcheck 41.0ms ± 1% 38.6ms ± 1% -6.00% (p=0.008 n=5+5) Change-Id: Id62b2fc9e4ef1be92e60e4c03faec0a953eee94e Reviewed-on: https://go-review.googlesource.com/c/go/+/222303 Run-TryBot: Cherry Zhang <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Than McIntosh <[email protected]>
1 parent 8e100a0 commit 8a074fa

4 files changed

Lines changed: 71 additions & 20 deletions

File tree

src/cmd/internal/goobj2/objfile.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,15 @@ func (a *Aux) Size() int {
368368
return 1 + a.Sym.Size()
369369
}
370370

371+
const AuxSize = 9 // TODO: is it possible to not hard-code this?
372+
373+
type Aux2 [AuxSize]byte
374+
375+
func (a *Aux2) Type() uint8 { return a[0] }
376+
func (a *Aux2) Sym() SymRef {
377+
return SymRef{binary.LittleEndian.Uint32(a[1:]), binary.LittleEndian.Uint32(a[5:])}
378+
}
379+
371380
type Writer struct {
372381
wr *bio.Writer
373382
stringMap map[string]uint32
@@ -616,6 +625,19 @@ func (r *Reader) AuxOff(i int, j int) uint32 {
616625
return r.h.Offsets[BlkAux] + (auxIdx+uint32(j))*uint32(auxsiz)
617626
}
618627

628+
// Aux2 returns a pointer to the j-th aux symbol of the i-th symbol.
629+
func (r *Reader) Aux2(i int, j int) *Aux2 {
630+
off := r.AuxOff(i, j)
631+
return (*Aux2)(unsafe.Pointer(&r.b[off]))
632+
}
633+
634+
// Auxs2 returns the aux symbols of the i-th symbol.
635+
func (r *Reader) Auxs2(i int) []Aux2 {
636+
off := r.AuxOff(i, 0)
637+
n := r.NAux(i)
638+
return (*[1 << 20]Aux2)(unsafe.Pointer(&r.b[off]))[:n:n]
639+
}
640+
619641
// DataOff returns the offset of the i-th symbol's data.
620642
func (r *Reader) DataOff(i int) uint32 {
621643
dataIdxOff := r.h.Offsets[BlkDataIdx] + uint32(i*4)

src/cmd/internal/goobj2/objfile_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,20 @@ func dummyWriter() *Writer {
2020
func TestSize(t *testing.T) {
2121
// This test checks that hard-coded sizes match the actual sizes
2222
// in the object file format.
23+
tests := []struct {
24+
x interface{ Write(*Writer) }
25+
want uint32
26+
}{
27+
{&Reloc{}, RelocSize},
28+
{&Aux{}, AuxSize},
29+
}
2330
w := dummyWriter()
24-
(&Reloc{}).Write(w)
25-
off := w.off
26-
if sz := uint32(RelocSize); off != sz {
27-
t.Errorf("size mismatch: %d bytes written, but size=%d", off, sz)
31+
for _, test := range tests {
32+
off0 := w.off
33+
test.x.Write(w)
34+
got := w.off - off0
35+
if got != test.want {
36+
t.Errorf("size(%T) mismatch: %d bytes written, but size=%d", test.x, got, test.want)
37+
}
2838
}
2939
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ func (d *deadcodePass2) init() {
118118
}
119119

120120
func (d *deadcodePass2) flood() {
121-
auxSyms := []loader.Sym{}
122121
for !d.wq.empty() {
123122
symIdx := d.wq.pop()
124123

@@ -162,9 +161,9 @@ func (d *deadcodePass2) flood() {
162161
}
163162
d.mark(r.Sym(), symIdx)
164163
}
165-
auxSyms = d.ldr.ReadAuxSyms(symIdx, auxSyms)
166-
for i := 0; i < len(auxSyms); i++ {
167-
d.mark(auxSyms[i], symIdx)
164+
naux := d.ldr.NAux(symIdx)
165+
for i := 0; i < naux; i++ {
166+
d.mark(d.ldr.Aux2(symIdx, i).Sym(), symIdx)
168167
}
169168
// Some host object symbols have an outer object, which acts like a
170169
// "carrier" symbol, or it holds all the symbols for a particular

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

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ type Reloc2 struct {
6969
func (rel Reloc2) Type() objabi.RelocType { return objabi.RelocType(rel.Reloc2.Type()) + rel.typ }
7070
func (rel Reloc2) Sym() Sym { return rel.l.resolve(rel.r, rel.Reloc2.Sym()) }
7171

72+
// Aux2 holds a "handle" to access an aux symbol record from an
73+
// object file.
74+
type Aux2 struct {
75+
*goobj2.Aux2
76+
r *oReader
77+
l *Loader
78+
}
79+
80+
func (a Aux2) Sym() Sym { return a.l.resolve(a.r, a.Aux2.Sym()) }
81+
7282
// oReader is a wrapper type of obj.Reader, along with some
7383
// extra information.
7484
// TODO: rename to objReader once the old one is gone?
@@ -1164,13 +1174,12 @@ func (l *Loader) SymGoType(i Sym) Sym {
11641174
return pp.gotype
11651175
}
11661176
r, li := l.toLocal(i)
1167-
naux := r.NAux(li)
1168-
for j := 0; j < naux; j++ {
1169-
a := goobj2.Aux{}
1170-
a.Read(r.Reader, r.AuxOff(li, j))
1171-
switch a.Type {
1177+
auxs := r.Auxs2(li)
1178+
for j := range auxs {
1179+
a := &auxs[j]
1180+
switch a.Type() {
11721181
case goobj2.AuxGotype:
1173-
return l.resolve(r, a.Sym)
1182+
return l.resolve(r, a.Sym())
11741183
}
11751184
}
11761185
return 0
@@ -1266,6 +1275,18 @@ func (l *Loader) AuxSym(i Sym, j int) Sym {
12661275
return l.resolve(r, a.Sym)
12671276
}
12681277

1278+
// Returns the "handle" to the j-th aux symbol of the i-th symbol.
1279+
func (l *Loader) Aux2(i Sym, j int) Aux2 {
1280+
if l.IsExternal(i) {
1281+
return Aux2{}
1282+
}
1283+
r, li := l.toLocal(i)
1284+
if j >= r.NAux(li) {
1285+
return Aux2{}
1286+
}
1287+
return Aux2{r.Aux2(li, j), r, l}
1288+
}
1289+
12691290
// GetFuncDwarfAuxSyms collects and returns the auxiliary DWARF
12701291
// symbols associated with a given function symbol. Prior to the
12711292
// introduction of the loader, this was done purely using name
@@ -1593,12 +1614,11 @@ func (l *Loader) FuncInfo(i Sym) FuncInfo {
15931614
return FuncInfo{}
15941615
}
15951616
r, li := l.toLocal(i)
1596-
n := r.NAux(li)
1597-
for j := 0; j < n; j++ {
1598-
a := goobj2.Aux{}
1599-
a.Read(r.Reader, r.AuxOff(li, j))
1600-
if a.Type == goobj2.AuxFuncInfo {
1601-
b := r.Data(int(a.Sym.SymIdx))
1617+
auxs := r.Auxs2(li)
1618+
for j := range auxs {
1619+
a := &auxs[j]
1620+
if a.Type() == goobj2.AuxFuncInfo {
1621+
b := r.Data(int(a.Sym().SymIdx))
16021622
return FuncInfo{l, r, b}
16031623
}
16041624
}

0 commit comments

Comments
 (0)