Skip to content

The slice::chunks_exact iterator does not optimize when the initial slice is non-empty #128077

Open

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 c6727fc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-heavyIssue: Problems and improvements with respect to binary size of generated code.I-slowIssue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions