about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDjzin <djzin@users.noreply.github.com>2017-03-13 20:03:10 +0000
committerDjzin <djzin@users.noreply.github.com>2017-03-13 20:06:07 +0000
commit1daf58964216dfe1f805cdaff76a91ca90d7523e (patch)
tree8350b96dd3defe1f7c93cb2d061ad40cf5496503
parentd1fec0d87a95310fcc1c59d72953ad6be89c78a5 (diff)
downloadrust-1daf58964216dfe1f805cdaff76a91ca90d7523e.tar.gz
rust-1daf58964216dfe1f805cdaff76a91ca90d7523e.zip
add SWAP_BLOCK_SIZE constant
-rw-r--r--src/libcore/mem.rs16
1 files changed, 9 insertions, 7 deletions
diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs
index 865ff3c6ee4..9a116e9041f 100644
--- a/src/libcore/mem.rs
+++ b/src/libcore/mem.rs
@@ -447,8 +447,10 @@ pub unsafe fn uninitialized<T>() -> T {
 #[stable(feature = "rust1", since = "1.0.0")]
 pub fn swap<T>(x: &mut T, y: &mut T) {
     unsafe {
+        const SWAP_BLOCK_SIZE: usize = 16;
+
         // Give ourselves some scratch space to work with
-        let mut t: [u8; 16] = uninitialized();
+        let mut t: [u8; SWAP_BLOCK_SIZE] = uninitialized();
 
         let x = x as *mut T as *mut u8;
         let y = y as *mut T as *mut u8;
@@ -457,12 +459,12 @@ pub fn swap<T>(x: &mut T, y: &mut 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;
+        while i + SWAP_BLOCK_SIZE as isize <= len {
+            // Perform the swap SWAP_BLOCK_SIZE bytes at a time, `&mut` pointers never alias
+            ptr::copy_nonoverlapping(x.offset(i), t, SWAP_BLOCK_SIZE);
+            ptr::copy_nonoverlapping(y.offset(i), x.offset(i), SWAP_BLOCK_SIZE);
+            ptr::copy_nonoverlapping(t, y.offset(i), SWAP_BLOCK_SIZE);
+            i += SWAP_BLOCK_SIZE as isize;
         }
         if i < len {
             // Swap any remaining bytes