From 277b1d02dfdf4a945000ffba5ea89dde8a5142d5 Mon Sep 17 00:00:00 2001 From: Sakuranda Date: Wed, 11 Mar 2026 20:49:31 +0800 Subject: [PATCH] perf(core): cache Windows PATH normalization snapshot --- .../src/services/shellExecutionService.ts | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/core/src/services/shellExecutionService.ts b/packages/core/src/services/shellExecutionService.ts index 3a3320ce7..c4270c564 100644 --- a/packages/core/src/services/shellExecutionService.ts +++ b/packages/core/src/services/shellExecutionService.ts @@ -22,6 +22,8 @@ const { Terminal } = pkg; const SIGKILL_TIMEOUT_MS = 200; const WINDOWS_PATH_DELIMITER = ';'; +let cachedWindowsPathFingerprint: string | undefined; +let cachedMergedWindowsPath: string | undefined; function mergeWindowsPathValues( env: NodeJS.ProcessEnv, @@ -50,6 +52,15 @@ function mergeWindowsPathValues( : undefined; } +function getWindowsPathFingerprint( + env: NodeJS.ProcessEnv, + pathKeys: string[], +): string { + return pathKeys + .map((key) => `${key}=${env[key] ?? ''}`) + .join('\0'); +} + function normalizePathEnvForWindows( env: NodeJS.ProcessEnv, ): NodeJS.ProcessEnv { @@ -76,7 +87,16 @@ function normalizePathEnvForWindows( return left.localeCompare(right); }); - const canonicalValue = mergeWindowsPathValues(normalized, orderedPathKeys); + const fingerprint = getWindowsPathFingerprint(normalized, orderedPathKeys); + const canonicalValue = + fingerprint === cachedWindowsPathFingerprint + ? cachedMergedWindowsPath + : mergeWindowsPathValues(normalized, orderedPathKeys); + + if (fingerprint !== cachedWindowsPathFingerprint) { + cachedWindowsPathFingerprint = fingerprint; + cachedMergedWindowsPath = canonicalValue; + } for (const key of pathKeys) { if (key !== 'PATH') {