Skip to content

Commit

Permalink
FEAT: Optimize shift_remove_index using MaybeUninit
Browse files Browse the repository at this point in the history
  • Loading branch information
bluss committed Apr 4, 2021
1 parent cc88042 commit 17cc606
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::mem::{size_of, ManuallyDrop};
use std::mem::{size_of, ManuallyDrop, MaybeUninit};
use alloc::slice;
use alloc::vec;
use alloc::vec::Vec;
Expand Down Expand Up @@ -2462,10 +2462,24 @@ where
Zip::from(tail.lanes_mut(axis)).for_each(|lane| {
// TODO give ArrayViewMut1 a split_first method?
let (mut fst, mut rest) = lane.split_at(Axis(0), 1);

// Logically we do a circular swap here, all elements in a chain
// Using MaybeUninit to avoid unecessary writes in the safe swap solution
//
// for elt in rest.iter_mut() {
// std::mem::swap(dst, elt);
// dst = elt;
// }
//
let mut slot = MaybeUninit::<A>::uninit();
let mut dst = &mut fst[0];
for elt in rest.iter_mut() {
std::mem::swap(dst, elt);
dst = elt;
unsafe {
slot.as_mut_ptr().copy_from_nonoverlapping(dst, 1);
for elt in rest.iter_mut() {
(dst as *mut A).copy_from_nonoverlapping(elt, 1);
dst = elt;
}
(dst as *mut A).copy_from_nonoverlapping(slot.as_ptr(), 1);
}
});
}
Expand Down

0 comments on commit 17cc606

Please sign in to comment.