Skip to content

Commit 31da1d9

Browse files
qingyunharandall77
authored andcommitted
cmd/internal/objfile: cache computation of goobj.Arch
Change-Id: I23774cf185e5fa6b89398001cd0655fb0c5bdb46 GitHub-Last-Rev: ca8cae2 GitHub-Pull-Request: #40877 Reviewed-on: https://go-review.googlesource.com/c/go/+/249180 Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent ac875bc commit 31da1d9

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

src/cmd/internal/archive/archive.go

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"log"
1818
"os"
1919
"strconv"
20+
"strings"
2021
"time"
2122
"unicode/utf8"
2223
)
@@ -83,6 +84,7 @@ func (e *Entry) String() string {
8384

8485
type GoObj struct {
8586
TextHeader []byte
87+
Arch string
8688
Data
8789
}
8890

@@ -404,6 +406,10 @@ func (r *objReader) parseObject(o *GoObj, size int64) error {
404406
}
405407
}
406408
o.TextHeader = h
409+
hs := strings.Fields(string(h))
410+
if len(hs) >= 4 {
411+
o.Arch = hs[3]
412+
}
407413
o.Offset = r.offset
408414
o.Size = size - int64(len(h))
409415

src/cmd/internal/objfile/goobj.go

+13-19
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ import (
1717
"fmt"
1818
"io"
1919
"os"
20-
"strings"
2120
)
2221

2322
type goobjFile struct {
2423
goobj *archive.GoObj
2524
r *goobj.Reader
2625
f *os.File
26+
arch *sys.Arch
2727
}
2828

2929
func openGoFile(f *os.File) (*File, error) {
@@ -45,9 +45,16 @@ L:
4545
return nil, err
4646
}
4747
r := goobj.NewReaderFromBytes(b, false)
48+
var arch *sys.Arch
49+
for _, a := range sys.Archs {
50+
if a.Name == e.Obj.Arch {
51+
arch = a
52+
break
53+
}
54+
}
4855
entries = append(entries, &Entry{
4956
name: e.Name,
50-
raw: &goobjFile{e.Obj, r, f},
57+
raw: &goobjFile{e.Obj, r, f, arch},
5158
})
5259
continue
5360
case archive.EntryNativeObj:
@@ -223,17 +230,8 @@ func (f *goobjFile) pcln() (textStart uint64, symtab, pclntab []byte, err error)
223230
// Returns "",0,nil if unknown.
224231
// This function implements the Liner interface in preference to pcln() above.
225232
func (f *goobjFile) PCToLine(pc uint64) (string, int, *gosym.Func) {
226-
// TODO: this is really inefficient. Binary search? Memoize last result?
227233
r := f.r
228-
var arch *sys.Arch
229-
archname := f.goarch()
230-
for _, a := range sys.Archs {
231-
if a.Name == archname {
232-
arch = a
233-
break
234-
}
235-
}
236-
if arch == nil {
234+
if f.arch == nil {
237235
return "", 0, nil
238236
}
239237
pcdataBase := r.PcdataBase()
@@ -264,10 +262,10 @@ func (f *goobjFile) PCToLine(pc uint64) (string, int, *gosym.Func) {
264262
lengths := info.ReadFuncInfoLengths(b)
265263
off, end := info.ReadPcline(b)
266264
pcline := r.BytesAt(pcdataBase+off, int(end-off))
267-
line := int(pcValue(pcline, pc-addr, arch))
265+
line := int(pcValue(pcline, pc-addr, f.arch))
268266
off, end = info.ReadPcfile(b)
269267
pcfile := r.BytesAt(pcdataBase+off, int(end-off))
270-
fileID := pcValue(pcfile, pc-addr, arch)
268+
fileID := pcValue(pcfile, pc-addr, f.arch)
271269
globalFileID := info.ReadFile(b, lengths.FileOff, uint32(fileID))
272270
fileName := r.File(int(globalFileID))
273271
// Note: we provide only the name in the Func structure.
@@ -332,11 +330,7 @@ func (f *goobjFile) text() (textStart uint64, text []byte, err error) {
332330
}
333331

334332
func (f *goobjFile) goarch() string {
335-
hs := strings.Fields(string(f.goobj.TextHeader))
336-
if len(hs) >= 4 {
337-
return hs[3]
338-
}
339-
return ""
333+
return f.goobj.Arch
340334
}
341335

342336
func (f *goobjFile) loadAddress() (uint64, error) {

0 commit comments

Comments
 (0)