Repository navigation
🐛 (patch) Fix sub_scatter_span() on a view that is already trimmed - #22
Merged
Merged
Conversation
sub_scatter_span() measured offsets from the raw chunks, ignoring the view's own start and final length. A sub-view of a sub-view therefore started too early or ran too long. It now walks the chunks trimmed the way the iterator presents them, which share one helper, and converts to raw chunk offsets at the end.
kammce
force-pushed
the
fix-trimmed-sub-scatter-span
branch
from
October 29, 2026 12:24
cd3d9c2 to
69a0b55
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.
sub_scatter_span()returned the wrong elements when called on a view that was itself made bysub_scatter_span(). It measured offsets from the raw chunks, so it ignored the view's own start and final length.1…6, trimmed to3 4 5 6{ .offset = 1 }2 3 4 5 64 5 61 2 3 | 4 5 | 6 7 8 9, trimmed to2…9{ .offset = 1, .count = 3 }2 3 43 4 5A sub-view of an untrimmed span was already correct, which is why the existing tests passed.
Change
sub_scatter_span()now walks the chunks as the iterator presents them: trimmed by the view's start and final length. While it walks, it counts the elements in the chunks before the current one (elements_before). It stops at the first chunk that ends after the result's first element, then keeps walking to the first chunk that ends after its last. An element's index in its chunk is then its index in the view minuselements_before. Raw chunk offsets are computed only at the end.For chunks
1 2 3 | 4 5 | 6 7 8 9with{ .offset = 2, .count = 4 }, the result is elements 2 to 5 (3 4 5 6):elements_before1 2 3)4 5)6 7 8 9)The result spans chunks 0 to 2, starting at index 2 of chunk 0 and ending at index 0 of chunk 2.
The trimming lives in one private helper,
trimmed_chunk(), which the iterator'sget()now uses too, so the two can't disagree again.The special case for single-chunk spans is gone, since the general walk covers it.
A
countof zero returns an empty span, as an offset past the end already did.Size
sub_scatter_span<unsigned char>()on a Cortex-M3, LLVM 23,-Os:sub_scatter_span()trimmed_chunk()(also used by the iterator)Testing
main, 2,515 of these cases fail, all of them sub-views of a trimmed view.