The `slice::chunks_exact` iterator does not optimize when the initial slice is non-empty
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I tried this code:
use std::hint::black_box;
pub fn demo(data: &[u8]) {
assert!(!data.is_empty());
let mut chunks = data.chunks_exact(1024);
let last_block = if chunks.remainder().is_empty() {
chunks.next_back().expect("this is impossible")
} else {
chunks.remainder()
};
black_box(last_block);
}
When compiled with optimizations enabled and viewing the assembly code, I expected to not see a reference to the string "this is impossible". Since there's an assert that the input data is non-empty, then either there will be a remainder, there will be at least one chunk, or both.
Instead, the assembly contains a reference to the expect string and there's a conditional call to expect_failed.
Meta
- 1.79.0
- 1.81.0-nightly (2024-07-12 c6727fc9b5c64cefa726)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by compiling the provided chunks_exact example with optimizations enabled and inspecting its assembly, then trace the relevant slice::chunks_exact implementation. The fix is complete when the impossible expect string and conditional expect_failed call are no longer present for the asserted non-empty input, with regression coverage for this case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100