Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor AppendError to use strings.Contains #1270

Merged
merged 1 commit into from
Dec 16, 2024

Conversation

alexandear
Copy link
Contributor

The PR replaces regexp.MustCompile with strings.Contains to simplify and performance.

These simple benchmark show, that version with strings.Contains is more performant.

Benchmark
func BenchmarkAppendError(b *testing.B) {
	analyzer := gosec.NewAnalyzer(nil, false, false, false, 1, nil)

	file := "test.go"
	err := errors.New("test error")

	for i := 0; i < b.N; i++ {
		analyzer.AppendError(file, err)
	}
}

Before (regexp.MustCompile):

-test.shuffle 1734344546323474000
goos: darwin
goarch: arm64
pkg: github.com/securego/gosec/v2
cpu: Apple M1 Pro
BenchmarkAppendError
BenchmarkAppendError-8   	   33993	     35043 ns/op	    3927 B/op	      23 allocs/op
PASS
coverage: 3.7% of statements
ok  	github.com/securego/gosec/v2	3.471s

After (strings.Contains):

Running tool: /opt/homebrew/bin/go test -benchmem -run=^$ -coverprofile=/var/folders/pk/5dzf3qsj6l18s2b3zfw194840000gn/T/vscode-goN80OyX/go-code-cover -bench ^BenchmarkAppendError$ github.com/securego/gosec/v2 -v -race -shuffle=on -parallel=8 -failfast

-test.shuffle 1734344511315519000
goos: darwin
goarch: arm64
pkg: github.com/securego/gosec/v2
cpu: Apple M1 Pro
BenchmarkAppendError
BenchmarkAppendError-8   	 1341718	       874.3 ns/op	     160 B/op	       0 allocs/op
PASS
coverage: 3.6% of statements
ok  	github.com/securego/gosec/v2	4.038s

@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 66.65%. Comparing base (1216c9b) to head (7215ab2).
Report is 23 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1270      +/-   ##
==========================================
- Coverage   68.49%   66.65%   -1.85%     
==========================================
  Files          75       75              
  Lines        4384     5185     +801     
==========================================
+ Hits         3003     3456     +453     
- Misses       1233     1581     +348     
  Partials      148      148              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ccojocar ccojocar merged commit 4fda076 into securego:master Dec 16, 2024
6 checks passed
@alexandear alexandear deleted the refactor-strings-contains branch December 16, 2024 14:39
Comment on lines +546 to 548
if strings.Contains(err.Error(), "no buildable Go source files in") {
return
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is indeed faster

But checking the error message still looks dirty

Is there a case where this could not be used ?

https://pkg.go.dev/go/build#NoGoError

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Created #1273

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants