You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Throughout the whole codebase there are many places that use fmt.Errorf whereas errors.New can be used. Using fmt.Errorf is roughly 4x slower than errors.New.
This can be easily benchmarked with:
var result error
func BenchmarkFmtError(b *testing.B) {
for range b.N {
result = fmt.Errorf("something")
}
}
func BenchmarkErrorsNew(b *testing.B) {
for range b.N {
result = errors.New("something")
}
}
Thanks for the detailed report. This is a good suggestion. It looks like a good number of these are in our generated code so we can patch our generator to remove a lot of these and gofmt the rest.
There are still 137 cases of fmt.Errorf being used in the repo. This can be easily seen by running gofmt -w -r "fmt.Errorf(s) -> errors.New(s)" on the whole repository.
Activity
codyoss commentedon Apr 11, 2024
Thanks for the detailed report. This is a good suggestion. It looks like a good number of these are in our generated code so we can patch our generator to remove a lot of these and gofmt the rest.
perf: use errors.New when possible
perf: use errors.New when possible (#1490)
quartzmo commentedon Aug 12, 2024
@codyoss Can this be closed now?
egonelbre commentedon Aug 13, 2024
There are still 137 cases of
fmt.Errorfbeing used in the repo. This can be easily seen by runninggofmt -w -r "fmt.Errorf(s) -> errors.New(s)"on the whole repository.perf(spanner): avoid using fmt.Errorf unnecessarily
perf(spanner): better error handling (#10734)