Skip to content

refactor: improve SamplesBy performance and revert non-panic behavior for negative count#786

Merged
samber merged 1 commit intosamber:masterfrom
d-enk:improve-samples-by
Feb 8, 2026
Merged

refactor: improve SamplesBy performance and revert non-panic behavior for negative count#786
samber merged 1 commit intosamber:masterfrom
d-enk:improve-samples-by

Conversation

@d-enk
Copy link
Contributor

@d-enk d-enk commented Jan 27, 2026

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

@codecov
Copy link

codecov bot commented Jan 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.26%. Comparing base (b6d1bf3) to head (74a34b2).
⚠️ Report is 4 commits behind head on master.

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              
Flag Coverage Δ
unittests 94.26% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@d-enk d-enk force-pushed the improve-samples-by branch from c4cf979 to 5cbfa11 Compare January 27, 2026 11:41
@d-enk d-enk marked this pull request as draft January 27, 2026 20:18
@d-enk d-enk force-pushed the improve-samples-by branch from 5cbfa11 to f7fecd0 Compare January 30, 2026 12:37
@d-enk d-enk changed the title perf: optimize SamplesBy to use index array instead of slice copy refactor: improve SamplesBy performance and revert non-panic behavior for negative count Jan 30, 2026
@d-enk d-enk force-pushed the improve-samples-by branch from f7fecd0 to 6161434 Compare January 30, 2026 12:39
@d-enk d-enk marked this pull request as ready for review January 30, 2026 12:40
… 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
@d-enk d-enk force-pushed the improve-samples-by branch from 6161434 to 74a34b2 Compare January 30, 2026 12:48
@samber samber merged commit 73a8fc6 into samber:master Feb 8, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants