From e9cb8ec4b259f0aae1d406a9bdc5575c17046746 Mon Sep 17 00:00:00 2001 From: Nicolas Braud-Santoni Date: Mon, 25 Nov 2019 23:12:07 +0700 Subject: [PATCH 1/2] construct::utils: Replace unecessary std::ptr::copy_nonoverlapping When copying from different slices, std::slice::copy_from_slice is always preferable: it implements bounds-checks, is safe, and also calls into memcpy. --- src/construct/utils.rs | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/construct/utils.rs b/src/construct/utils.rs index ef5ff66..8e35be6 100644 --- a/src/construct/utils.rs +++ b/src/construct/utils.rs @@ -62,13 +62,7 @@ pub fn suffixes_from_substrs<'s, T, F>( // 2. sort lms suffixes if k + 1 == tail.len() { // lms substrings => lms suffixes - unsafe { - std::ptr::copy_nonoverlapping( - &tail[0] as *const u32, - &mut head[0] as *mut u32, - tail.len(), - ); - } + &head[0..tail.len()].copy_from_slice(tail); } else { // construct sub-problem let mut t = 0; @@ -83,13 +77,8 @@ pub fn suffixes_from_substrs<'s, T, F>( sort(&mut head[..t], k, tail); // rearrange the lms suffixes - unsafe { - std::ptr::copy_nonoverlapping( - &tail[0] as *const u32, - &mut head[0] as *mut u32, - tail.len(), - ); - } + &head[0..tail.len()].copy_from_slice(tail); + let mut h = tail.len(); for_each_lms(s, true, |i, _| { h -= 1; From 416cbb8e6548706501c24a823e04b86be1926e42 Mon Sep 17 00:00:00 2001 From: Nicolas Braud-Santoni Date: Tue, 26 Nov 2019 11:04:07 +0700 Subject: [PATCH 2/2] construct::sacak::sacak_u32: Replace unnecessary std::ptr::copy slice::copy_within supports safely copying within a given slice, and is implemented with std::ptr::copy. --- src/construct/sacak/sacak_u32s/mod.rs | 29 +++++---------------------- 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/src/construct/sacak/sacak_u32s/mod.rs b/src/construct/sacak/sacak_u32s/mod.rs index a8478b0..68ac1df 100644 --- a/src/construct/sacak/sacak_u32s/mod.rs +++ b/src/construct/sacak/sacak_u32s/mod.rs @@ -122,13 +122,7 @@ fn sort_lms_suffixes(s: &[u32], sa: &mut [u32]) { sa[q] = tmp; } else { let t = q + 1 - m; - unsafe { - std::ptr::copy( - &sa[l] as *const u32, - &mut sa[t] as *mut u32, - m, - ); - } + sa.copy_within(l..l+m, t); sa[l..Ord::min(r, t)].iter_mut().for_each(|i| *i = EMPTY); } @@ -329,13 +323,7 @@ fn finish_head(sa: &mut [u32]) { for p in 1..sa.len() { if sa[p] > EMPTY { let n = -(sa[p] as i32) as usize; - unsafe { - std::ptr::copy( - &sa[p + 1] as *const u32, - &mut sa[p] as *mut u32, - n, - ); - } + sa.copy_within(p+1..p+n+1, p); sa[p + n] = EMPTY; } } @@ -347,13 +335,7 @@ fn finish_tail(sa: &mut [u32]) { for p in (1..sa.len()).rev() { if sa[p] > EMPTY { let n = -(sa[p] as i32) as usize; - unsafe { - std::ptr::copy( - &sa[p - n] as *const u32, - &mut sa[p - n + 1] as *mut u32, - n, - ); - } + sa.copy_within(p-n..p, p-n+1); sa[p - n] = EMPTY; } } @@ -367,9 +349,8 @@ fn sa_move( n: usize, ptr: &mut Option<&mut usize>, ) { - unsafe { - std::ptr::copy(&sa[src] as *const u32, &mut sa[dst] as *mut u32, n); - } + sa.copy_within(src..src+n, dst); + if let Some(p) = std::mem::replace(ptr, None) { if *p >= src && *p < src + n { if dst >= src {