Skip to content

Missed optimization when computing max on an iterator #110595

Open
@xTachyon

Description

@xTachyon

From a conversation on the Discord Rust Community Server:

Impl 1:

pub fn f1(x: i32, y: i32, z: i32) -> i32 {
    [x, y, z].into_iter().max().unwrap()
}

Impl 2:

pub fn f2(x: i32, y: i32, z: i32) -> i32 {
    *[x, y, z].iter().max().unwrap()
}

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

These functions should be equivalent, but the first function generates way more code than it needs to. I expected the code to be equivalent.

There is another interesting combination: for u64, the code on 1.68 looks identical, but on nightly it's even bigger.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-iteratorsArea: IteratorsC-bugCategory: This is a bug.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions