Skip to content

Commit 6484eee

Browse files
SunkenInTimeclaude
andcommitted
perf(shared): cache the git launcher swap with the PATH scan
The swap's file checks ran before every git launch, about 0.28 ms of synchronous stat calls each on Windows, so a cached resolveSpawnCommand took ~288 us instead of ~35 us. It now runs on a cache miss and is stored with the scan, keyed on whether MSYSTEM is set. A Git upgrade that moves the real binary can fail git for up to the 30 second cache lifetime. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ea61922 commit 6484eee

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

‎packages/shared/src/shell.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,30 @@ effectIt.layer(NodeServices.layer)("resolveSpawnCommand", (it) => {
692692
}).pipe(Effect.provideService(CommandResolutionCache, new Map())),
693693
);
694694

695+
it.effect("keeps launcher swaps apart for environments with and without MSYSTEM", () =>
696+
Effect.gen(function* () {
697+
let scans = 0;
698+
const scan: SpawnExecutableResolver = () => {
699+
scans++;
700+
return "C:\\Git\\cmd\\git.exe";
701+
};
702+
const resolve = (env: NodeJS.ProcessEnv) =>
703+
resolveSpawnCommand("git", [], {
704+
env: { PATH: "C:\\Git\\cmd", PATHEXT: ".EXE", ...env },
705+
}).pipe(
706+
Effect.provideService(HostProcess.Platform, "win32"),
707+
Effect.provideService(SpawnExecutableResolution, scan),
708+
);
709+
710+
yield* resolve({});
711+
yield* resolve({});
712+
expect(scans).toBe(1);
713+
// A swap made without MSYSTEM must not reach a child that has it set.
714+
yield* resolve({ MSYSTEM: "MINGW64" });
715+
expect(scans).toBe(2);
716+
}).pipe(Effect.provideService(CommandResolutionCache, new Map())),
717+
);
718+
695719
it.effect("does not fall back to a shell for unresolved Windows commands", () =>
696720
Effect.gen(function* () {
697721
const command = yield* resolveSpawnCommand("missing & calc", ["unsafe & value"], {

‎packages/shared/src/shell.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@ export const resolveSpawnCommand = Effect.fnUntraced(function* (
765765
platform,
766766
resolvePathEnvironmentVariable(env),
767767
resolveWindowsPathExtensions(env).join(";"),
768+
env.MSYSTEM ? "msystem" : "",
768769
command,
769770
].join(COMMAND_RESOLUTION_CACHE_KEY_SEPARATOR);
770771
const nowNanos = yield* Clock.currentTimeNanos;
@@ -773,14 +774,16 @@ export const resolveSpawnCommand = Effect.fnUntraced(function* (
773774
if (cached !== undefined && cached.expiresAtNanos > nowNanos) {
774775
resolvedExecutable = cached.resolvedPath;
775776
} else {
776-
resolvedExecutable = resolveExecutable(command, platform, env) ?? null;
777+
// Cached with the scan: its file checks would otherwise run before every
778+
// git launch. A Git upgrade that moves the real binary can fail git for up
779+
// to the cache lifetime.
780+
const found = resolveExecutable(command, platform, env);
781+
resolvedExecutable = found === undefined ? null : preferGitForWindowsBinary(found, env);
777782
if (!explicitPath && resolvedExecutable !== null) {
778783
cacheCommandResolution(cache, cacheKey, resolvedExecutable, nowNanos);
779784
}
780785
}
781-
// Outside the cache: a Git upgrade moves the real binary, never the launcher.
782-
const resolvedCommand =
783-
resolvedExecutable === null ? command : preferGitForWindowsBinary(resolvedExecutable, env);
786+
const resolvedCommand = resolvedExecutable ?? command;
784787
const extension = NodePath.win32.extname(resolvedCommand).toLowerCase();
785788
if (extension !== ".cmd" && extension !== ".bat") {
786789
return { command: resolvedCommand, args: [...args], shell: false };

0 commit comments

Comments
 (0)