-
Notifications
You must be signed in to change notification settings - Fork 104
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
feat: Opt for concatenation instead of using fmt.Sprintf #2270
Conversation
|
pkg/procedure/procedure_test.go
Outdated
{"StringsLength-10", 10}, | ||
{"StringsLength-100", 100}, | ||
{"StringsLength-1000", 1000}, |
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.
I think this is a bit flaky? With 10, we're likely just randomizing enough of the same stringsto get to "0" ? Otherwise the numbers don't seem to make sense.
name old allocs/op new allocs/op delta
ToName/StringsLength-10-10 3.00 ± 0% 0.00 -100.00% (p=0.000 n=100+100)
ToName/StringsLength-100-10 3.00 ± 0% 1.00 ± 0% -66.67% (p=0.000 n=100+100)
ToName/StringsLength-1000-10 3.00 ± 0% 1.00 ± 0% -66.67% (p=0.000 n=100+100)
I would just put a single basic benchmark with a hardcoded value?
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.
Agreed, I will update the benchmark strategy and revise the summary accordingly.
// ToName gets the procedure name we should use for a method | ||
// with the given service name and method name. | ||
func ToName(serviceName string, methodName string) string { | ||
return fmt.Sprintf("%s::%s", serviceName, methodName) | ||
return serviceName + _separator + methodName |
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.
In general, isn't this fully pre-computable and cacheable at startup? All yarpc configuration is static, right?
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.
I agree that by pre-computing this information early and storing it in the client object, we might further reduce allocations. I'll investigate this possibility in a separate PR.
Summary
The procedure.ToName function, a frequently used pathway,
currently relies on
fmt.Sprintf
to merge strings.However, this method could inadvertently cause additional
memory allocations. By directly concatenating these strings
instead of utilizing fmt and constructing a format template,
we can reduce unnecessary allocations.
This minor modification has the potential to enhance the
efficiency of this library for all users.
Benchmark strategy
The strategy involves generating a string for both
the serviceName and methodName, running a series of
benchmarks, and then comparing the results using benchstat.
before.txt
after.txt
Results
bechstat result after running it with
go test -bench=. -count=100