Skip to content

refactor: introduce clone helper for slice copying#784

Draft
d-enk wants to merge 1 commit into
samber:masterfrom
d-enk:refactor-use-clone
Draft

refactor: introduce clone helper for slice copying#784
d-enk wants to merge 1 commit into
samber:masterfrom
d-enk:refactor-use-clone

Conversation

@d-enk
Copy link
Copy Markdown
Contributor

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

  • Add clone() function that returns a shallow copy without preserving nilness
    is guaranteed to be inlined by the compiler (verified with -gcflags=-m)
    (separate from Clone() which preserves nilness - library convention is to return empty slice)
  • Simplify Clone() to delegate to clone() for non-nil collections
  • Refactor Chunk, Sliding, Drop, DropRight, DropWhile, DropRightWhile, Take, TakeWhile, Replace, Trim to use clone()
  • Update SamplesBy and debounce.reset to use clone()
  • Replace make(Slice, 0) with Slice{} for return statements

Also

  • Some redundant checks have been removed in Take, NthOrEmpty
  • Simplify TrimPrefix and TrimSuffix loops
  • Micro-optimize Replace() by reordering condition to short-circuit on n != 0 first
    (TODO: consider adding early break when n reaches 0)

@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.27%. Comparing base (d610980) to head (d873409).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #784      +/-   ##
==========================================
+ Coverage   94.18%   94.27%   +0.08%     
==========================================
  Files          18       18              
  Lines        2873     2848      -25     
==========================================
- Hits         2706     2685      -21     
+ Misses        151      149       -2     
+ Partials       16       14       -2     
Flag Coverage Δ
unittests 94.27% <100.00%> (+0.08%) ⬆️

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 refactor-use-clone branch from d50307f to 3ab8107 Compare January 22, 2026 17:18
@d-enk d-enk marked this pull request as draft January 23, 2026 12:10
@d-enk
Copy link
Copy Markdown
Contributor Author

d-enk commented Jan 23, 2026

Draft reason golang/go#77287

- Add clone() function that returns a shallow copy without preserving nilness
	is guaranteed to be inlined by the compiler (verified with -gcflags=-m)
  (separate from Clone() which preserves nilness - library convention is to return empty slice)
- Simplify Clone() to delegate to clone() for non-nil collections
- Refactor Chunk, Sliding, Drop, DropRight, DropWhile, DropRightWhile, Take, TakeWhile, Replace, Trim to use clone()
- Update SamplesBy and debounce.reset to use clone()
- Replace make(Slice, 0) with Slice{} for return statements

Also
- Some redundant checks have been removed in Take, NthOrEmpty
- Simplify TrimPrefix and TrimSuffix loops
- Micro-optimize Replace() by reordering condition to short-circuit on n != 0 first
  (TODO: consider adding early break when n reaches 0)
@d-enk d-enk force-pushed the refactor-use-clone branch from 3ab8107 to d873409 Compare January 27, 2026 09:56
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.

1 participant