-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
Errors() +performance : using strings.Builder instead of string concatenation #31
base: master
Are you sure you want to change the base?
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.
Thanks, makes perfect sense to change that. The strings.Builder
didn't exist back when I was writing this code.
No need to include 2 versions and benchmarks, though, we're not a strings building library ;-). Just change the old implementation to the new one.
Done, thanks! |
if err != nil { | ||
s += err.Error() + " " | ||
if idx >= 1 { |
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.
Now it has the same problem with nil
at the beginning ;-].
You cannot rely on indexes when you're also filtering at the same time.
Think the way to solve it here is to check s.Len() > 1
. Shouldn't add much to the time, as the Len call is effectively O(1).
if err != nil { | ||
s += err.Error() + " " | ||
if idx >= 1 { | ||
s.WriteString(" ") |
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 suppose that WriteByte(' ')
would be somewhat faster?
Proposing to use
strings.Builder
in place ofstring concatenation
, inbattery.Error()
- following are the results -