Skip to content

Commit bf5a38f

Browse files
committed
perf: lazily process each 64 tasks
1 parent d81be04 commit bf5a38f

1 file changed

Lines changed: 32 additions & 5 deletions

File tree

src/tree_builder.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ pub mod info;
33
pub use info::Info;
44

55
use super::{data_tree::DataTree, size};
6-
use itertools::Itertools;
6+
use pipe_trait::Pipe;
77
use rayon::prelude::*;
8+
use std::{marker::PhantomData, ops::Not};
89

910
/// Collection of functions and starting points in order to build a [`DataTree`] with [`From`] or [`Into`].
1011
#[derive(Debug)]
@@ -56,10 +57,8 @@ where
5657

5758
let children = children
5859
.into_iter()
59-
.chunks(64)
60-
.into_iter()
61-
.map(Vec::<NameIter::Item>::from_iter)
62-
.map(|names| -> Vec<_> {
60+
.pipe(Chunks::<64, _>::new)
61+
.map(|names: Vec<_>| -> Vec<_> {
6362
names
6463
.into_par_iter()
6564
.map(|name| TreeBuilder {
@@ -85,3 +84,31 @@ where
8584
}
8685
}
8786
}
87+
88+
/// Utility type to iterate over each `Vec` of at most `LEN` items.
89+
#[derive(Debug, Clone, Copy)]
90+
struct Chunks<const LEN: usize, Iter> {
91+
iter: Iter,
92+
_phantom: PhantomData<[(); LEN]>,
93+
}
94+
95+
impl<const LEN: usize, Iter> Chunks<LEN, Iter> {
96+
/// Start iterating chunks.
97+
fn new(iter: Iter) -> Self {
98+
Chunks {
99+
iter,
100+
_phantom: PhantomData,
101+
}
102+
}
103+
}
104+
105+
impl<const LEN: usize, Iter> Iterator for Chunks<LEN, Iter>
106+
where
107+
Iter: Iterator,
108+
{
109+
type Item = Vec<Iter::Item>;
110+
fn next(&mut self) -> Option<Self::Item> {
111+
let chunk: Vec<_> = self.iter.by_ref().take(LEN).collect();
112+
chunk.is_empty().not().then_some(chunk)
113+
}
114+
}

0 commit comments

Comments
 (0)