-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Reduce Box::default
stack copies in debug mode
#136089
Merged
bors
merged 2 commits into
rust-lang:master
from
jwong101:box-default-debug-stack-usage
Feb 21, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//@ compile-flags: -Copt-level=0 | ||
|
||
// Test to make sure that `<Box<T>>::default` does not create too many copies of `T` on the stack. | ||
// in debug mode. This regressed in dd0620b86721ae8cae86736443acd3f72ba6fc32 to | ||
// four `T` allocas. | ||
// | ||
// See https://github.com/rust-lang/rust/issues/136043 for more context. | ||
// | ||
// FIXME: This test only wants to ensure that there are at most two allocas of `T` created, instead | ||
// of checking for exactly two. | ||
|
||
#![crate_type = "lib"] | ||
|
||
#[allow(dead_code)] | ||
pub struct Thing([u8; 1000000]); | ||
|
||
impl Default for Thing { | ||
fn default() -> Self { | ||
Thing([0; 1000000]) | ||
} | ||
} | ||
|
||
// CHECK-COUNT-2: %{{.*}} = alloca {{.*}}1000000 | ||
// CHECK-NOT: %{{.*}} = alloca {{.*}}1000000 | ||
#[no_mangle] | ||
pub fn box_default_single_copy() -> Box<Thing> { | ||
Box::default() | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't the stack frame of
ptr::write
also involve a stack allocation since it takes the argument by value? A more robust solution would be to usecopy_nonoverlapping
from the local inBox::default
directly to the heap.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From what I've observed,
ptr::write
doesn't actually create a new alloca, and essentially just callsmemcpy
directly on the argument(assuming the argument is passed is big enough and not something like a usize). Godbolt. Let me check how we lower theptr::write
intrinsic to make sure of this though.Unfortunately, we need to also
mem::forget
1 the actual value after copying it, or create aManuallyDrop<T>
. Themem::forget
copy doesn't appear to be eliminated in debug mode, andManuallyDrop::<T>::default()
call copiesT::default
to an additional alloca on its stack frame. I created two additional functions in the Godbolt link to showcase this codegen behavior.Footnotes
Technically we can skip this if
needs_drop::<T>()
is false. ↩