Skip to content

Commit 24b003c

Browse files
committed
Simplify swap and swap_simple
1 parent 7894bbf commit 24b003c

File tree

5 files changed

+207
-234
lines changed

5 files changed

+207
-234
lines changed

library/core/src/mem/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ pub const fn swap<T>(x: &mut T, y: &mut T) {
738738
// tends to copy the whole thing to stack rather than doing it one part
739739
// at a time, so instead treat them as one-element slices and piggy-back
740740
// the slice optimizations that will split up the swaps.
741-
if size_of::<T>() / align_of::<T>() > 4 {
741+
if const { size_of::<T>() / align_of::<T>() > 4 } {
742742
// SAFETY: exclusive references always point to one non-overlapping
743743
// element and are non-null and properly aligned.
744744
return unsafe { ptr::swap_nonoverlapping(x, y, 1) };
@@ -774,11 +774,14 @@ pub(crate) const fn swap_simple<T>(x: &mut T, y: &mut T) {
774774
// asymmetry to the behaviour where one value went through read+write
775775
// whereas the other was copied over by the intrinsic (see #94371).
776776

777+
let x: *mut T = x;
778+
let y: *mut T = y;
779+
777780
// SAFETY: exclusive references are always valid to read/write,
778781
// including being aligned, and nothing here panics so it's drop-safe.
779782
unsafe {
780-
let a = ptr::read(x);
781-
let b = ptr::read(y);
783+
let a = ptr::read_mut(x);
784+
let b = ptr::read_mut(y);
782785
ptr::write(x, b);
783786
ptr::write(y, a);
784787
}

tests/mir-opt/pre-codegen/mem_swap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -O -C debuginfo=0 -Zmir-opt-level=2 -Z inline-mir-hint-threshold=300
1+
// compile-flags: -O -C debuginfo=0 -Zmir-opt-level=2
22
// only-64bit
33
// ignore-debug
44

tests/mir-opt/pre-codegen/mem_swap.swap_big.PreCodegen.after.mir

+49-49
Original file line numberDiff line numberDiff line change
@@ -12,64 +12,64 @@ fn swap_big(_1: &mut [String; 9], _2: &mut [String; 9]) -> () {
1212
let mut _5: *mut [std::string::String; 9]; // in scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
1313
scope 2 {
1414
}
15-
scope 3 (inlined std::mem::size_of::<[String; 9]>) { // at $SRC_DIR/core/src/mem/mod.rs:LL:COL
16-
}
17-
scope 4 (inlined align_of::<[String; 9]>) { // at $SRC_DIR/core/src/mem/mod.rs:LL:COL
18-
}
19-
scope 5 (inlined mem::swap_simple::<[String; 9]>) { // at $SRC_DIR/core/src/mem/mod.rs:LL:COL
20-
debug x => _1; // in scope 5 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
21-
debug y => _2; // in scope 5 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
22-
let mut _7: *const [std::string::String; 9]; // in scope 5 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
23-
let mut _9: *const [std::string::String; 9]; // in scope 5 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
24-
let mut _10: *mut [std::string::String; 9]; // in scope 5 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
25-
let mut _11: *mut [std::string::String; 9]; // in scope 5 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
26-
scope 6 {
27-
let _6: [std::string::String; 9]; // in scope 6 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
28-
scope 7 {
29-
debug a => _6; // in scope 7 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
30-
let _8: [std::string::String; 9]; // in scope 7 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
31-
scope 8 {
32-
debug b => _8; // in scope 8 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
33-
scope 15 (inlined std::ptr::write::<[String; 9]>) { // at $SRC_DIR/core/src/mem/mod.rs:LL:COL
34-
debug dst => _10; // in scope 15 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
35-
debug src => _8; // in scope 15 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
36-
let mut _14: *mut [std::string::String; 9]; // in scope 15 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
37-
scope 16 {
38-
scope 17 (inlined std::ptr::write::runtime::<[String; 9]>) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL
39-
debug dst => _14; // in scope 17 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
15+
scope 3 (inlined mem::swap_simple::<[String; 9]>) { // at $SRC_DIR/core/src/mem/mod.rs:LL:COL
16+
debug x => _1; // in scope 3 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
17+
debug y => _2; // in scope 3 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
18+
let _6: *mut [std::string::String; 9]; // in scope 3 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
19+
scope 4 {
20+
debug x => _6; // in scope 4 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
21+
let _7: *mut [std::string::String; 9]; // in scope 4 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
22+
scope 5 {
23+
debug y => _7; // in scope 5 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
24+
scope 6 {
25+
let _8: [std::string::String; 9]; // in scope 6 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
26+
scope 7 {
27+
debug a => _8; // in scope 7 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
28+
let _9: [std::string::String; 9]; // in scope 7 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
29+
scope 8 {
30+
debug b => _9; // in scope 8 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
31+
scope 15 (inlined std::ptr::write::<[String; 9]>) { // at $SRC_DIR/core/src/mem/mod.rs:LL:COL
32+
debug dst => _6; // in scope 15 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
33+
debug src => _9; // in scope 15 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
34+
let mut _12: *mut [std::string::String; 9]; // in scope 15 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
35+
scope 16 {
36+
scope 17 (inlined std::ptr::write::runtime::<[String; 9]>) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL
37+
debug dst => _12; // in scope 17 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
38+
}
39+
}
40+
}
41+
scope 18 (inlined std::ptr::write::<[String; 9]>) { // at $SRC_DIR/core/src/mem/mod.rs:LL:COL
42+
debug dst => _7; // in scope 18 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
43+
debug src => _8; // in scope 18 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
44+
let mut _13: *mut [std::string::String; 9]; // in scope 18 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
45+
scope 19 {
46+
scope 20 (inlined std::ptr::write::runtime::<[String; 9]>) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL
47+
debug dst => _13; // in scope 20 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
48+
}
49+
}
4050
}
4151
}
42-
}
43-
scope 18 (inlined std::ptr::write::<[String; 9]>) { // at $SRC_DIR/core/src/mem/mod.rs:LL:COL
44-
debug dst => _11; // in scope 18 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
45-
debug src => _6; // in scope 18 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
46-
let mut _15: *mut [std::string::String; 9]; // in scope 18 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
47-
scope 19 {
48-
scope 20 (inlined std::ptr::write::runtime::<[String; 9]>) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL
49-
debug dst => _15; // in scope 20 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
52+
scope 12 (inlined ptr::read_mut::<[String; 9]>) { // at $SRC_DIR/core/src/mem/mod.rs:LL:COL
53+
debug src => _7; // in scope 12 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
54+
let mut _11: *mut [std::string::String; 9]; // in scope 12 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
55+
scope 13 {
56+
scope 14 (inlined ptr::read_mut::runtime::<[String; 9]>) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL
57+
debug src => _11; // in scope 14 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
58+
}
5059
}
5160
}
5261
}
53-
}
54-
scope 12 (inlined std::ptr::read::<[String; 9]>) { // at $SRC_DIR/core/src/mem/mod.rs:LL:COL
55-
debug src => _9; // in scope 12 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
56-
let mut _13: *const [std::string::String; 9]; // in scope 12 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
57-
scope 13 {
58-
scope 14 (inlined std::ptr::read::runtime::<[String; 9]>) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL
59-
debug src => _13; // in scope 14 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
62+
scope 9 (inlined ptr::read_mut::<[String; 9]>) { // at $SRC_DIR/core/src/mem/mod.rs:LL:COL
63+
debug src => _6; // in scope 9 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
64+
let mut _10: *mut [std::string::String; 9]; // in scope 9 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
65+
scope 10 {
66+
scope 11 (inlined ptr::read_mut::runtime::<[String; 9]>) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL
67+
debug src => _10; // in scope 11 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
68+
}
6069
}
6170
}
6271
}
6372
}
64-
scope 9 (inlined std::ptr::read::<[String; 9]>) { // at $SRC_DIR/core/src/mem/mod.rs:LL:COL
65-
debug src => _7; // in scope 9 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
66-
let mut _12: *const [std::string::String; 9]; // in scope 9 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
67-
scope 10 {
68-
scope 11 (inlined std::ptr::read::runtime::<[String; 9]>) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL
69-
debug src => _12; // in scope 11 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
70-
}
71-
}
72-
}
7373
}
7474
}
7575
}

0 commit comments

Comments
 (0)