Skip to content

Commit d16f215

Browse files
authored
perf: skip creating children beyond depth (#284)
1 parent c747dcb commit d16f215

File tree

2 files changed

+7
-13
lines changed

2 files changed

+7
-13
lines changed

src/data_tree/constructors.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,4 @@ impl<Name, Size: size::Size> DataTree<Name, Size> {
2828
{
2929
move |name, children| DataTree::dir(name, inode_size, children)
3030
}
31-
32-
/// Remove the children. Free memory usage.
33-
pub(crate) fn drop_children(&mut self) {
34-
self.children = Vec::new();
35-
}
3631
}

src/tree_builder.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ where
4949
let Info { size, children } = get_info(&path);
5050
let max_depth = max_depth.saturating_sub(1);
5151

52-
let children: Vec<_> = children
52+
let children = children
5353
.into_par_iter()
5454
.map(|name| TreeBuilder {
5555
path: join_path(&path, &name),
@@ -58,14 +58,13 @@ where
5858
join_path,
5959
max_depth,
6060
})
61-
.map(Self::from)
62-
.collect(); // TODO: this collect can be called into different types depending on whether `max_depth` is 0
61+
.map(Self::from);
6362

64-
let mut tree = DataTree::dir(name, size, children);
65-
if max_depth == 0 {
66-
// TODO: replace this with a more memory efficient method that doesn't require constructing `children` in the first place
67-
tree.drop_children();
63+
if max_depth > 0 {
64+
DataTree::dir(name, size, children.collect())
65+
} else {
66+
let size = size + children.map(|child| child.size()).sum();
67+
DataTree::dir(name, size, Vec::new())
6868
}
69-
tree
7069
}
7170
}

0 commit comments

Comments
 (0)