refactor: improve SamplesBy performance and revert non-panic behavior for negative count#786
Merged
samber merged 1 commit intosamber:masterfrom Feb 8, 2026
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #786 +/- ##
==========================================
- Coverage 94.26% 94.26% -0.01%
==========================================
Files 18 18
Lines 2860 2858 -2
==========================================
- Hits 2696 2694 -2
Misses 149 149
Partials 15 15
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
c4cf979 to
5cbfa11
Compare
5cbfa11 to
f7fecd0
Compare
f7fecd0 to
6161434
Compare
… for negative count Regression introduced in 1c66270 (perf: preallocate result slice in SamplesBy) where `make(Slice, count)` with negative count causes panic. Previously used `Slice{}` with `append`, which handled negative counts gracefully. - Simplify NthOrEmpty by removing redundant error handling - Simplify Sample by removing intermediate variable - Refactor SamplesBy to use index tracking instead of collection copying - More efficient: avoids copying the entire collection - Uses Range to generate indexes and swaps/removes from index slice - Uses `n := len(indexes)` to eliminate redundant bounds checks - Add early return for count <= 0 in SamplesBy (fixes panic with make) - Add edge case tests for SamplesBy: - Test with deterministic random generators (always last index, always 0) - Test count = 0 and count = -1 (verifies non-panic behavior) - Add panic test for out-of-range index generation
6161434 to
74a34b2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Regression introduced in 1c66270 (perf: preallocate result slice in SamplesBy) where
make(Slice, count)with negative count causes panic. Previously usedSlice{}withappend, which handled negative counts gracefully.n := len(indexes)to eliminate redundant bounds checks