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

Errors() +performance : using strings.Builder instead of string concatenation #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@

package battery

import "fmt"
import (
"fmt"
"strings"
)

// ErrNotFound variable represents battery not found error.
//
Expand Down Expand Up @@ -118,16 +121,18 @@ func (p ErrPartial) noNil() bool {
type Errors []error

func (e Errors) Error() string {
s := "["
for _, err := range e {
var s strings.Builder
s.WriteString("[")
for idx, err := range e {
if err != nil {
s += err.Error() + " "
if idx >= 1 {
Copy link
Member

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).

s.WriteString(" ")
Copy link
Member

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?

}
s.WriteString(err.Error())
}
}
if len(s) > 1 {
s = s[:len(s)-1]
}
return s + "]"
s.WriteString("]")
return s.String()
}

func wrapError(err error) error {
Expand Down
19 changes: 19 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func TestErrors(t *testing.T) {
{Errors{ErrFatal{errors.New("t1")}}, "[Could not retrieve battery info: `t1`]"},
{Errors{ErrPartial{Full: errors.New("t2")}, ErrFatal{errors.New("t3")}}, "[{Full:t2} Could not retrieve battery info: `t3`]"},
{Errors{ErrPartial{Full: errors.New("t4")}, ErrPartial{Current: errors.New("t5")}}, "[{Full:t4} {Current:t5}]"},
{Errors{ErrFatal{errors.New("testing error")}, nil}, "[Could not retrieve battery info: `testing error`]"},
}

for i, c := range cases {
Expand All @@ -76,3 +77,21 @@ func TestErrors(t *testing.T) {
}
}
}

func BenchmarkErrors_Error(b *testing.B) {
e := getErrors()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = e.Error()
}
}

func getErrors() Errors {
return Errors([]error{
errors.New("1"),
errors.New("2"),
errors.New("3"),
errors.New("4"),
errors.New("5"),
})
}