internal/tags: use slices.SortFunc instead of sort.Sort - #182
Conversation
sort.Sort(t) requires boxing the TagSet into a sort.Interface, which allocates on every call since TagSet doesn't fit in a single interface word. slices.SortFunc is generic and takes the comparison function directly, avoiding that allocation entirely. Bumps the module's minimum Go version to 1.21, when the stdlib slices package was introduced. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
| module github.com/lyft/gostats | ||
|
|
||
| go 1.18 | ||
| go 1.21 |
There was a problem hiding this comment.
This change is breaking in that it relies on importers to upgrade to 1.21. The alternative is to add a dependency on golang.org/x/exp/slices.
All OSS dependents on gostats are forks of envoyproxy/ratelimit, and envoyproxy itself is at go1.26: https://github.com/envoyproxy/ratelimit/
Benchmark: allocation impactRan
The 1-4 tag cases are unaffected as expected — they use hand-unrolled fast paths this change doesn't touch. The >4 tag default branch drops from 3 allocations to 2: Methodology: |
sort.Sort(t) requires boxing the TagSet into a sort.Interface, which
allocates on every call since TagSet doesn't fit in a single interface
word. slices.SortFunc is generic and takes the comparison function
directly, avoiding that allocation entirely.
Bumps the module's minimum Go version to 1.21, when the stdlib slices
package was introduced.
Co-Authored-By: Claude Sonnet 5 noreply@anthropic.com
Stack created with GitHub Stacks CLI • Give Feedback 💬