Repository navigation
Avoid copies when matching prefixes - #63
Conversation
| // 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.
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. 🙈
There was a problem hiding this comment.
This tripped me up too but yeah the behaviour is preserved
| err.IsRetryable = ¬Retryable | ||
| for _, c := range retryableCodes { | ||
| if PrefixMatches(err, c) { | ||
| if err.PrefixMatches(c) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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. 🙈
We are calling
PrefixMatcheswhich takes a copy of the terr when we don't need to, avoiding this saves a bunch of allocations