Skip to content
Merged
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
65 changes: 65 additions & 0 deletions benchmark_test.go

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

It'd be cool if CI could track benchmark results over time, or if we could assert on things like allocations, but definitely not for this PR. 🙈

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package terrors

import (
"errors"
"testing"
)

// newTerrorAtDepth recurses to a given depth before constructing a fresh
// terror, so the benchmark captures (and symbolises) a stack of a realistic
// size rather than the shallow one the test harness itself provides.
func newTerrorAtDepth(depth int) *Error {
if depth > 0 {
return newTerrorAtDepth(depth - 1)
}
return New("benchmark.error", "something went wrong", map[string]string{
"context": "benchmark",
})
}

// augmentAtDepth recurses to a given depth before augmenting the given error,
// so the augment path builds a stack of a realistic size.
func augmentAtDepth(err error, depth int) error {
if depth > 0 {
return augmentAtDepth(err, depth-1)
}
return Augment(err, "additional context", map[string]string{
"augment": "benchmark",
})
}

// BenchmarkNew measures constructing a brand new terror from a realistic call
// depth. This exercises the stack capture and symbolisation on the hot path.
func BenchmarkNew(b *testing.B) {
var err *Error
b.ReportAllocs()
for i := 0; i < b.N; i++ {
err = newTerrorAtDepth(20)
}
_ = err
}

// BenchmarkAugmentGoError measures augmenting a vanilla Go error into a terror.
// This goes through NewInternalWithCause and captures a fresh stack.
func BenchmarkAugmentGoError(b *testing.B) {
goErr := errors.New("plain go error")
var err error
b.ReportAllocs()
for i := 0; i < b.N; i++ {
err = augmentAtDepth(goErr, 20)
}
_ = err
}

// BenchmarkAugmentTerror measures augmenting an existing terror. This path
// captures the current stack to decide whether it shares a common ancestry
// with the stack already recorded on the terror being augmented.
func BenchmarkAugmentTerror(b *testing.B) {
terr := New("benchmark.error", "something went wrong", nil)
var err error
b.ReportAllocs()
for i := 0; i < b.N; i++ {
err = augmentAtDepth(terr, 20)
}
_ = err
}
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (p *Error) Retryable() bool {
return *p.IsRetryable
}
for _, c := range retryableCodes {
if PrefixMatches(p, c) {
if p.PrefixMatches(c) {
return true
}
}
Expand Down
12 changes: 7 additions & 5 deletions factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,24 +127,26 @@ func RateLimited(code, message string, params map[string]string) *Error {
// errorConstructor returns a `*Error` with the specified code, message and params.
// Builds a stack based on the current call stack
func errorFactory(code string, message string, params map[string]string) *Error {
// Params is guaranteed non-nil for callers. Reuse the caller's map when
// provided rather than allocating an empty one and immediately discarding it.
if params == nil {
params = map[string]string{}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to say:

I'm a bit worried that if we're no longer unconditionally copying the params map, then the caller of the terrors constructor can keep a reference to this map around and modify it after the fact.

But that's not even what we were doing before, so it must be fine. 🙈

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tripped me up too but yeah the behaviour is preserved

}
err := &Error{
Code: ErrUnknown,
Message: message,
Params: map[string]string{},
Params: params,
}
if len(code) > 0 {
err.Code = code

err.IsRetryable = &notRetryable
for _, c := range retryableCodes {
if PrefixMatches(err, c) {
if err.PrefixMatches(c) {

@EddShaw EddShaw Aug 13, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, took me a moment to clock that the allocation is coming from the free function PrefixMatches, which takes an error. Error.PrefixMatches doesn't Wrap to access the error code, avoiding an allocation.

I think we can avoid the allocation in PrefixMatches as well - we don't need to Wrap. We could use the same type-cast as in WrapWithCode. That'll benefit PrefixMatches callers too. But it might not be worth the marginal increase in complexity?

err.IsRetryable = &retryable
}
}
}
if params != nil {
err.Params = params
}

// TODO pass in context.Context

Expand Down
Loading