Skip to content

Commit

Permalink
perf: prevent duplicate call to getPCLNTABData
Browse files Browse the repository at this point in the history
  • Loading branch information
Zxilly committed Oct 10, 2024
1 parent 815803d commit 8d59910
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
16 changes: 10 additions & 6 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (f *GoFile) initModuleData() error {
f.initModuleDataError = err
return
}
f.moduledata, f.initModuleDataError = extractModuledata(f.FileInfo, f.fh)
f.moduledata, f.initModuleDataError = extractModuledata(f)
})
return f.initModuleDataError
}
Expand Down Expand Up @@ -382,8 +382,7 @@ func (f *GoFile) Close() error {
return f.fh.Close()
}

// PCLNTab returns the PCLN table.
func (f *GoFile) PCLNTab() (*gosym.Table, error) {
func (f *GoFile) initPclntab() error {
f.pclntabOnce.Do(func() {
addr, data, err := f.fh.getPCLNTABData()
if err != nil {
Expand Down Expand Up @@ -431,10 +430,15 @@ func (f *GoFile) PCLNTab() (*gosym.Table, error) {
}
f.runtimeText = runtimeText
})
if f.pclntabError != nil {
return nil, f.pclntabError
}
return f.pclntabError
}

// PCLNTab returns the PCLN table.
func (f *GoFile) PCLNTab() (*gosym.Table, error) {
err := f.initPclntab()
if err != nil {
return nil, err
}
return gosym.NewTable(make([]byte, 0), gosym.NewLineTable(f.pclntabBytes, f.runtimeText))
}

Expand Down
21 changes: 11 additions & 10 deletions moduledata.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ func pickVersionedModuleData(info *FileInfo) (modulable, error) {
return buf, nil
}

func extractModuledata(fileInfo *FileInfo, f fileHandler) (moduledata, error) {
vmd, err := pickVersionedModuleData(fileInfo)
func extractModuledata(f *GoFile) (moduledata, error) {
vmd, err := pickVersionedModuleData(f.FileInfo)
if err != nil {
return moduledata{}, err
}
Expand All @@ -277,26 +277,27 @@ func extractModuledata(fileInfo *FileInfo, f fileHandler) (moduledata, error) {
var magic []byte
var tabAddr uint64

secAddr, secData, err := f.getSectionData(f.moduledataSection())
secAddr, secData, err := f.fh.getSectionData(f.fh.moduledataSection())
if err != nil {
return moduledata{}, err
}

// if we can get the moduledata addr from the symbol, we have no need to search
if ok, err := f.hasSymbolTable(); ok && err == nil {
addr, _, err := f.getSymbol("runtime.firstmoduledata")
if ok, err := f.fh.hasSymbolTable(); ok && err == nil {
addr, _, err := f.fh.getSymbol("runtime.firstmoduledata")
if err == nil {
off = int(addr - secAddr)
goto load
}
}

tabAddr, _, err = f.getPCLNTABData()
err = f.initPclntab()
if err != nil {
return moduledata{}, err
}
tabAddr = f.pclntabAddr

magic = buildPclnTabAddrBinary(fileInfo.WordSize, fileInfo.ByteOrder, tabAddr)
magic = buildPclnTabAddrBinary(f.FileInfo.WordSize, f.FileInfo.ByteOrder, tabAddr)

search:
off = bytes.Index(secData, magic)
Expand All @@ -312,7 +313,7 @@ load:

// Read the module struct from the file.
r := bytes.NewReader(data)
err = binary.Read(r, fileInfo.ByteOrder, vmd)
err = binary.Read(r, f.FileInfo.ByteOrder, vmd)
if err != nil {
return moduledata{}, fmt.Errorf("error when reading module data from file: %w", err)
}
Expand All @@ -324,7 +325,7 @@ load:
text := md.TextAddr
etext := md.TextAddr + md.TextLen

textSectAddr, textSect, err := f.getCodeSection()
textSectAddr, textSect, err := f.fh.getCodeSection()
if err != nil {
return moduledata{}, err
}
Expand All @@ -337,7 +338,7 @@ load:
}

// Add the file handler.
md.fh = f
md.fh = f.fh

return md, nil

Expand Down

0 comments on commit 8d59910

Please sign in to comment.