Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference interferes with optimization #58622

Open
tspiteri opened this issue Feb 21, 2019 · 3 comments
Open

Reference interferes with optimization #58622

tspiteri opened this issue Feb 21, 2019 · 3 comments
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tspiteri
Copy link
Contributor

(This comes from a Reddit thread.)

In the code below, slow takes double the time fast takes. If print is changed to take a value rather than reference, the difference goes away.

fn print(s: &u64) {
    println!("{}", s);
}
fn fast() {
    let mut s: u64 = 0;
    for x in 0..10000000000 {
        if x % 16 < 4 {
            s += x;
        }
    }
    let s = s;
    print(&s);
}
fn slow() {
    let mut s: u64 = 0;
    for x in 0..10000000000 {
        if x % 16 < 4 {
            s += x;
        }
    }
    print(&s);
}
fn main() {
    if std::env::var("FAST").is_ok() {
        fast();
    } else {
        slow();
    }
}

Timings:

$ rustc -O main.rs
$ time FAST=1 ./main
12499999983750000000

real	0m4.334s
user	0m4.328s
sys	0m0.001s
$ time ./main
12499999983750000000

real	0m8.788s
user	0m8.776s
sys	0m0.002s
@jonas-schievink jonas-schievink added I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 21, 2019
@smmalis37
Copy link
Contributor

This sounds somewhat similar to https://medium.com/@robertgrosse/how-copying-an-int-made-my-code-11-times-faster-f76c66312e0f

@krdln
Copy link
Contributor

krdln commented Feb 21, 2019

Could it also be a similar thing that enables the assert_eq optimization?

@memoryruins
Copy link
Contributor

The fast and slow functions now generate effectively the same codegen (with a minor difference, godbolt); the current nightly on godbolt is rustc 1.64.0-nightly (06754d885 2022-07-08).

Based on the issue's creation date, stable rustc at that time was 1.32. Because the codegen for this example improved from 1.56 onward, I presume this was fixed by the upgrade to LLVM 13.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants