Skip to content

Missed optimization when using split_first #109328

Closed

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

  1. added
    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
    I-slowIssue: Problems and improvements with respect to performance of generated code.
    on Mar 18, 2023
  2. JakobDegen commented on Mar 18, 2023

    @JakobDegen
    Contributor

    Quoting myself on Zulip:

    I've seen this before too. The extra comparison is a null pointer check and is going to be the result of something creating a Option<&T>

  3. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    and removed
    C-bugCategory: This is a bug.
    on Mar 18, 2023
  4. nikic commented on Mar 19, 2023

    @nikic
    Contributor

    This is a metadata preservation failure during SimplifyCFG speculation: https://llvm.godbolt.org/z/frs8o853f

    Preserving !nonnull here used to be illegal, but thanks to https://reviews.llvm.org/D141386 we are allowed to preserve it now. Only need to drop !noundef.

  5. self-assigned this
    on Mar 22, 2023
  6. nikic commented on Mar 22, 2023

    @nikic
    Contributor
  7. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Aug 14, 2023
  8. removed their assignment
    on Aug 14, 2023
  9. nikic commented on Aug 14, 2023

    @nikic
    Contributor

    Fixed by #114048, needs codegen test.

  10. added
    C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing such
    on Oct 8, 2023
  11. added 5 commits that reference this issue on Jun 9, 2024
    b7edafe
    a1b4415
    b1761a2
    4936869
    7ac6c2f
  12. added 3 commits that reference this issue on Jun 30, 2024
  13. added a commit that references this issue on Jun 30, 2024
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