Skip to content

Commit

Permalink
Use string builder instead of repeated, re-allocating sprintfs.
Browse files Browse the repository at this point in the history
  • Loading branch information
cstorey-monzo committed Mar 12, 2024
1 parent 3fbe698 commit 745d41c
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,14 @@ func (p *Error) StackTrace() []uintptr {

// StackString formats the stack as a beautiful string with newlines
func (p *Error) StackString() string {
// TODO: Use a string builder.
stackStr := ""
var buffer strings.Builder
terr := p
for terr != nil {
if len(stackStr) != 0 && len(terr.StackFrames) > 0 {
stackStr = fmt.Sprintf("%s\n---\n", stackStr)
if buffer.Len() != 0 && len(terr.StackFrames) > 0 {
fmt.Fprintf(&buffer, "\n---")
}
for _, frame := range terr.StackFrames {
stackStr = fmt.Sprintf("%s\n %s:%d in %s", stackStr, frame.Filename, frame.Line, frame.Method)
fmt.Fprintf(&buffer, "\n %s:%d in %s", frame.Filename, frame.Line, frame.Method)
}

if tcause, ok := terr.cause.(*Error); ok {
Expand All @@ -178,7 +177,7 @@ func (p *Error) StackString() string {
}
}

return stackStr
return buffer.String()
}

// VerboseString returns the error message, stack trace and params
Expand Down

0 comments on commit 745d41c

Please sign in to comment.