From c7838510497102a5f502aaf038e10b11c5dfaa2a Mon Sep 17 00:00:00 2001 From: Dikshant Date: Mon, 19 Aug 2024 15:09:24 +0530 Subject: [PATCH] removed redundant copy from copy, sort Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 25 ++++--------------------- hpx-sys/src/lib.rs | 19 +++++-------------- 2 files changed, 9 insertions(+), 35 deletions(-) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index 42e2af1..8959b71 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -34,17 +34,8 @@ inline std::int32_t disconnect_with_timeout(double shutdown_timeout, double loca inline std::int32_t finalize() { return hpx::finalize(); } -inline void hpx_copy(const rust::Vec& src, rust::Vec& dest) { - std::vector cpp_src(src.begin(), src.end()); - std::vector cpp_dest(dest.size()); - - hpx::copy(hpx::execution::par, cpp_src.begin(), cpp_src.end(), cpp_dest.begin()); - - dest.clear(); - dest.reserve(cpp_dest.size()); - for (const auto& item : cpp_dest) { - dest.push_back(item); - } +inline void hpx_copy(rust::Slice src, rust::Slice dest) { + hpx::copy(hpx::execution::par, src.begin(), src.end(), dest.begin()); } inline void hpx_copy_n(const rust::Vec& src, size_t count, rust::Vec& dest) { @@ -137,16 +128,8 @@ inline int64_t hpx_find(const rust::Vec& src, int32_t value) { return -1; } -inline void hpx_sort(rust::Vec& src) { - std::vector cpp_vec(src.begin(), src.end()); - - hpx::sort(hpx::execution::par, cpp_vec.begin(), cpp_vec.end()); - - src.clear(); - src.reserve(cpp_vec.size()); - for (const auto& item : cpp_vec) { - src.push_back(item); - } +inline void hpx_sort(rust::Slice src) { + hpx::sort(hpx::execution::par, src.begin(), src.end()); } inline void hpx_sort_comp(rust::Vec& src, rust::Fn comp) { diff --git a/hpx-sys/src/lib.rs b/hpx-sys/src/lib.rs index 208ebb8..fd9991a 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -18,7 +18,7 @@ pub mod ffi { fn terminate(); fn disconnect() -> i32; fn disconnect_with_timeout(shutdown_timeout: f64, localwait: f64) -> i32; - fn hpx_copy(src: &Vec, dest: &mut Vec); + fn hpx_copy(src: &[i32], dest: &mut [i32]); fn hpx_copy_n(src: &Vec, count: usize, dest: &mut Vec); fn hpx_copy_if(src: &Vec, dest: &mut Vec, pred: fn(i32) -> bool); fn hpx_count(src: &Vec, value: i32) -> i64; @@ -27,7 +27,7 @@ pub mod ffi { fn hpx_equal(slice1: &[i32], slice2: &[i32]) -> bool; fn hpx_fill(src: &mut Vec, value: i32); // will only work for linear vectors fn hpx_find(src: &Vec, value: i32) -> i64; - fn hpx_sort(src: &mut Vec); + fn hpx_sort(src: &mut [i32]); fn hpx_sort_comp(src: &mut Vec, comp: fn(i32, i32) -> bool); fn hpx_merge(src1: &Vec, src2: &Vec, dest: &mut Vec); fn hpx_partial_sort(src: &mut Vec, last: usize); @@ -48,19 +48,12 @@ pub fn create_c_args(args: &[&str]) -> (i32, Vec<*mut c_char>) { (ptrs.len() as i32, ptrs) } -pub fn copy_vector(src: &Vec) -> Vec { +pub fn copy_vector(src: &[i32]) -> Vec { let mut dest = vec![0; src.len()]; ffi::hpx_copy(src, &mut dest); dest } -pub fn copy_vector_range(src: &Vec, start: usize, end: usize) -> Vec { - let slice = &src[start..end]; - let mut dest = vec![0; slice.len()]; - ffi::hpx_copy(&slice.to_vec(), &mut dest); - dest -} - pub fn copy_n(src: &[i32], count: usize) -> Vec { let mut dest = Vec::with_capacity(count); ffi::hpx_copy_n(&src.to_vec(), count, &mut dest); @@ -90,9 +83,7 @@ pub fn find(vec: &Vec, value: i32) -> Option { #[cfg(test)] mod tests { use super::ffi; - use crate::{ - copy_if_positive, copy_n, copy_vector, copy_vector_range, count, create_c_args, find, - }; + use crate::{copy_if_positive, copy_n, copy_vector, count, create_c_args, find}; use serial_test::serial; use std::ffi::CString; use std::os::raw::c_char; @@ -140,7 +131,7 @@ mod tests { let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { let src = vec![1, 2, 3, 4, 5]; - let result = copy_vector_range(&src, 0, 3); + let result = copy_vector(&src[0..3]); assert_eq!(&src[0..3], &result); ffi::finalize() };