diff --git a/lib/core.js b/lib/core.js index 783321c..b2f7c52 100644 --- a/lib/core.js +++ b/lib/core.js @@ -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; diff --git a/lib/extract-asar.js b/lib/extract-asar.js index 4b83bc3..da395b4 100644 --- a/lib/extract-asar.js +++ b/lib/extract-asar.js @@ -21,8 +21,58 @@ export function diskFs() { return cachedDiskFs; } -function asarFileIn(dir) { - if (!dir) return false; +/** + * asar 判定缓存(启动性能修复 / F14 + F17)。 + * + * `asarShadowsPath()` 每次调用都要向上探最多 12 层,每层 1~2 次 original-fs 系统调用, + * 于是**每一次**经 hostFs 的调用都变成约 25 次系统调用。实测: + * - findAiBaseWeb 的兜底目录扫描 6 万个目录:裸 node:fs 6.2 s → 经这层 94 s; + * - 兜底扫描修掉之后,剩下的模块加载期里,光是这套探针自身仍占宿主启动 CPU 的 24% + * (4117 ms / 17226 ms)。 + * + * 缓存分两级: + * 1) 持久级:5 s 存活期。任何一次经 hostFs 的**写操作**立即清空;插件自己搬 app.asar 的 + * 那些函数(renameAsarAside / extractAsarToSide / commitExtractedDir / …)也显式清空。 + * 进程外的官方更新器换 app.asar 由 5 s 存活期兜底,官方更新后本来也会重启宿主。 + * 2) 作用域级:withFsProbeCache() 显式开合的强缓存。作用域内不改文件系统, + * 给目录递归扫描那种十万次调用的一次性突发用。 + */ +const PROBE_TTL_MS = 5000; +const PROBE_MAX_ENTRIES = 400000; + +let probePersist = null; +let probePersistEpoch = 0; +let probeScope = null; + +/** 丢弃全部 asar 判定缓存。任何可能改动 app.asar 的操作之后都必须调用。 */ +export function invalidateAsarProbes() { + probePersist = new Map(); + probePersistEpoch = Date.now(); +} + +function activeProbes() { + if (probeScope) return probeScope; + const now = Date.now(); + if (!probePersist || now - probePersistEpoch >= PROBE_TTL_MS) invalidateAsarProbes(); + return probePersist; +} + +/** 在给定的同步函数内启用强缓存(作用域内不得改动文件系统)。可重入。 */ +export function withFsProbeCache(fn) { + if (probeScope) return fn(); + probeScope = new Map(); + try { + return fn(); + } finally { + probeScope = null; + } +} + +function probeKey(kind, p) { + return kind + String(p).replace(/\\/g, "/").replace(/\/+$/, "").toLowerCase(); +} + +function probeAsarFileIn(dir) { try { const asar = path.join(dir, "app.asar"); return diskFs().existsSync(asar) && diskFs().statSync(asar).isFile(); @@ -31,10 +81,18 @@ function asarFileIn(dir) { } } -/** 这个路径会被 Electron 映射进旁边的 app.asar。已解开且归档已挪走时为假。 */ -export function asarShadowsPath(targetPath) { - const raw = String(targetPath || "").trim(); - if (!raw) return false; +function asarFileIn(dir) { + if (!dir) return false; + const cache = activeProbes(); + const k = probeKey("f:", dir); + const hit = cache.get(k); + if (hit !== undefined) return hit; + const v = probeAsarFileIn(dir); + if (cache.size < PROBE_MAX_ENTRIES) cache.set(k, v); + return v; +} + +function computeAsarShadows(raw) { let dir = path.normalize(raw); for (let i = 0; i < 12; i += 1) { const base = path.basename(dir).toLowerCase(); @@ -47,6 +105,19 @@ export function asarShadowsPath(targetPath) { return false; } +/** 这个路径会被 Electron 映射进旁边的 app.asar。已解开且归档已挪走时为假。 */ +export function asarShadowsPath(targetPath) { + const raw = String(targetPath || "").trim(); + if (!raw) return false; + const cache = activeProbes(); + const k = probeKey("s:", raw); + const hit = cache.get(k); + if (hit !== undefined) return hit; + const v = computeAsarShadows(raw); + if (cache.size < PROBE_MAX_ENTRIES) cache.set(k, v); + return v; +} + /** * 宿主文件用哪套 fs。 * 没有 app.asar(Web、第三方桌面、官方包已经解开并挪走归档)时继续用 node:fs。 @@ -60,8 +131,17 @@ function fsForPair(left, right) { return asarShadowsPath(left) || asarShadowsPath(right) ? diskFs() : nodeFs; } +// 这些方法会改动文件系统,可能新增/删除 app.asar,调用前必须让探针缓存失效。 +// openSync 故意不列入:它多数时候是只读打开,放进来会把缓存冲掉。 +const HOST_WRITE = new Set([ + "writeFileSync", "mkdirSync", "chmodSync", "unlinkSync", "rmSync", + "renameSync", "appendFileSync", "symlinkSync", "copyFileSync", +]); + function route(method, pair) { + const isWrite = HOST_WRITE.has(method); return (first, ...rest) => { + if (isWrite) invalidateAsarProbes(); const f = pair ? fsForPair(first, rest[0]) : fsFor(first); return f[method](first, ...rest); }; @@ -78,12 +158,12 @@ export const hostFs = { copyFileSync: route("copyFileSync", true), promises: { readFile: (p, ...args) => fsFor(p).promises.readFile(p, ...args), - writeFile: (p, ...args) => fsFor(p).promises.writeFile(p, ...args), - mkdir: (p, ...args) => fsFor(p).promises.mkdir(p, ...args), - unlink: (p, ...args) => fsFor(p).promises.unlink(p, ...args), - rm: (p, ...args) => fsFor(p).promises.rm(p, ...args), - copyFile: (src, dest, ...args) => fsForPair(src, dest).promises.copyFile(src, dest, ...args), - rename: (src, dest, ...args) => fsForPair(src, dest).promises.rename(src, dest, ...args), + writeFile: (p, ...args) => { invalidateAsarProbes(); return fsFor(p).promises.writeFile(p, ...args); }, + mkdir: (p, ...args) => { invalidateAsarProbes(); return fsFor(p).promises.mkdir(p, ...args); }, + unlink: (p, ...args) => { invalidateAsarProbes(); return fsFor(p).promises.unlink(p, ...args); }, + rm: (p, ...args) => { invalidateAsarProbes(); return fsFor(p).promises.rm(p, ...args); }, + copyFile: (src, dest, ...args) => { invalidateAsarProbes(); return fsForPair(src, dest).promises.copyFile(src, dest, ...args); }, + rename: (src, dest, ...args) => { invalidateAsarProbes(); return fsForPair(src, dest).promises.rename(src, dest, ...args); }, }, }; for (const method of HOST_SYNC) hostFs[method] = route(method, false); @@ -99,6 +179,8 @@ function renameBusy(error) { } export function renameAsarAside(asarPath) { + // 这一步直接改动 app.asar 的有无,必须让 asar 判定缓存立刻失效。 + invalidateAsarProbes(); const afs = archiveFs(); if (!asarPath || !afs.existsSync(asarPath)) return { renamed: false }; const bak = `${asarPath}.bak`;