Skip to content

perf: use Zip instead of manual for loop #47

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions downsample_rs/src/minmax/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,27 @@ pub(crate) fn min_max_generic<T: Copy>(
let block_size: f64 = (arr.len() - 1) as f64 / (n_out / 2) as f64;
let arr_ptr = arr.as_ptr();

let mut sampled_indices: Array1<usize> = Array1::<usize>::default(n_out);
// Store the enumerated indexes in the output array
let mut sampled_indices: Array1<usize> = Array1::from_vec((0..n_out).collect::<Vec<usize>>());

let mut start_idx: usize = 0;
for i in 0..n_out / 2 {
// Decided to use multiplication instead of adding to the accumulator (end)
// as multiplication seems to be less prone to rounding errors.
let end: f64 = block_size * (i + 1) as f64;
let end_idx: usize = end as usize + 1;
Zip::from(sampled_indices.exact_chunks_mut(2)).for_each(|mut sampled_index| {
let i: f64 = unsafe { *sampled_index.uget(0) >> 1 } as f64;
let start_idx: usize = (block_size * i) as usize + (i != 0.0) as usize;
let end_idx: usize = (block_size * (i + 1.0)) as usize + 1;

let (min_index, max_index) = f_argminmax(unsafe {
ArrayView1::from_shape_ptr((end_idx - start_idx,), arr_ptr.add(start_idx))
ArrayView1::from_shape_ptr((end_idx - start_idx,), arr.as_ptr().add(start_idx))
});

// Add the indexes in sorted order
if min_index < max_index {
sampled_indices[2 * i] = min_index + start_idx;
sampled_indices[2 * i + 1] = max_index + start_idx;
sampled_index[0] = min_index + start_idx;
sampled_index[1] = max_index + start_idx;
} else {
sampled_indices[2 * i] = max_index + start_idx;
sampled_indices[2 * i + 1] = min_index + start_idx;
sampled_index[0] = max_index + start_idx;
sampled_index[1] = min_index + start_idx;
}

start_idx = end_idx;
}
});

sampled_indices
}
Expand Down