Repository navigation
Avoid copies when matching prefixes #63
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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{} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. 🙈
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = ¬Retryable | ||
| for _, c := range retryableCodes { | ||
| if PrefixMatches(err, c) { | ||
| if err.PrefixMatches(c) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 I think we can avoid the allocation in |
||
| err.IsRetryable = &retryable | ||
| } | ||
| } | ||
| } | ||
| if params != nil { | ||
| err.Params = params | ||
| } | ||
|
|
||
| // TODO pass in context.Context | ||
|
|
||
|
|
||
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.
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. 🙈