about summary refs log tree commit diff
path: root/tests/codegen
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2025-02-21 12:45:22 +0100
committerGitHub <noreply@github.com>2025-02-21 12:45:22 +0100
commit8d52aae968e8a4822e3a4c902c148a2280657eb6 (patch)
tree0e2b270af0dd12a7f704f8bfd578d820cb72c6a1 /tests/codegen
parente67d4499a61cfe5c4e6ff257e05ced2de652d5b6 (diff)
parent97005678c38fd391c9b502d011cc3f3d4434a18a (diff)
downloadrust-8d52aae968e8a4822e3a4c902c148a2280657eb6.tar.gz
rust-8d52aae968e8a4822e3a4c902c148a2280657eb6.zip
Rollup merge of #136089 - jwong101:box-default-debug-stack-usage, r=Amanieu
Reduce `Box::default` stack copies in debug mode

The `Box::new(T::default())` implementation of `Box::default` only
had two stack copies in debug mode, compared to the current version,
which has four. By avoiding creating any `MaybeUninit<T>`'s and just writing
`T` directly to the `Box` pointer, the stack usage in debug mode remains
the same as the old version.

Another option would be to mark `Box::write` as `#[inline(always)]`,
and change it's implementation to to avoid calling `MaybeUninit::write`
(which creates a `MaybeUninit<T>` on the stack) and to use `ptr::write` instead.

Fixes: #136043
Diffstat (limited to 'tests/codegen')
-rw-r--r--tests/codegen/box-default-debug-copies.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/tests/codegen/box-default-debug-copies.rs b/tests/codegen/box-default-debug-copies.rs
new file mode 100644
index 00000000000..06cc41b21c0
--- /dev/null
+++ b/tests/codegen/box-default-debug-copies.rs
@@ -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()
+}