Skip to content

Commit

Permalink
Replace loop over insert with splice
Browse files Browse the repository at this point in the history
This should be more efficient since Rust can calculate the final size
and thus allocate only once
  • Loading branch information
cschwan committed Apr 25, 2024
1 parent ca8f09c commit 270aedd
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions pineappl/src/packed_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use ndarray::ArrayView3;
use serde::{Deserialize, Serialize};
use std::iter;
use std::mem;
use std::ops::{Index, IndexMut, MulAssign};

Expand Down Expand Up @@ -204,10 +205,10 @@ impl<T: Clone + Copy + Default + PartialEq, const D: usize> IndexMut<[usize; D]>
} else if raveled_index < start_index + length + threshold_distance {
let distance = raveled_index - (start_index + length) + 1;
self.lengths[point - 1] += distance;

for _ in 0..distance {
self.entries.insert(point_entries, Default::default());
}
self.entries.splice(
point_entries..point_entries,
iter::repeat(Default::default()).take(distance),
);

if let Some(start_index_next) = self.start_indices.get(point) {
if raveled_index + threshold_distance >= *start_index_next {
Expand All @@ -216,9 +217,10 @@ impl<T: Clone + Copy + Default + PartialEq, const D: usize> IndexMut<[usize; D]>
self.lengths[point - 1] += distance_next - 1 + self.lengths[point];
self.lengths.remove(point);
self.start_indices.remove(point);
for _ in 0..(distance_next - 1) {
self.entries.insert(point_entries, Default::default());
}
self.entries.splice(
point_entries..point_entries,
iter::repeat(Default::default()).take(distance_next - 1),
);
}
}

Expand All @@ -232,9 +234,10 @@ impl<T: Clone + Copy + Default + PartialEq, const D: usize> IndexMut<[usize; D]>

self.start_indices[point] = raveled_index;
self.lengths[point] += distance;
for _ in 0..distance {
self.entries.insert(point_entries, Default::default());
}
self.entries.splice(
point_entries..point_entries,
iter::repeat(Default::default()).take(distance),
);
return &mut self.entries[point_entries];
}
}
Expand Down

0 comments on commit 270aedd

Please sign in to comment.