-
Notifications
You must be signed in to change notification settings - Fork 13.3k
speed up mem::swap #40454
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
speed up mem::swap #40454
Changes from 4 commits
4bcfbc3
85049e5
5702f43
d1fec0d
1daf589
2816998
c6ca81a
165f366
ca2fa97
fcc970a
c6307a2
7475135
d4d3f53
8a973df
b795b7b
83f1f11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,7 +109,7 @@ pub use intrinsics::transmute; | |
/// [`Clone`][clone]. You need the value's destructor to run only once, | ||
/// because a double `free` is undefined behavior. | ||
/// | ||
/// An example is the definition of [`mem::swap`][swap] in this module: | ||
/// An example is the (old) definition of [`mem::swap`][swap] in this module: | ||
/// | ||
/// ``` | ||
/// use std::mem; | ||
|
@@ -448,17 +448,29 @@ pub unsafe fn uninitialized<T>() -> T { | |
pub fn swap<T>(x: &mut T, y: &mut T) { | ||
unsafe { | ||
// Give ourselves some scratch space to work with | ||
let mut t: T = uninitialized(); | ||
let mut t: [u8; 16] = uninitialized(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor point: Instead of using a literal const SWAP_BLOCK_SIZE: usize = 16; at the top of the function. This would make it slightly less error-prone to tweak the value later, and it would make it easy to, e.g., pick different sizes for different architectures. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a good suggestion, thanks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for reference, the actual block size didn't matter too much but that was just on my machine - 16 may very well not be optimal |
||
|
||
// Perform the swap, `&mut` pointers never alias | ||
ptr::copy_nonoverlapping(&*x, &mut t, 1); | ||
ptr::copy_nonoverlapping(&*y, x, 1); | ||
ptr::copy_nonoverlapping(&t, y, 1); | ||
let x = x as *mut T as *mut u8; | ||
let y = y as *mut T as *mut u8; | ||
let t = &mut t as *mut _ as *mut u8; | ||
|
||
// y and t now point to the same thing, but we need to completely | ||
// forget `t` because we do not want to run the destructor for `T` | ||
// on its value, which is still owned somewhere outside this function. | ||
forget(t); | ||
// can't use a for loop as the `range` impl calls `mem::swap` recursively | ||
let len = size_of::<T>() as isize; | ||
let mut i = 0; | ||
while i + 16 <= len { | ||
// Perform the swap 16 bytes at a time, `&mut` pointers never alias | ||
ptr::copy_nonoverlapping(x.offset(i), t, 16); | ||
ptr::copy_nonoverlapping(y.offset(i), x.offset(i), 16); | ||
ptr::copy_nonoverlapping(t, y.offset(i), 16); | ||
i += 16; | ||
} | ||
if i < len { | ||
// Swap any remaining bytes | ||
let rem = (len - i) as usize; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if it ever matters, but it occurs to me that calculating I doubt this matters in most cases, but I could see the optimizer failing when the size is large, since the loop might not be fully unrolled. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested it; looks like the generated assembly is the same either way. |
||
ptr::copy_nonoverlapping(x.offset(i), t, rem); | ||
ptr::copy_nonoverlapping(y.offset(i), x.offset(i), rem); | ||
ptr::copy_nonoverlapping(t, y.offset(i), rem); | ||
} | ||
} | ||
} | ||
|
||
|
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.
I would phrase this differently - maybe "An example is a possible implementation of mem::swap" or something like that.
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.
I agree we should not intimate the existance of a former impl.