Skip to content

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

@Nekrolm

Description

@Nekrolm

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

Activity

added
C-bugCategory: This is a bug.
regression-untriagedUntriaged performance or correctness regression.
on Jun 29, 2022
added
I-prioritizeIssue: Indicates that prioritization has been requested for this issue.
on Jun 29, 2022
bugadani

bugadani commented on Jun 29, 2022

@bugadani
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

erikdesjardins commented on Jun 30, 2022

@erikdesjardins
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

erikdesjardins commented on Jun 30, 2022

@erikdesjardins
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)

added
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Jun 30, 2022
apiraino

apiraino commented on Jul 7, 2022

@apiraino
Contributor

WG-prioritization assigning priority (Zulip discussion).

@rustbot label -I-prioritize +P-medium

added and removed
I-prioritizeIssue: Indicates that prioritization has been requested for this issue.
on Jul 7, 2022
Jules-Bertholet

Jules-Bertholet commented on Jun 10, 2023

@Jules-Bertholet
Contributor

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

added
A-codegenArea: Code generation
I-heavyIssue: Problems and improvements with respect to binary size of generated code.
I-slowIssue: Problems and improvements with respect to performance of generated code.
regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
and removed
regression-untriagedUntriaged performance or correctness regression.
on Jun 10, 2023
erikdesjardins

erikdesjardins commented on Feb 24, 2024

@erikdesjardins
Contributor

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

apiraino

apiraino commented on Feb 26, 2024

@apiraino
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationC-bugCategory: This is a bug.I-heavyIssue: Problems and improvements with respect to binary size of generated code.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-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @bugadani@apiraino@erikdesjardins@Nekrolm@rustbot

        Issue actions

          Heap allocation in Box optimized out with rustc 1.60 but not with 1.61+ · Issue #98679 · rust-lang/rust