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

+24
Переглянути файл
@@ -439,6 +439,29 @@ pub async fn fs_read_text(host: &Host, window: &str, path: PathBuf) -> IdeResult
.await
}
/// 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.
#[command(trust = always)]
pub async fn fs_read_texts(host: &Host, window: &str, paths: Vec<PathBuf>) -> IdeResult<Vec<Option<String>>> {
let project = host.project(window).ok();
host.blocking(move || {
let read = |path: &PathBuf| match &project {
Some(p) => p.text_of(path).ok(),
None => ide_vfs::load_text(path).ok().map(|t| t.text),
};
let workers = std::thread::available_parallelism().map_or(4, |n| n.get().min(8));
let chunk = paths.len().div_ceil(workers).max(1);
Ok(std::thread::scope(|scope| {
let parts: Vec<_> = paths
.chunks(chunk)
.map(|part| (part.len(), scope.spawn(move || part.iter().map(read).collect::<Vec<_>>())))
.collect();
parts.into_iter().flat_map(|(len, h)| h.join().unwrap_or_else(|_| vec![None; len])).collect()
}))
})
.await
}
/// The file `window` may hand to the OS's default application: `path` itself, resolved, when it is a
/// file inside the window's project (its folder or a content root). The window must be trusted and
/// open locally; a remote window's files live on its own machine and are not launched here.
@@ -550,6 +573,7 @@ ide_command::domain!(
fs_entry_kind,
fs_read_dir,
fs_read_text,
fs_read_texts,
fs_rename,
fs_scratches_dir,
fs_set_writable,
+1
Переглянути файл
@@ -361,6 +361,7 @@ fs_read_dir always project_host bin
fs_read_image always project_host bindings async
fs_read_range always project_host bindings async
fs_read_text always project_host bindings async
fs_read_texts always project_host bindings async
fs_rename always project_host bindings async
fs_resolve_reference always project_host bindings async
fs_scratches_dir always project_host bindings sync
+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