Skip to content

Commit

Permalink
fix: avoid regex compilation for every symbol
Browse files Browse the repository at this point in the history
Signed-off-by: Alessio Greggi <[email protected]>
  • Loading branch information
alegrey91 committed Jan 27, 2025
1 parent 4e7d728 commit 6deca6b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
9 changes: 6 additions & 3 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ var analyzeCmd = &cobra.Command{
}

// retrieving function symbols from ELF file.
elf := elfreader.NewElfReader(testBinPath)
symbolsFromElf, err := elf.FunctionSymbols(moduleName)
elf, err := elfreader.NewElfReader(testBinPath)
if err != nil {
return fmt.Errorf("failed to initialize elf file: %v", err)
}
fnSymbols, err := elf.FunctionSymbols(moduleName)
if err != nil {
return fmt.Errorf("failed to get function symbols: %v", err)
}
Expand All @@ -115,7 +118,7 @@ var analyzeCmd = &cobra.Command{
// in the _test.go files in the same directory.
// if not, they will not be included in the report,
// so we can avoid useless symbols to be traced.
for _, symbol := range symbolsFromElf {
for _, symbol := range fnSymbols {
functionName := analyzer.ExtractFunctionName(symbol)
testFiles, _ := listTestFiles(path)
for _, testFile := range testFiles {
Expand Down
22 changes: 12 additions & 10 deletions internal/elfreader/symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ type ElfReader struct {
file *elf.File
}

// Regular expression to match ".func" followed by one or more digits
var reGoroutine = regexp.MustCompile(`\.func\d+$`)

// Regular expression to check for valid Test suffix patterns
var reTestFunction = regexp.MustCompile(`\.Test[\w.]*$`)

// NewElfReader opens an ELF file and returns the ElfReader struct.
func NewElfReader(filePath string) *ElfReader {
func NewElfReader(filePath string) (*ElfReader, error) {
elfFile, err := elf.Open(filePath)
if err != nil {
fmt.Println("error: %w", err)
return nil, fmt.Errorf("%v", err)
}
return &ElfReader{
file: elfFile,
}
}, nil
}

// Close closes the ElfReader file.
Expand All @@ -37,7 +43,7 @@ func (e *ElfReader) FunctionSymbols(pattern string) ([]string, error) {
var symbolsList []string
for _, symb := range symbols {
// filter only function symbols
if !(elf.ST_TYPE(symb.Info) == elf.STT_FUNC) {
if elf.ST_TYPE(symb.Info) != elf.STT_FUNC {
continue
}
// filter for package related symbols
Expand All @@ -63,14 +69,10 @@ func (e *ElfReader) FunctionSymbols(pattern string) ([]string, error) {

// isGoroutine detects if the symbol passed as argument is a goroutine function.
func isGoroutine(s string) bool {
// Regular expression to match ".func" followed by one or more digits
re := regexp.MustCompile(`\.func\d+$`)
return re.MatchString(s)
return reGoroutine.MatchString(s)
}

// isTestFunction detects if the symbol passed as argument is a Test function.
func isTestFunction(s string) bool {
// Regular expression to check for valid Test suffix patterns
re := regexp.MustCompile(`\.Test[\w.]*$`)
return re.MatchString(s)
return reTestFunction.MatchString(s)
}

0 comments on commit 6deca6b

Please sign in to comment.