mutable: zero out tail slots dropped by Filter and FilterI#890
mutable: zero out tail slots dropped by Filter and FilterI#890c-tonneslan wants to merge 2 commits into
Conversation
A caller that didn't reassign the original slice would see the removed slots holding leftover values from the in-place compaction. The example in the issue shows it best: removing "bar" from [foo bar baz] produced [foo baz baz] in the original slice, with baz duplicated. Zero out the unused tail after compaction, the same trick the standard library uses in slices.Delete. Written as an explicit loop instead of clear() so this still compiles on the module's go 1.18 minimum. Also fixes the doc comments while I'm here: Filter's predicate doesn't take an index, Map doesn't return a slice. Updated the example output to match the new tail-zeroing behavior. Fixes samber#842. Signed-off-by: Charlie Tonneslan <[email protected]>
|
By default, this is faster to skip zeroing. Can you tell me more about why it would be useful for you? |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #890 +/- ##
=======================================
Coverage 92.64% 92.65%
=======================================
Files 32 32
Lines 5110 5116 +6
=======================================
+ Hits 4734 4740 +6
Misses 273 273
Partials 103 103
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:
|
|
Two main reasons:
The visible bug from #842 (duplicate If you'd rather skip zeroing for value-type slices specifically, I can branch on that. Happy to follow up. But matching stdlib feels like the right default. |
|
Ok, the argument (2) legitimate the extra cost. Let's merge ;) |
Fixes #842.
A caller who didn't reassign the original slice would see the dropped slots holding leftover values from the in-place compaction. The issue's example is the clearest demo: removing `bar` from `[foo bar baz]` left the original slice looking like `[foo baz baz]` with `baz` duplicated. The new shorter slice returned by `Filter` was correct, the backing array wasn't.
Zero out the unused tail after compaction, the same approach the standard library uses in `slices.Delete`. Written as an explicit loop rather than `clear` so this still compiles on the module's `go 1.18` minimum.
While in there:
Reduced the noise in the doc comments at the same time.