From 50a2460eb12cb2688c15b275aaf963aa7d3f2717 Mon Sep 17 00:00:00 2001 From: Lachlan Deakin Date: Mon, 26 Feb 2024 12:49:02 +1100 Subject: [PATCH] Remove an unnecessary allocation in `IndicesIterator` --- CHANGELOG.md | 3 +++ Cargo.toml | 2 +- src/array_subset/iterators/indices_iterator.rs | 18 ++++++------------ 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a41be4e..7ef8ae47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Disabled `blosc` codec partial decoding +### Fixed + - Remove an unnecessary allocation in `IndicesIterator` + ## [0.12.1] - 2024-02-24 ### Added diff --git a/Cargo.toml b/Cargo.toml index d25ca130..a64400c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "zarrs" -version = "0.12.1" +version = "0.12.2" authors = ["Lachlan Deakin "] edition = "2021" rust-version = "1.71" diff --git a/src/array_subset/iterators/indices_iterator.rs b/src/array_subset/iterators/indices_iterator.rs index 858e1f0f..44cd3a5a 100644 --- a/src/array_subset/iterators/indices_iterator.rs +++ b/src/array_subset/iterators/indices_iterator.rs @@ -142,12 +142,9 @@ impl Iterator for IndicesIterator<'_> { type Item = ArrayIndices; fn next(&mut self) -> Option { - let indices = std::iter::zip( - unravel_index(self.index_front, self.subset.shape()).iter(), - self.subset.start(), - ) - .map(|(index, start)| index + start) - .collect::>(); + let mut indices = unravel_index(self.index_front, self.subset.shape()); + std::iter::zip(indices.iter_mut(), self.subset.start()) + .for_each(|(index, start)| *index += start); if self.index_front < self.index_back { self.index_front += 1; @@ -166,12 +163,9 @@ impl DoubleEndedIterator for IndicesIterator<'_> { fn next_back(&mut self) -> Option { if self.index_back > self.index_front { self.index_back -= 1; - let indices = std::iter::zip( - unravel_index(self.index_back, self.subset.shape()).iter(), - self.subset.start(), - ) - .map(|(index, start)| index + start) - .collect::>(); + let mut indices = unravel_index(self.index_back, self.subset.shape()); + std::iter::zip(indices.iter_mut(), self.subset.start()) + .for_each(|(index, start)| *index += start); Some(indices) } else { None