Skip to content

iterator for_each performance regression #112911

Open
@dgiger42

Description

@dgiger42

Code

I tried this code:

pub fn main() {
    let n = 100 as usize;
    println!("n: {}", n);
    let mut v = vec![0; n];

    for _ in 0..100_000_000 {
        v.iter_mut().for_each(|x| {
            *x = 4;
        })
    }
}

I expected to see this happen: Performance at least matching previous versions of Rust

Instead, this happened: On my machine, the execution time on nightly is around 2x as long as on Rust 1.69. The time reported by perf stat went from 0.35 to 0.67 seconds, and the instruction counts went up from 6.7 billion to 8.9 billion.

Version it worked on

It most recently worked on: 1.69

Version with regression

rustc --version --verbose:

rustc 1.72.0-nightly (46514218f 2023-06-20)
binary: rustc
commit-hash: 46514218f6f31ad3a1510ecc32af47e9e486c27d
commit-date: 2023-06-20
host: x86_64-unknown-linux-gnu
release: 1.72.0-nightly
LLVM version: 16.0.5


The regression also affects Rust 1.70, but the performance difference is not as big as on nightly.

Backtrace

Backtrace

<backtrace>

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.I-slowIssue: Problems and improvements with respect to performance of generated code.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-untriagedUntriaged performance or correctness regression.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions