mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-30 18:40:24 +08:00
Merge pull request #97 from fomaper/perf/desktop-startup-skip-home-scan
perf(desktop): 桌面宿主启动不再全盘扫描 $HOME,asar 判定加缓存(插件 boot 阶段 90s → 0.78s)
This commit is contained in:
commit
bebc915ac0
2 files changed
+120
-22
No files matched your search
+26
-10
@@ -24,6 +24,7 @@ import {
|
|||||||
overlayAsarUnpacked,
|
overlayAsarUnpacked,
|
||||||
renameAsarAside,
|
renameAsarAside,
|
||||||
restoreExtractedDir,
|
restoreExtractedDir,
|
||||||
|
withFsProbeCache,
|
||||||
} from "./extract-asar.js";
|
} from "./extract-asar.js";
|
||||||
import { adapterFor, detectSurface } from "./surface.js";
|
import { adapterFor, detectSurface } from "./surface.js";
|
||||||
import { hostPromptKeepsInject } from "./host-clean.js";
|
import { hostPromptKeepsInject } from "./host-clean.js";
|
||||||
@@ -1772,18 +1773,30 @@ function findAiBaseWeb() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const needle = path.join("node_modules", nested);
|
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)在容器/非标准布局下常撞到同一目录,
|
// #91:三个兜底根(DSH_HOME / DSH_HOME/.. / $HOME)在容器/非标准布局下常撞到同一目录,
|
||||||
// 原来会把 $HOME 整个扫两遍。normalize+Set 去重后至少省掉重复的几万个 readdir。
|
// 原来会把 $HOME 整个扫两遍。normalize+Set 去重后至少省掉重复的几万个 readdir。
|
||||||
const scannedRoots = new Set();
|
const scannedRoots = new Set();
|
||||||
for (const sr of [findDshHome(), path.join(findDshHome(), ".."), os.homedir()]) {
|
// F14:兜底三根的目录递归是每次启动最大的一次 fs 突发(实测 94 s / 十万次调用)。
|
||||||
if (!sr) continue;
|
// 这一段内本进程不会改动文件系统,所以开启 asar 判定缓存,
|
||||||
const rootKey = path.normalize(sr);
|
// 把每次 fs 调用的 12 层向上探测摊掉。作用域结束缓存即失效。
|
||||||
if (scannedRoots.has(rootKey)) continue;
|
return withFsProbeCache(() => {
|
||||||
scannedRoots.add(rootKey);
|
for (const sr of [findDshHome(), path.join(findDshHome(), ".."), os.homedir()]) {
|
||||||
const found = findAiBaseRecursive(rootKey, needle, 0);
|
if (!sr) continue;
|
||||||
if (found && !desktop.isInsideDesktopInstall(found)) return found;
|
const rootKey = path.normalize(sr);
|
||||||
}
|
if (scannedRoots.has(rootKey)) continue;
|
||||||
return null;
|
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 等)。 */
|
/** 一次 Apply 要打到的全部 @deepseek-ai 插件根(Web/CLI npm-global + 桌面 resources/app 等)。 */
|
||||||
@@ -2265,7 +2278,10 @@ function findAiBaseRecursive(root, needle, depth, visited, rootDev) {
|
|||||||
if (!e.isDirectory()) continue;
|
if (!e.isDirectory()) continue;
|
||||||
const child = path.join(root, e.name);
|
const child = path.join(root, e.name);
|
||||||
// #91:跨挂载点不下钻 — 本盘枚举几千个本地目录没问题,踩进云盘就卡死
|
// #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;
|
let childDev;
|
||||||
try { childDev = fs.statSync(child).dev; } catch { continue; }
|
try { childDev = fs.statSync(child).dev; } catch { continue; }
|
||||||
if (childDev !== rootDev) continue;
|
if (childDev !== rootDev) continue;
|
||||||
|
|||||||
+94
-12
@@ -21,8 +21,58 @@ export function diskFs() {
|
|||||||
return cachedDiskFs;
|
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 {
|
try {
|
||||||
const asar = path.join(dir, "app.asar");
|
const asar = path.join(dir, "app.asar");
|
||||||
return diskFs().existsSync(asar) && diskFs().statSync(asar).isFile();
|
return diskFs().existsSync(asar) && diskFs().statSync(asar).isFile();
|
||||||
@@ -31,10 +81,18 @@ function asarFileIn(dir) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 这个路径会被 Electron 映射进旁边的 app.asar。已解开且归档已挪走时为假。 */
|
function asarFileIn(dir) {
|
||||||
export function asarShadowsPath(targetPath) {
|
if (!dir) return false;
|
||||||
const raw = String(targetPath || "").trim();
|
const cache = activeProbes();
|
||||||
if (!raw) return false;
|
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);
|
let dir = path.normalize(raw);
|
||||||
for (let i = 0; i < 12; i += 1) {
|
for (let i = 0; i < 12; i += 1) {
|
||||||
const base = path.basename(dir).toLowerCase();
|
const base = path.basename(dir).toLowerCase();
|
||||||
@@ -47,6 +105,19 @@ export function asarShadowsPath(targetPath) {
|
|||||||
return false;
|
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。
|
* 宿主文件用哪套 fs。
|
||||||
* 没有 app.asar(Web、第三方桌面、官方包已经解开并挪走归档)时继续用 node:fs。
|
* 没有 app.asar(Web、第三方桌面、官方包已经解开并挪走归档)时继续用 node:fs。
|
||||||
@@ -60,8 +131,17 @@ function fsForPair(left, right) {
|
|||||||
return asarShadowsPath(left) || asarShadowsPath(right) ? diskFs() : nodeFs;
|
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) {
|
function route(method, pair) {
|
||||||
|
const isWrite = HOST_WRITE.has(method);
|
||||||
return (first, ...rest) => {
|
return (first, ...rest) => {
|
||||||
|
if (isWrite) invalidateAsarProbes();
|
||||||
const f = pair ? fsForPair(first, rest[0]) : fsFor(first);
|
const f = pair ? fsForPair(first, rest[0]) : fsFor(first);
|
||||||
return f[method](first, ...rest);
|
return f[method](first, ...rest);
|
||||||
};
|
};
|
||||||
@@ -78,12 +158,12 @@ export const hostFs = {
|
|||||||
copyFileSync: route("copyFileSync", true),
|
copyFileSync: route("copyFileSync", true),
|
||||||
promises: {
|
promises: {
|
||||||
readFile: (p, ...args) => fsFor(p).promises.readFile(p, ...args),
|
readFile: (p, ...args) => fsFor(p).promises.readFile(p, ...args),
|
||||||
writeFile: (p, ...args) => fsFor(p).promises.writeFile(p, ...args),
|
writeFile: (p, ...args) => { invalidateAsarProbes(); return fsFor(p).promises.writeFile(p, ...args); },
|
||||||
mkdir: (p, ...args) => fsFor(p).promises.mkdir(p, ...args),
|
mkdir: (p, ...args) => { invalidateAsarProbes(); return fsFor(p).promises.mkdir(p, ...args); },
|
||||||
unlink: (p, ...args) => fsFor(p).promises.unlink(p, ...args),
|
unlink: (p, ...args) => { invalidateAsarProbes(); return fsFor(p).promises.unlink(p, ...args); },
|
||||||
rm: (p, ...args) => fsFor(p).promises.rm(p, ...args),
|
rm: (p, ...args) => { invalidateAsarProbes(); return fsFor(p).promises.rm(p, ...args); },
|
||||||
copyFile: (src, dest, ...args) => fsForPair(src, dest).promises.copyFile(src, dest, ...args),
|
copyFile: (src, dest, ...args) => { invalidateAsarProbes(); return fsForPair(src, dest).promises.copyFile(src, dest, ...args); },
|
||||||
rename: (src, dest, ...args) => fsForPair(src, dest).promises.rename(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);
|
for (const method of HOST_SYNC) hostFs[method] = route(method, false);
|
||||||
@@ -99,6 +179,8 @@ function renameBusy(error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function renameAsarAside(asarPath) {
|
export function renameAsarAside(asarPath) {
|
||||||
|
// 这一步直接改动 app.asar 的有无,必须让 asar 判定缓存立刻失效。
|
||||||
|
invalidateAsarProbes();
|
||||||
const afs = archiveFs();
|
const afs = archiveFs();
|
||||||
if (!asarPath || !afs.existsSync(asarPath)) return { renamed: false };
|
if (!asarPath || !afs.existsSync(asarPath)) return { renamed: false };
|
||||||
const bak = `${asarPath}.bak`;
|
const bak = `${asarPath}.bak`;
|
||||||
|
|||||||
Reference in new issue
Block a user