perf(code-insight): read usage lines of many files in one call (#7598)

Usage lists read the line text of each file with one IPC call per file, which for ~1800 files took as long as the references request. fs_read_texts reads them in one call on a few threads (265-377 ms -> 57-59 ms on macOS); open editors and virtual documents keep using their editor text.

Refs #6823
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-03 03:20:18 +08:00
1 джерело f39b728947
коміт eace2fc741
5 files changed
+56 -15

No files matched your search

+5
Переглянути файл
@@ -919,6 +919,11 @@ export const commands = {
* definition). Prefers the unsaved buffer when the file is open.
*/
fsReadText: (path: string) => typedError<string, IdeError>(__TAURI_INVOKE("fs_read_text", { path })),
/**
* Reads many text files in one call, as `fs_read_text` does each (usage lists show a line from
* each of hundreds of files): `None` for one that can't be read.
*/
fsReadTexts: (paths: string[]) => typedError<(string | null)[], IdeError>(__TAURI_INVOKE("fs_read_texts", { paths })),
fsRename: (from: string, to: string) => typedError<null, IdeError>(__TAURI_INVOKE("fs_rename", { from, to })),
/** The existing file or directory a path written in `from` names (`href`, `url()`, a string literal). */
fsResolveReference: (from: IpcPath, value: string) => typedError<string | null, IdeError>(__TAURI_INVOKE("fs_resolve_reference", { from, value })),
+25 -2
Переглянути файл
@@ -24,13 +24,36 @@ export async function readFileLines(path: string): Promise<string[] | null> {
}
}
/** Reads several files with bounded concurrency; missing files map to `null`. */
/**
* Lines of several files; missing ones map to `null`. Files on disk come in one backend call:
* a usage list can name thousands, and one call per file is slow where IPC is (WebView2).
*/
export async function readManyFileLines(
paths: readonly string[],
concurrency = 4,
): Promise<Map<string, string[] | null>> {
const out = new Map<string, string[] | null>();
const queue = [...new Set(paths)];
const queue: string[] = [];
const onDisk: string[] = [];
for (const p of new Set(paths)) {
if (isTauri() && !editorsFor(p)[0] && !isVirtualUri(p)) onDisk.push(p);
else queue.push(p);
}
if (onDisk.length > 1) {
try {
const texts = await call(commands.fsReadTexts(onDisk));
onDisk.forEach((p, i) => {
const text = texts[i];
// Opened meanwhile: its editor has the newer text.
out.set(
p,
editorsFor(p)[0] ? editorsFor(p)[0]!.view.state.doc.toJSON() : text == null ? null : text.split(/\r\n?|\n/),
);
});
} catch {
queue.push(...onDisk);
}
} else queue.push(...onDisk);
const worker = async () => {
for (let p = queue.shift(); p !== undefined; p = queue.shift()) {
// oxlint-disable-next-line no-await-in-loop -- workers drain a shared queue; concurrency is bounded by the worker count
+1 -13
Переглянути файл
@@ -115,19 +115,7 @@ export async function previewText(path: string): Promise<string> {
return lines.join("\n");
}
export async function readManyFileLines(
paths: readonly string[],
concurrency = 4,
): Promise<Map<string, string[] | null>> {
const out = new Map<string, string[] | null>();
const queue = [...new Set(paths)];
const worker = async () => {
// oxlint-disable-next-line no-await-in-loop -- bounded worker pool: queue.shift() limits fan-out to `concurrency`
for (let p = queue.shift(); p !== undefined; p = queue.shift()) out.set(p, await readFileLines(p));
};
await Promise.all(Array.from({ length: Math.min(concurrency, queue.length) }, worker));
return out;
}
export { readManyFileLines } from "@/editor/lsp/files";
/**
* Runs `fn` with a backend document id for `path`, holding a reference only