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

Heap allocation in Box optimized out with rustc 1.60 but not with 1.61+ #98679

Closed
Nekrolm opened this issue Jun 29, 2022 · 7 comments
Closed
Labels
A-codegen Area: Code generation C-bug Category: This is a bug. I-heavy Issue: Problems and improvements with respect to binary size of generated code. I-slow Issue: Problems and improvements with respect to performance of generated code. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Nekrolm
Copy link

Nekrolm commented Jun 29, 2022

I tried this code:

pub struct BBox {
    top: i32,
    left: i32,
    height: i32,
    width: i32
}

fn fiddle(bbox: BBox) -> Box<BBox> {
    Box::new(BBox {
        top: bbox.top * 2,
        ..bbox
    })
}

pub fn main() {
    let bbox = Box::new(BBox {
            top: 551,
            left: 100,
            height: 200,
            width: 180,
    });
    let bbox = fiddle(*bbox);
    let bbox_top = bbox.top;
    println!("{}", bbox_top)
}

With rustc 1.60 all Box allocations are totally optimized out.
But starting from 1.61 it doesn't happen.

godbolt link

Possible it is the same issue as #97751

@Nekrolm Nekrolm added C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression. labels Jun 29, 2022
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Jun 29, 2022
@bugadani
Copy link
Contributor

Interestingly, a much simpler case isn't optimized at all with either compiler version (though generated code changed between the versions): https://godbolt.org/z/5G9cP5n88

@erikdesjardins
Copy link
Contributor

@bugadani that happens because println takes references to its arguments, which keeps the allocation alive. If you force a move then it is optimized out: https://godbolt.org/z/qz8n7M5jd

@erikdesjardins
Copy link
Contributor

Since this starts getting optimized in 1.59 and stops in 1.61, it is likely #92419, which landed in 1.59 and was reverted in 1.61. (see issues linked in #94823 for more info)

@apiraino apiraino added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 30, 2022
@apiraino
Copy link
Contributor

apiraino commented Jul 7, 2022

WG-prioritization assigning priority (Zulip discussion).

@rustbot label -I-prioritize +P-medium

@rustbot rustbot added P-medium Medium priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jul 7, 2022
@Jules-Bertholet
Copy link
Contributor

@rustbot label A-codegen I-heavy I-slow -regression-untriaged +regression-from-stable-to-stable

@rustbot rustbot added A-codegen Area: Code generation I-heavy Issue: Problems and improvements with respect to binary size of generated code. I-slow Issue: Problems and improvements with respect to performance of generated code. regression-from-stable-to-stable Performance or correctness regression from one stable version to another. and removed regression-untriaged Untriaged performance or correctness regression. labels Jun 10, 2023
@erikdesjardins
Copy link
Contributor

This was fixed in 1.75 by #102099: https://godbolt.org/z/zqcqh3Mch

@apiraino
Copy link
Contributor

Given the age of this issue, I'm going to close it and assume it's fixed. @Nekrolm please feel free to open a new issue if it's not the case.

Closed by #102099

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation C-bug Category: This is a bug. I-heavy Issue: Problems and improvements with respect to binary size of generated code. I-slow Issue: Problems and improvements with respect to performance of generated code. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. 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

6 participants