perf(desktop): 桌面宿主启动不再全盘扫描 $HOME,asar 判定加缓存

启动路径上 restoreMissingOriginalsAllHosts() -> enumeratePatchAiBases()
-> findAiBaseWeb() 的最后一段兜底递归会遍历整个 $HOME:所有确定性探测
(DSH_BASE / 运行中进程 / DSH_HOME / npm prefix / shim)在桌面端都会被
isInsideDesktopInstall() 排除,于是必然落到这段递归,而实测结果恒为 null。
同时 hostFs 的每次调用都要过 asarShadowsPath(),后者向上探最多 12 层、
每层 1~2 次 original-fs 调用,单次 fs 调用被放大成约 25 次系统调用。

三处改动:
- findAiBaseWeb: desktop surface 跳过递归兜底(DSH_PURGE_DEEP_AIBASE_SCAN=1 可强制开启)
- findAiBaseRecursive: Windows 上只在 reparse point 上做跨设备 dev 判定
- asarShadowsPath: 加两级缓存(持久 TTL 5s + 作用域强缓存),写操作与
  renameAsarAside 均显式失效

效果:enumeratePatchAiBases() 96,959 ms -> 279 ms;插件 boot 阶段约 90 s -> 0.78 s。
回归:DSH_PURGE_DEEP_AIBASE_SCAN=1 强制开启时返回的宿主列表与关闭时一致。
This commit is contained in:
fomaper committed 2026-10-11 07:59:26 +08:00
1 parent beae190918
commit c9aabc8d95
2 files changed
+120 -22

No files matched your search

+26 -10
View File
@@ -24,6 +24,7 @@ import {
overlayAsarUnpacked,
renameAsarAside,
restoreExtractedDir,
withFsProbeCache,
} from "./extract-asar.js";
import { adapterFor, detectSurface } from "./surface.js";
import { hostPromptKeepsInject } from "./host-clean.js";
@@ -1772,18 +1773,30 @@ function findAiBaseWeb() {
}
const needle = path.join("node_modules", nested);
// F16:递归兜底扫描按设计只是「上面所有便宜探测都落空」后的最后手段(容器 / 非标准布局)。
// 桌面宿主上 resources/app 就是权威宿主,官方文档也要求 desktop 与 web 两个 profile 分开应用,
// 而实测这次扫描要 16 s、遍历 6 万个目录、结果为空 —— 纯启动负担。
// 需要桌面端也做深度扫描时设 DSH_PURGE_DEEP_AIBASE_SCAN=1。
if (detectSurface() === "desktop" && String(process.env.DSH_PURGE_DEEP_AIBASE_SCAN || "").trim() !== "1") {
return null;
}
// #91:三个兜底根(DSH_HOME / DSH_HOME/.. / $HOME)在容器/非标准布局下常撞到同一目录,
// 原来会把 $HOME 整个扫两遍。normalize+Set 去重后至少省掉重复的几万个 readdir。
const scannedRoots = new Set();
for (const sr of [findDshHome(), path.join(findDshHome(), ".."), os.homedir()]) {
if (!sr) continue;
const rootKey = path.normalize(sr);
if (scannedRoots.has(rootKey)) continue;
scannedRoots.add(rootKey);
const found = findAiBaseRecursive(rootKey, needle, 0);
if (found && !desktop.isInsideDesktopInstall(found)) return found;
}
return null;
// F14:兜底三根的目录递归是每次启动最大的一次 fs 突发(实测 94 s / 十万次调用)。
// 这一段内本进程不会改动文件系统,所以开启 asar 判定缓存,
// 把每次 fs 调用的 12 层向上探测摊掉。作用域结束缓存即失效。
return withFsProbeCache(() => {
for (const sr of [findDshHome(), path.join(findDshHome(), ".."), os.homedir()]) {
if (!sr) continue;
const rootKey = path.normalize(sr);
if (scannedRoots.has(rootKey)) continue;
scannedRoots.add(rootKey);
const found = findAiBaseRecursive(rootKey, needle, 0);
if (found && !desktop.isInsideDesktopInstall(found)) return found;
}
return null;
});
}
/** 一次 Apply 要打到的全部 @deepseek-ai 插件根(Web/CLI npm-global + 桌面 resources/app 等)。 */
@@ -2265,7 +2278,10 @@ function findAiBaseRecursive(root, needle, depth, visited, rootDev) {
if (!e.isDirectory()) continue;
const child = path.join(root, e.name);
// #91:跨挂载点不下钻 — 本盘枚举几千个本地目录没问题,踩进云盘就卡死
if (rootDev !== null) {
// F15:Windows 上跨设备只可能发生在 reparse point(符号链接 / junction / 卷挂载点)上,
// 普通子目录逐个 statSync 取 dev 是纯浪费 —— 6 万个目录约 7.2 s,占兜底扫描的一半开销。
// 非 Windows 的挂载点不是符号链接,原判定保留。
if (rootDev !== null && (process.platform !== "win32" || e.isSymbolicLink())) {
let childDev;
try { childDev = fs.statSync(child).dev; } catch { continue; }
if (childDev !== rootDev) continue;