segmentio / stats

Go package for abstracting stats collection
https://godoc.org/github.com/segmentio/stats
MIT License
208 stars 32 forks source link

Skip tag sort if possible in Report / ReportAt #126

Closed tysonmote closed 3 years ago

tysonmote commented 3 years ago

This is the codepath used by Report() / ReportAt(), which is the fast path for reporting stats in v4. Skipping the sort saves us some work:

benchmark                              old ns/op     new ns/op     delta
BenchmarkTagsBufferSortSorted-12       107           49.3          -53.93%
BenchmarkTagsBufferSortUnsorted-12     270           271           +0.37%

While users are less likely to have already-sorted stat tags in this case (because the tagsBuffer tag slice is built on every call and doesn't retain its sort between calls the way that you would retain sorted engine tags if you were calling stats.Incr() without tags often), it can show up in benchmarks and guide users toward this little cheat by passing in their tags in sorted order.