The best way to control Claude Code, Codex and more from any computer or phone
perf(server): cut fff-node FFI marshaling overhead on large workspaces (#18514)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Max Weinbach committed
Oct 15, 2026 at 13:39 UTC
6e5322b6cf37521e4999f6137607983e95491fcc
3 files changed
+333
-5
apps/server/src/workspace/WorkspaceSearchIndex.native.test.ts
new
+115
@@ -0,0 +1,115 @@
1
+import * as NodeModule from "node:module";
2
+import type { Result } from "@ff-labs/fff-node";
3
+import * as NodeServices from "@effect/platform-node/NodeServices";
4
+import { expect, it } from "@effect/vitest";
5
+import * as Effect from "effect/Effect";
6
+import * as FileSystem from "effect/FileSystem";
7
+import * as Path from "effect/Path";
8
+
9
+// Use the same require export as WorkspaceSearchIndex, with the actual native library.
10
+const requireForFff = NodeModule.createRequire(import.meta.url);
11
+const { FileFinder } = requireForFff("@ff-labs/fff-node") as typeof import("@ff-labs/fff-node");
12
+
13
+function valueOf<T>(result: Result<T>): T {
14
+ if (!result.ok) throw new Error(result.error);
15
+ return result.value;
16
+}
17
+
18
+it.effect("decodes native search and grep strings and keeps scores only through 200 results", () =>
19
+ Effect.gen(function* () {
20
+ const fileSystem = yield* FileSystem.FileSystem;
21
+ const path = yield* Path.Path;
22
+ const basePath = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-fff-native-" });
23
+ const names = [
24
+ ...Array.from({ length: 205 }, (_, index) => `file-${String(index).padStart(3, "0")}`),
25
+ "日本語",
26
+ "café",
27
+ "emoji-🦊",
28
+ ];
29
+ const relativePaths = names.map((name) => `${name}/${name}.ts`);
30
+ const content = "prefix marker-日本語🦊\n";
31
+ yield* Effect.forEach(
32
+ names,
33
+ (name) =>
34
+ Effect.gen(function* () {
35
+ yield* fileSystem.makeDirectory(path.join(basePath, name));
36
+ yield* fileSystem.writeFileString(path.join(basePath, name, `${name}.ts`), content);
37
+ }),
38
+ { concurrency: "unbounded" },
39
+ );
40
+ expect(requireForFff.resolve("@ff-labs/fff-node").replaceAll("\\", "/")).toMatch(
41
+ /\/dist\/index\.js$/u,
42
+ );
43
+ const finder = yield* Effect.acquireRelease(
44
+ Effect.sync(() =>
45
+ valueOf(FileFinder.create({ basePath, disableWatch: true, disableMmapCache: true })),
46
+ ),
47
+ (finder) => Effect.sync(() => finder.destroy()),
48
+ );
49
+ expect(valueOf(yield* Effect.promise(() => finder.waitForIndexReady(10_000)))).toBe(true);
50
+
51
+ const files200 = valueOf(finder.fileSearch("", { pageSize: 200 }));
52
+ const files201 = valueOf(finder.fileSearch("", { pageSize: 201 }));
53
+ expect(files200.items).toHaveLength(200);
54
+ expect(files200.scores).toHaveLength(200);
55
+ expect(files201.items).toHaveLength(201);
56
+ expect(files201.scores).toEqual([]);
57
+ expect(files201.items.slice(0, 200)).toEqual(files200.items);
58
+ const files = valueOf(finder.fileSearch("", { pageSize: 1000 }));
59
+ expect(files.items.map((item) => item.relativePath.normalize("NFC")).toSorted()).toEqual(
60
+ relativePaths.toSorted(),
61
+ );
62
+ for (const item of files.items) {
63
+ expect(item.fileName.normalize("NFC")).toBe(
64
+ path.basename(item.relativePath).normalize("NFC"),
65
+ );
66
+ expect(item.size).toBe(Buffer.byteLength(content));
67
+ expect(Number.isFinite(item.modified)).toBe(true);
68
+ }
69
+ const exact = valueOf(finder.fileSearch("日本語.ts", { pageSize: 1 }));
70
+ expect(exact.items[0]?.fileName.normalize("NFC")).toBe("日本語.ts");
71
+ expect(exact.scores[0]).toMatchObject({ exactMatch: true, matchType: "exact_filename" });
72
+
73
+ const dirs200 = valueOf(finder.directorySearch("", { pageSize: 200 }));
74
+ const dirs201 = valueOf(finder.directorySearch("", { pageSize: 201 }));
75
+ expect(dirs200.scores).toHaveLength(200);
76
+ expect(dirs201.items).toHaveLength(201);
77
+ expect(dirs201.scores).toEqual([]);
78
+ expect(dirs201.items.slice(0, 200)).toEqual(dirs200.items);
79
+ const directories = valueOf(finder.directorySearch("", { pageSize: 1000 }));
80
+ expect(
81
+ directories.items
82
+ .map((item) => item.relativePath.normalize("NFC"))
83
+ .filter(Boolean)
84
+ .toSorted(),
85
+ ).toEqual(names.map((name) => `${name}/`).toSorted());
86
+
87
+ const mixed200 = valueOf(finder.mixedSearch("", { pageSize: 200 }));
88
+ const mixed201 = valueOf(finder.mixedSearch("", { pageSize: 201 }));
89
+ expect(mixed200.scores).toHaveLength(200);
90
+ expect(mixed201.items).toHaveLength(201);
91
+ expect(mixed201.scores).toEqual([]);
92
+ expect(mixed201.items.slice(0, 200)).toEqual(mixed200.items);
93
+ const mixed = valueOf(finder.mixedSearch("", { pageSize: 1000 }));
94
+ expect(mixed.items.filter((item) => item.type === "file").map(({ item }) => item)).toEqual(
95
+ files.items,
96
+ );
97
+ expect(mixed.items.filter((item) => item.type === "directory").map(({ item }) => item)).toEqual(
98
+ directories.items,
99
+ );
100
+
101
+ const grep = valueOf(finder.grep("marker", { mode: "plain", pageSize: 1000 }));
102
+ expect(grep.items.map((item) => item.relativePath.normalize("NFC")).toSorted()).toEqual(
103
+ relativePaths.toSorted(),
104
+ );
105
+ for (const item of grep.items) {
106
+ expect(item.fileName.normalize("NFC")).toBe(
107
+ path.basename(item.relativePath).normalize("NFC"),
108
+ );
109
+ expect(item.lineContent).toBe(content.trimEnd());
110
+ expect(item.lineNumber).toBe(1);
111
+ expect(item.col).toBe(7);
112
+ expect(item.matchRanges).toEqual([[7, 13]]);
113
+ }
114
+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
115
+);
patches/@ff-labs__fff-node@0.11.0.patch
+215
-2
@@ -1,5 +1,4 @@
1
diff --git a/package.json b/package.json
2
-index e000a78..8432546 100644
2
--- a/package.json
3
+++ b/package.json
4
@@ -11,7 +11,7 @@
@@ -11,8 +10,37 @@ index e000a78..8432546 100644
10
"default": "./dist/index.js"
11
},
12
"./package.json": "./package.json"
13
+diff --git a/dist/fff-api.d.ts b/dist/fff-api.d.ts
14
+--- a/dist/fff-api.d.ts
15
++++ b/dist/fff-api.d.ts
16
+@@ -199,7 +199,7 @@ export type Location = {
17
+ export interface SearchResult {
18
+ /** Matched file items */
19
+ items: FileItem[];
20
+- /** Corresponding scores for each item */
21
++ /** Corresponding scores for each item. Empty when the page has more than 200 items (patched in T3 Code to skip per-item score FFI calls). */
22
+ scores: Score[];
23
+ /** Total number of files that matched */
24
+ totalMatched: number;
25
+@@ -238,7 +238,7 @@ export interface DirSearchOptions {
26
+ export interface DirSearchResult {
27
+ /** Matched directory items */
28
+ items: DirItem[];
29
+- /** Corresponding scores for each item */
30
++ /** Corresponding scores for each item. Empty when the page has more than 200 items (patched in T3 Code to skip per-item score FFI calls). */
31
+ scores: Score[];
32
+ /** Total number of directories that matched */
33
+ totalMatched: number;
34
+@@ -262,7 +262,7 @@ export type MixedItem = {
35
+ export interface MixedSearchResult {
36
+ /** Matched items (files and directories interleaved by score) */
37
+ items: MixedItem[];
38
+- /** Corresponding scores for each item */
39
++ /** Corresponding scores for each item. Empty when the page has more than 200 items (patched in T3 Code to skip per-item score FFI calls). */
40
+ scores: Score[];
41
+ /** Total number of items (files + dirs) that matched */
42
+ totalMatched: number;
43
diff --git a/dist/index.js b/dist/index.js
15
-index 7c1d722..1d0bc27 100644
44
--- a/dist/index.js
45
+++ b/dist/index.js
46
@@ -1,7 +1,7 @@
@@ -50,3 +78,188 @@ index 7c1d722..1d0bc27 100644
78
}
79
} catch {
80
}
81
+@@ -262,6 +272,7 @@ function snakeToCamel(obj) {
82
+ return result;
83
+ }
84
+ function readCString(ptr) {
85
++ if (typeof ptr === "string") return ptr;
86
+ if (isNullPointer(ptr)) return null;
87
+ try {
88
+ const [str] = restorePointer({
89
+@@ -426,9 +437,9 @@ function ffiDestroy(handle) {
90
+ });
91
+ }
92
+ var FFF_FILE_ITEM_STRUCT = {
93
+- relative_path: DataType.External,
94
+- file_name: DataType.External,
95
+- git_status: DataType.External,
96
++ relative_path: DataType.String,
97
++ file_name: DataType.String,
98
++ git_status: DataType.String,
99
+ size: DataType.U64,
100
+ modified: DataType.U64,
101
+ access_frecency_score: DataType.I64,
102
+@@ -445,8 +456,9 @@ var FFF_SCORE_STRUCT = {
103
+ distance_penalty: DataType.I32,
104
+ current_file_penalty: DataType.I32,
105
+ combo_match_boost: DataType.I32,
106
++ path_alignment_bonus: DataType.I32,
107
+ exact_match: DataType.U8,
108
+- match_type: DataType.External
109
++ match_type: DataType.String
110
+ };
111
+ var FFF_SEARCH_RESULT_STRUCT = {
112
+ items: DataType.External,
113
+@@ -462,8 +474,8 @@ var FFF_SEARCH_RESULT_STRUCT = {
114
+ location_end_col: DataType.I32
115
+ };
116
+ var FFF_DIR_ITEM_STRUCT = {
117
+- relative_path: DataType.External,
118
+- dir_name: DataType.External,
119
++ relative_path: DataType.String,
120
++ dir_name: DataType.String,
121
+ max_access_frecency: DataType.I32
122
+ };
123
+ var FFF_DIR_SEARCH_RESULT_STRUCT = {
124
+@@ -475,9 +487,9 @@ var FFF_DIR_SEARCH_RESULT_STRUCT = {
125
+ };
126
+ var FFF_MIXED_ITEM_STRUCT = {
127
+ item_type: DataType.U8,
128
+- relative_path: DataType.External,
129
+- display_name: DataType.External,
130
+- git_status: DataType.External,
131
++ relative_path: DataType.String,
132
++ display_name: DataType.String,
133
++ git_status: DataType.String,
134
+ size: DataType.U64,
135
+ modified: DataType.U64,
136
+ access_frecency_score: DataType.I64,
137
+@@ -500,10 +512,10 @@ var FFF_MIXED_SEARCH_RESULT_STRUCT = {
138
+ location_end_col: DataType.I32
139
+ };
140
+ var FFF_GREP_MATCH_STRUCT = {
141
+- relative_path: DataType.External,
142
+- file_name: DataType.External,
143
+- git_status: DataType.External,
144
+- line_content: DataType.External,
145
++ relative_path: DataType.String,
146
++ file_name: DataType.String,
147
++ git_status: DataType.String,
148
++ line_content: DataType.String,
149
+ match_ranges: DataType.External,
150
+ context_before: DataType.External,
151
+ context_after: DataType.External,
152
+@@ -598,18 +610,13 @@ function readMixedItemFromRaw(raw) {
153
+ }
154
+ function callAccessor(funcName, resultPtr, index, structDef) {
155
+ loadLibrary();
156
+- const elemPtr = load({
157
++ return load({
158
+ library: LIBRARY_KEY,
159
+ funcName,
160
+- retType: DataType.External,
161
++ retType: structDef,
162
+ paramsType: [DataType.External, DataType.U32],
163
+ paramsValue: [resultPtr, index]
164
+ });
165
+- const [raw] = restorePointer({
166
+- retType: [structDef],
167
+- paramsValue: wrapPointer([elemPtr])
168
+- });
169
+- return raw;
170
+ }
171
+ function ptrOffset(base, bytes) {
172
+ return load({
173
+@@ -766,6 +773,7 @@ function parseSearchResult(rawPtr) {
174
+ }
175
+ const items = [];
176
+ const scores = [];
177
++ const includeScores = count <= 200;
178
+ for (let i = 0; i < count; i++) {
179
+ const rawItem = callAccessor(
180
+ "fff_search_result_get_item",
181
+@@ -774,13 +782,15 @@ function parseSearchResult(rawPtr) {
182
+ FFF_FILE_ITEM_STRUCT
183
+ );
184
+ items.push(readFileItemFromRaw(rawItem));
185
+- const rawScore = callAccessor(
186
+- "fff_search_result_get_score",
187
+- handlePtr,
188
+- i,
189
+- FFF_SCORE_STRUCT
190
+- );
191
+- scores.push(readScoreFromRaw(rawScore));
192
++ if (includeScores) {
193
++ const rawScore = callAccessor(
194
++ "fff_search_result_get_score",
195
++ handlePtr,
196
++ i,
197
++ FFF_SCORE_STRUCT
198
++ );
199
++ scores.push(readScoreFromRaw(rawScore));
200
++ }
201
+ }
202
+ load({
203
+ library: LIBRARY_KEY,
204
+@@ -824,6 +834,7 @@ function parseDirSearchResult(rawPtr) {
205
+ const count = sr.count;
206
+ const items = [];
207
+ const scores = [];
208
++ const includeScores = count <= 200;
209
+ for (let i = 0; i < count; i++) {
210
+ const rawItem = callAccessor(
211
+ "fff_dir_search_result_get_item",
212
+@@ -832,13 +843,15 @@ function parseDirSearchResult(rawPtr) {
213
+ FFF_DIR_ITEM_STRUCT
214
+ );
215
+ items.push(readDirItemFromRaw(rawItem));
216
+- const rawScore = callAccessor(
217
+- "fff_dir_search_result_get_score",
218
+- handlePtr,
219
+- i,
220
+- FFF_SCORE_STRUCT
221
+- );
222
+- scores.push(readScoreFromRaw(rawScore));
223
++ if (includeScores) {
224
++ const rawScore = callAccessor(
225
++ "fff_dir_search_result_get_score",
226
++ handlePtr,
227
++ i,
228
++ FFF_SCORE_STRUCT
229
++ );
230
++ scores.push(readScoreFromRaw(rawScore));
231
++ }
232
+ }
233
+ load({
234
+ library: LIBRARY_KEY,
235
+@@ -897,6 +910,7 @@ function parseMixedSearchResult(rawPtr) {
236
+ }
237
+ const items = [];
238
+ const scores = [];
239
++ const includeScores = count <= 200;
240
+ for (let i = 0; i < count; i++) {
241
+ const rawItem = callAccessor(
242
+ "fff_mixed_search_result_get_item",
243
+@@ -905,13 +919,15 @@ function parseMixedSearchResult(rawPtr) {
244
+ FFF_MIXED_ITEM_STRUCT
245
+ );
246
+ items.push(readMixedItemFromRaw(rawItem));
247
+- const rawScore = callAccessor(
248
+- "fff_mixed_search_result_get_score",
249
+- handlePtr,
250
+- i,
251
+- FFF_SCORE_STRUCT
252
+- );
253
+- scores.push(readScoreFromRaw(rawScore));
254
++ if (includeScores) {
255
++ const rawScore = callAccessor(
256
++ "fff_mixed_search_result_get_score",
257
++ handlePtr,
258
++ i,
259
++ FFF_SCORE_STRUCT
260
++ );
261
++ scores.push(readScoreFromRaw(rawScore));
262
++ }
263
+ }
264
+ load({
265
+ library: LIBRARY_KEY,
pnpm-lock.yaml
+3
-3
@@ -146,7 +146,7 @@ patchedDependencies:
146
'@clerk/expo@4.6.8': b8b22ab733de5bea846ad9e88046c10582352fe9ad86f314f368b2a823f0f281
147
'@effect/vitest@4.0.4': ebaec66c6bdd25e1b8d5bfd4a14f25ab959bdcca6e22ed8871d061e50e2db0ad
148
'@expo/metro-config@58.0.6': 90fa980e8b6480631fb2fbb04559834e3c612db40c550cd828f11be297d8d760
149
- '@ff-labs/fff-node@0.11.0': 8956d6ed6938a1bce69b2d0b067ff258dd9ca753c5bbe7006f7b68f760ea3ad2
149
+ '@ff-labs/fff-node@0.11.0': cc71fcec015f4d39bc96ae2e2bf6b0ca678124c722c66a7841efce9d34b5983c
150
'@legendapp/list@3.3.5': 680cc6a5c5b4a4032e467e7b3fde22f89a84c0ee2e6eac6fda737d6277cc0806
151
'@pierre/diffs@1.5.2': 298e94372dbd51846a4e26ece2ecc3e22eb1f94b0450b1f1a9c6fec127809be7
152
'@react-native-ai/apple@0.12.0': 2d09870c2848d185cb05b53ed823a46e12dba519324d8dd8e584e28731990f9d
@@ -620,7 +620,7 @@ importers:
620
version: 4.0.4(bufferutil@4.1.0)(effect@4.0.4)(utf-8-validate@6.0.6)
621
'@ff-labs/fff-node':
622
specifier: 0.11.0
623
- version: 0.11.0(patch_hash=8956d6ed6938a1bce69b2d0b067ff258dd9ca753c5bbe7006f7b68f760ea3ad2)
623
+ version: 0.11.0(patch_hash=cc71fcec015f4d39bc96ae2e2bf6b0ca678124c722c66a7841efce9d34b5983c)
624
'@muse-code/sdk':
625
specifier: 1.4.6
626
version: 1.4.6(@types/node@24.12.4)
@@ -14786,7 +14786,7 @@ snapshots:
14786
'@ff-labs/fff-bin-win32-x64@0.11.0':
14787
optional: true
14788
14789
- '@ff-labs/fff-node@0.11.0(patch_hash=8956d6ed6938a1bce69b2d0b067ff258dd9ca753c5bbe7006f7b68f760ea3ad2)':
14789
+ '@ff-labs/fff-node@0.11.0(patch_hash=cc71fcec015f4d39bc96ae2e2bf6b0ca678124c722c66a7841efce9d34b5983c)':
14790
dependencies:
14791
ffi-rs: 1.3.2
14792
optionalDependencies: