From 3b539041f8f0a89e88df96ae76109e81e2b3d474 Mon Sep 17 00:00:00 2001 From: maoxin1234 <875408344@qq.com> Date: Sun, 13 Sep 2026 21:23:54 +0800 Subject: [PATCH] fix(vscode): cap and chunk file search initialization to prevent event loop starvation (#13249) --- extensions/vscode/src/util/FileSearch.ts | 26 ++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/extensions/vscode/src/util/FileSearch.ts b/extensions/vscode/src/util/FileSearch.ts index 10bf0977c0f..96f2096bfeb 100644 --- a/extensions/vscode/src/util/FileSearch.ts +++ b/extensions/vscode/src/util/FileSearch.ts @@ -7,6 +7,9 @@ import * as vscode from "vscode"; type FileMiniSearchResult = { relativePath: string; id: string }; +export const MAX_FILE_SEARCH_FILES = 50_000; +export const FILE_SEARCH_BATCH_SIZE = 1_000; + /* id = file URI */ @@ -35,12 +38,23 @@ export class FileSearch { const results = await walkDirs(this.ide, { source: "file search initialization", }); - this.miniSearch.addAll( - results.flat().map((uri) => ({ - id: uri, - relativePath: vscode.workspace.asRelativePath(uri), - })), - ); + const flatResults = results.flat(); + const cappedResults = + flatResults.length > MAX_FILE_SEARCH_FILES + ? flatResults.slice(0, MAX_FILE_SEARCH_FILES) + : flatResults; + + for (let i = 0; i < cappedResults.length; i += FILE_SEARCH_BATCH_SIZE) { + const batch = cappedResults + .slice(i, i + FILE_SEARCH_BATCH_SIZE) + .map((uri) => ({ + id: uri, + relativePath: vscode.workspace.asRelativePath(uri), + })); + this.miniSearch.addAll(batch); + // Yield to event loop to keep the extension host and renderer responsive + await new Promise((resolve) => setTimeout(resolve, 0)); + } } public search(query: string): FileMiniSearchResult[] {