Skip to content

Missed optimization when using split_first #109328

Description

@xTachyon

Impl 1:

pub fn f(input: &mut &[u64]) -> Option<u64> {
    match input {
        [] => None,
        [first, rest @ ..] => {
            *input = rest;
            Some(*first)
        }
    }
}

Impl 2:

pub fn f(input: &mut &[u64]) -> Option<u64> {
    let (first, rest) = input.split_first()?;
    *input = rest;
    Some(*first)
}

Godbolt: https://godbolt.org/z/aasq15z7a

These functions should be equivalent, but the second function generates one more if than the first one. This also happens when using .get(0) and then doing the slicing manually. I expected the impls to generate the same code.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-enhancementCategory: An issue proposing an enhancement or a PR with one.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.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