





















@@ -28,7 +28,9 @@ import {
2828} from "./status-dependencies.js";
29293030const installedManifestRegistryIndexFingerprintCache = new WeakMap<InstalledPluginIndex, string>();
31+const installedPackageJsonPathCache = new Map<string, string | null>();
3132const installedPackageMetadataCache = new Map<string, InstalledPackageMetadata>();
33+const MAX_INSTALLED_PACKAGE_JSON_PATH_CACHE_ENTRIES = 256;
3234const MAX_INSTALLED_PACKAGE_METADATA_CACHE_ENTRIES = 256;
33353436type InstalledPackageMetadata = {
@@ -38,6 +40,7 @@ type InstalledPackageMetadata = {
3840};
39414042export function clearInstalledManifestRegistryProcessCaches(): void {
43+installedPackageJsonPathCache.clear();
4144installedPackageMetadataCache.clear();
4245}
4346@@ -88,18 +91,25 @@ function resolvePackageJsonPath(
8891if (!record.packageJson?.path) {
8992return undefined;
9093}
94+const cacheKey = buildInstalledPackageJsonPathCacheKey(record);
95+if (cacheKey) {
96+const cached = installedPackageJsonPathCache.get(cacheKey);
97+if (cached !== undefined) {
98+return cached ?? undefined;
99+}
100+}
91101const rootDir = resolveInstalledPluginRootDir(record);
92102const realRootDir = safeRealpathSync(rootDir, realpathCache) ?? path.resolve(rootDir);
93103const packageJsonPath = path.resolve(realRootDir, record.packageJson.path);
94104const relative = path.relative(realRootDir, packageJsonPath);
95105if (!isRelativePathInsideOrEqual(relative)) {
96-return undefined;
106+return rememberInstalledPackageJsonPath(cacheKey, undefined);
97107}
98108const packageJsonRealPath = safeRealpathSync(packageJsonPath, realpathCache);
99109if (!packageJsonRealPath || !isPathInside(realRootDir, packageJsonRealPath)) {
100-return undefined;
110+return rememberInstalledPackageJsonPath(cacheKey, undefined);
101111}
102-return packageJsonPath;
112+return rememberInstalledPackageJsonPath(cacheKey, packageJsonPath);
103113}
104114105115function safeFileSignature(filePath: string | undefined): string | undefined {
@@ -139,6 +149,36 @@ function rememberInstalledPackageMetadata(
139149return metadata;
140150}
141151152+function rememberInstalledPackageJsonPath(
153+key: string | undefined,
154+packageJsonPath: string | undefined,
155+): string | undefined {
156+if (!key) {
157+return packageJsonPath;
158+}
159+installedPackageJsonPathCache.set(key, packageJsonPath ?? null);
160+while (installedPackageJsonPathCache.size > MAX_INSTALLED_PACKAGE_JSON_PATH_CACHE_ENTRIES) {
161+const oldest = installedPackageJsonPathCache.keys().next().value;
162+if (oldest === undefined) {
163+break;
164+}
165+installedPackageJsonPathCache.delete(oldest);
166+}
167+return packageJsonPath;
168+}
169+170+function buildInstalledPackageJsonPathCacheKey(
171+record: InstalledPluginIndexRecord,
172+): string | undefined {
173+if (!record.packageJson?.path || !record.packageJson.hash) {
174+return undefined;
175+}
176+return hashJson({
177+rootDir: path.resolve(resolveInstalledPluginRootDir(record)),
178+packageJson: record.packageJson,
179+});
180+}
181+142182function buildInstalledPackageMetadataCacheKey(params: {
143183packageJsonPath?: string;
144184record: InstalledPluginIndexRecord;
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。