Skip to content
This repository was archived by the owner on Apr 29, 2020. It is now read-only.

Commit cca3531

Browse files
committed
perf: do not descend into subtrees
If maxDepth is specified and we have reached that depth, do not load subtrees as we already have the requested node(s)
1 parent b4e3a3b commit cca3531

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/dir-flat.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ function dirExporter (cid, node, name, path, pathRest, resolve, size, dag, paren
1818
type: 'dir'
1919
}
2020

21+
// we are at the max depth so no need to descend into children
22+
if (options.maxDepth && options.maxDepth <= depth) {
23+
return pull.values([dir])
24+
}
25+
2126
const streams = [
2227
pull(
2328
pull.values(node.links),
29+
pull.filter((item) => accepts === undefined || item.name === accepts),
2430
pull.map((link) => ({
2531
depth: depth + 1,
2632
size: link.size,
@@ -31,7 +37,6 @@ function dirExporter (cid, node, name, path, pathRest, resolve, size, dag, paren
3137
pathRest: pathRest.slice(1),
3238
type: 'dir'
3339
})),
34-
pull.filter((item) => accepts === undefined || item.linkName === accepts),
3540
resolve
3641
)
3742
]

src/dir-hamt-sharded.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ function shardedDirExporter (cid, node, name, path, pathRest, resolve, size, dag
1919
}
2020
}
2121

22+
// we are at the max depth so no need to descend into children
23+
if (options.maxDepth && options.maxDepth <= depth) {
24+
return pull.values([dir])
25+
}
26+
2227
const streams = [
2328
pull(
2429
pull.values(node.links),
@@ -31,6 +36,7 @@ function shardedDirExporter (cid, node, name, path, pathRest, resolve, size, dag
3136
if (p && pathRest.length) {
3237
accept = (p === pathRest[0])
3338
}
39+
3440
if (accept) {
3541
return {
3642
depth: p ? depth + 1 : depth,
@@ -49,6 +55,7 @@ function shardedDirExporter (cid, node, name, path, pathRest, resolve, size, dag
4955
)
5056
]
5157

58+
// place dir before if not specifying subtree
5259
if (!pathRest.length || options.fullPath) {
5360
streams.unshift(pull.values([dir]))
5461
}

0 commit comments

Comments
 (0)