-
Notifications
You must be signed in to change notification settings - Fork 89
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
scoring: reduce allocations for addScore #670
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternative feedback inline.
While you are cleaning this up, its probably worth adding a closure in the main place we call addScore to seach call site doesn't need to also pass in debugScore.
eval.go
Outdated
func (m *FileMatch) addScore(what string, computed float64, raw float64, debugScore bool) { | ||
if computed != 0 && debugScore { | ||
m.Debug += fmt.Sprintf("%s:%.2f", what, computed) | ||
if raw != -1 { | ||
m.Debug += fmt.Sprintf("(%s), ", strconv.FormatFloat(raw, 'f', -1, 64)) // uses the smallest number of digits necessary to represent raw. | ||
} else { | ||
m.Debug += ", " | ||
} | ||
} | ||
m.Score += s | ||
m.Score += computed | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alternative implementation to still aid reading stuff at the call sites.
func addScore(s float64, debugScore bool, whatFmt string, whatArgs ...any) {
if s != 0 && debugScore {
var b strings.Builder
fmt.Fprintf(&b, whatFmt, whatArgs...)
fmt.Fprintf(&b, ":%.2f, ", s)
m.Debug += b.String()
}
m.Score += s
}
What I don't like about what I propose is it was nice to have the string first. Then maybe consider something like
func addScore(what string, s float64, debugScore bool, whatArgs ...any) {
if s != 0 && debugScore {
var b strings.Builder
b.WriteString(what)
for _, v := range whatArgs {
fmt.Fprintf(&b, ":%v", v)
}
fmt.Fprintf(&b, ":%.2f, ", s)
m.Debug += b.String()
}
m.Score += s
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...any
will still cause an alloc. I added the closure to the call sites and updated the code to use strings.Builder
because it does read nicer.
Fuzz test failed on commit 51eae7c. To troubleshoot locally, use the GitHub CLI to download the seed corpus with
|
we add a bit more complexity to addScore but avoid allocation a string if debugScore = false. Test plan: - I ran a couple of benchmarks and checked the allocations.
51eae7c
to
a6cc91d
Compare
Follow-up from this comment.
We add a bit more complexity to addScore but avoid allocating a string if debugScore = false.
Test plan: