Skip to content

Commit 52e4071

Browse files
committed
fix(perf): actually make it different
1 parent bf5a38f commit 52e4071

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

src/tree_builder.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ where
5858
let children = children
5959
.into_iter()
6060
.pipe(Chunks::<64, _>::new)
61+
.par_bridge()
6162
.map(|names: Vec<_>| -> Vec<_> {
6263
names
6364
.into_par_iter()
@@ -71,10 +72,7 @@ where
7172
.map(Self::from)
7273
.collect()
7374
})
74-
.fold(Vec::new(), |mut a, b| {
75-
a.extend(b);
76-
a
77-
});
75+
.reduce(Vec::new, add_short_vec_to_long);
7876

7977
if max_depth > 0 {
8078
DataTree::dir(name, size, children)
@@ -85,6 +83,17 @@ where
8583
}
8684
}
8785

86+
/// Concat 2 `Vec`s in any order with minimal copying.
87+
fn add_short_vec_to_long<Item>(mut a: Vec<Item>, mut b: Vec<Item>) -> Vec<Item> {
88+
if a.len() > b.len() {
89+
a.extend(b);
90+
a
91+
} else {
92+
b.extend(a);
93+
b
94+
}
95+
}
96+
8897
/// Utility type to iterate over each `Vec` of at most `LEN` items.
8998
#[derive(Debug, Clone, Copy)]
9099
struct Chunks<const LEN: usize, Iter> {

0 commit comments

Comments
 (0)