Skip to content

Commit 781cc55

Browse files
luke2xmaria-rcks
andauthored
perf(mobile): flatten large file trees without copying descendants (pingdotgg#21276)
Co-authored-by: maria-rcks <[email protected]>
1 parent 1e3e3a8 commit 781cc55

2 files changed

Lines changed: 65 additions & 4 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { expect, it } from "vite-plus/test";
2+
3+
import { buildFileTree, flattenFileTree } from "./fileTree";
4+
5+
it("flattens a wide expanded directory without exceeding argument limits", () => {
6+
const children = Array.from({ length: 200_000 }, (_, index) => ({
7+
path: `root/file-${index}`,
8+
name: `file-${index}`,
9+
kind: "file" as const,
10+
children: [],
11+
}));
12+
const nodes = [{ path: "root", name: "root", kind: "directory" as const, children }];
13+
const rows = flattenFileTree({ nodes, expanded: new Set(["root"]) });
14+
expect(rows).toHaveLength(children.length + 1);
15+
expect(rows[0]).toEqual({ node: nodes[0], depth: 0 });
16+
const middleChildIndex = Math.floor(children.length / 2);
17+
expect(rows[middleChildIndex + 1]).toEqual({
18+
node: children[middleChildIndex],
19+
depth: 1,
20+
});
21+
expect(rows.at(-1)).toEqual({ node: children.at(-1), depth: 1 });
22+
});
23+
24+
it("prunes unmatched subtrees without losing adjacent rows or their depths", () => {
25+
const nodes = buildFileTree([
26+
{ kind: "file", path: "root/a/hidden.txt" },
27+
{ kind: "file", path: "root/b/match.txt" },
28+
{ kind: "file", path: "root/b/other.txt" },
29+
{ kind: "file", path: "root/c/hidden.txt" },
30+
{ kind: "file", path: "tail.txt" },
31+
]);
32+
const expanded = new Set(["root", "root/a", "root/b", "root/c"]);
33+
const rows = (searchResultPaths: ReadonlySet<string> | null) =>
34+
flattenFileTree({ nodes, expanded, searchResultPaths }).map(
35+
({ node, depth }) => `${depth}:${node.path}`,
36+
);
37+
38+
expect(rows(new Set(["root/b/match.txt", "tail.txt"]))).toEqual([
39+
"0:root",
40+
"1:root/b",
41+
"2:root/b/match.txt",
42+
"0:tail.txt",
43+
]);
44+
expect(rows(new Set(["root/b"]))).toEqual(["0:root", "1:root/b"]);
45+
expect(rows(new Set())).toEqual([]);
46+
expect(rows(null)).toEqual([
47+
"0:root",
48+
"1:root/a",
49+
"2:root/a/hidden.txt",
50+
"1:root/b",
51+
"2:root/b/match.txt",
52+
"2:root/b/other.txt",
53+
"1:root/c",
54+
"2:root/c/hidden.txt",
55+
"0:tail.txt",
56+
]);
57+
expect(flattenFileTree({ nodes, expanded: new Set() }).map(({ node }) => node.path)).toEqual([
58+
"root",
59+
"tail.txt",
60+
]);
61+
});

‎apps/mobile/src/features/files/fileTree.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,23 @@ function flattenNode(
100100
const isSearching = searchResultPaths !== null;
101101
const matches = searchResultPaths?.has(node.path) ?? false;
102102
let descendantMatches = false;
103-
const childOutput: VisibleFileTreeNode[] = [];
103+
const start = output.length;
104+
output.push({ node, depth });
104105

105106
if (node.kind === "directory" && (expanded.has(node.path) || isSearching)) {
106107
for (const child of node.children) {
107-
if (flattenNode(childOutput, child, depth + 1, expanded, searchResultPaths)) {
108+
if (flattenNode(output, child, depth + 1, expanded, searchResultPaths)) {
108109
descendantMatches = true;
109110
}
110111
}
111112
}
112113

113114
const visible = !isSearching || matches || descendantMatches;
114115
if (!visible) {
116+
output.length = start;
115117
return false;
116118
}
117119

118-
output.push({ node, depth });
119-
output.push(...childOutput);
120120
return matches || descendantMatches;
121121
}
122122

0 commit comments

Comments
 (0)