The best way to control Claude Code, Codex and more from any computer or phone
perf(mobile): flatten large file trees without copying descendants (#21276)
Co-authored-by: maria-rcks <maria@kuuro.net>
luke committed
Oct 29, 2026 at 17:54 UTC
781cc55c2d182566fbf650abc08d0ad6e67a7fbf
2 files changed
+65
-4
apps/mobile/src/features/files/fileTree.test.ts
new
+61
@@ -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
+4
-4
@@ -100,11 +100,12 @@ function flattenNode(
100
const isSearching = searchResultPaths !== null;
101
const matches = searchResultPaths?.has(node.path) ?? false;
102
let descendantMatches = false;
103
- const childOutput: VisibleFileTreeNode[] = [];
103
+ const start = output.length;
104
+ output.push({ node, depth });
105
106
if (node.kind === "directory" && (expanded.has(node.path) || isSearching)) {
107
for (const child of node.children) {
107
- if (flattenNode(childOutput, child, depth + 1, expanded, searchResultPaths)) {
108
+ if (flattenNode(output, child, depth + 1, expanded, searchResultPaths)) {
109
descendantMatches = true;
110
}
111
}
@@ -112,11 +113,10 @@ function flattenNode(
113
114
const visible = !isSearching || matches || descendantMatches;
115
if (!visible) {
116
+ output.length = start;
117
return false;
118
}
119
118
- output.push({ node, depth });
119
- output.push(...childOutput);
120
return matches || descendantMatches;
121
}
122