Skip to content

Commit 34b15a9

Browse files
perf(server): cache default branch name and origin existence across status refreshes (pingdotgg#5008)
1 parent fbecb73 commit 34b15a9

2 files changed

Lines changed: 123 additions & 13 deletions

File tree

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,51 @@ it.effect("uses stable diagnostics for every parsed non-repository command", ()
153153
}).pipe(Effect.provide(layer));
154154
});
155155

156+
it.effect("invalidates origin remote cache when a driver mutation adds origin", () =>
157+
Effect.gen(function* () {
158+
const driver = yield* GitVcsDriver.GitVcsDriver;
159+
const cwd = yield* makeTmpDir();
160+
const remote = yield* makeTmpDir("git-vcs-driver-remote-");
161+
yield* initRepoWithCommit(cwd);
162+
yield* git(remote, ["init", "--bare"]);
163+
164+
const before = yield* driver.statusDetailsLocal(cwd);
165+
assert.equal(before.hasOriginRemote, false);
166+
167+
yield* driver.ensureRemote({ cwd, preferredName: "origin", url: remote });
168+
169+
const after = yield* driver.statusDetailsLocal(cwd);
170+
assert.equal(after.hasOriginRemote, true);
171+
}).pipe(Effect.provide(TestLayer)),
172+
);
173+
174+
it.effect("re-reads origin remote status after cache TTL expiry and bypassed invalidation", () =>
175+
Effect.gen(function* () {
176+
const driver = yield* GitVcsDriver.GitVcsDriver;
177+
const cwd = yield* makeTmpDir();
178+
const remote = yield* makeTmpDir("git-vcs-driver-remote-");
179+
yield* initRepoWithCommit(cwd);
180+
yield* git(remote, ["init", "--bare"]);
181+
182+
// First call caches hasOriginRemote = false (5-min TTL)
183+
assert.equal((yield* driver.statusDetailsLocal(cwd)).hasOriginRemote, false);
184+
185+
// Add origin via raw git (bypasses invalidation hook)
186+
yield* git(cwd, ["remote", "add", "origin", remote]);
187+
188+
// Cache still has the stale false (TTL not yet expired)
189+
const stillCached = yield* driver.statusDetailsLocal(cwd);
190+
assert.equal(stillCached.hasOriginRemote, false);
191+
192+
// Advance past the 5-minute TTL so the cache entry expires
193+
yield* TestClock.adjust("6 minutes");
194+
195+
// After expiry, the next call re-executes and picks up the remote
196+
const afterExpiry = yield* driver.statusDetailsLocal(cwd);
197+
assert.equal(afterExpiry.hasOriginRemote, true);
198+
}).pipe(Effect.provide(TestLayer)),
199+
);
200+
156201
it.effect("coalesces concurrent ref pages into one repository snapshot", () =>
157202
Effect.scoped(
158203
Effect.gen(function* () {

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ const LIST_REFS_SNAPSHOT_CACHE_CAPACITY = 64;
6161
const LIST_REFS_SNAPSHOT_CACHE_TTL = Duration.minutes(2);
6262
const LIST_REFS_REFRESH_COALESCE_TTL = Duration.seconds(5);
6363
const LIST_REFS_REFRESH_FAILURE_COOLDOWN = Duration.seconds(30);
64+
const STATUS_DEFAULT_BRANCH_CACHE_TTL = Duration.minutes(5);
65+
const STATUS_ORIGIN_EXISTS_CACHE_TTL = Duration.minutes(5);
6466
const STATUS_UPSTREAM_REFRESH_ENV = Object.freeze({
6567
GCM_INTERACTIVE: "never",
6668
GIT_ASKPASS: "",
@@ -1119,6 +1121,63 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
11191121
return Cache.get(refresh ? repositoryPathsRefreshCache : repositoryPathsCache, cacheKey);
11201122
};
11211123

1124+
const defaultBranchCache = yield* Cache.makeWith(
1125+
(gitCommonDir: string) =>
1126+
Effect.gen(function* () {
1127+
const path = yield* Path.Path;
1128+
const fetchCwd =
1129+
path.basename(gitCommonDir) === ".git" ? path.dirname(gitCommonDir) : gitCommonDir;
1130+
return yield* executeGit(
1131+
"GitVcsDriver.statusDetails.defaultBranch",
1132+
fetchCwd,
1133+
["--git-dir", gitCommonDir, "symbolic-ref", "refs/remotes/origin/HEAD"],
1134+
{ allowNonZeroExit: true },
1135+
).pipe(
1136+
Effect.map((result) => {
1137+
if (result.exitCode !== 0) return null;
1138+
return parseDefaultBranchFromRemoteHeadRef(result.stdout, "origin");
1139+
}),
1140+
);
1141+
}),
1142+
{
1143+
capacity: 2_048,
1144+
timeToLive: Exit.match({
1145+
onSuccess: () => STATUS_DEFAULT_BRANCH_CACHE_TTL,
1146+
onFailure: () => Duration.zero,
1147+
}),
1148+
},
1149+
);
1150+
const originExistsCache = yield* Cache.makeWith(
1151+
(gitCommonDir: string) =>
1152+
Effect.gen(function* () {
1153+
const path = yield* Path.Path;
1154+
const fetchCwd =
1155+
path.basename(gitCommonDir) === ".git" ? path.dirname(gitCommonDir) : gitCommonDir;
1156+
return yield* executeGit(
1157+
"GitVcsDriver.statusDetails.originExists",
1158+
fetchCwd,
1159+
["--git-dir", gitCommonDir, "remote", "get-url", "origin"],
1160+
{ allowNonZeroExit: true },
1161+
).pipe(Effect.map((result) => result.exitCode === 0));
1162+
}),
1163+
{
1164+
capacity: 2_048,
1165+
timeToLive: Exit.match({
1166+
onSuccess: () => STATUS_ORIGIN_EXISTS_CACHE_TTL,
1167+
onFailure: () => Duration.zero,
1168+
}),
1169+
},
1170+
);
1171+
const invalidateStatusStaticCaches = (cwd: string) =>
1172+
Effect.gen(function* () {
1173+
const repositoryPaths = yield* resolveRepositoryPaths(cwd).pipe(
1174+
Effect.catchTags({ GitCommandError: () => Effect.succeed(null) }),
1175+
);
1176+
const cacheKey = repositoryPaths?.gitCommonDir ?? normalizeRepositoryPathsCacheKey(cwd);
1177+
yield* Cache.invalidate(defaultBranchCache, cacheKey);
1178+
yield* Cache.invalidate(originExistsCache, cacheKey);
1179+
});
1180+
11221181
const resolveGitCommonDir = Effect.fn("resolveGitCommonDir")(function* (cwd: string) {
11231182
const repositoryPaths = yield* resolveRepositoryPaths(cwd);
11241183
if (repositoryPaths !== null) {
@@ -1517,7 +1576,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
15171576
});
15181577
}
15191578

1520-
const [numstatStdout, defaultRefResult, hasPrimaryRemote] = yield* Effect.all(
1579+
const repositoryPaths = yield* resolveRepositoryPaths(cwd).pipe(
1580+
Effect.catchTags({ GitCommandError: () => Effect.succeed(null) }),
1581+
);
1582+
const statusCacheKey = repositoryPaths?.gitCommonDir;
1583+
const [numstatStdout, defaultBranch, hasPrimaryRemote] = yield* Effect.all(
15211584
[
15221585
executeGitWithStableDiagnostics(
15231586
"GitVcsDriver.statusDetails.numstat",
@@ -1574,21 +1637,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
15741637
);
15751638
}),
15761639
),
1577-
executeGit(
1578-
"GitVcsDriver.statusDetails.defaultRef",
1579-
cwd,
1580-
["symbolic-ref", "refs/remotes/origin/HEAD"],
1581-
{ allowNonZeroExit: true },
1582-
),
1583-
originRemoteExists(cwd).pipe(Effect.orElseSucceed(() => false)),
1640+
statusCacheKey
1641+
? Cache.get(defaultBranchCache, statusCacheKey).pipe(Effect.orElseSucceed(() => null))
1642+
: resolveDefaultBranchName(cwd, "origin").pipe(Effect.orElseSucceed(() => null)),
1643+
statusCacheKey
1644+
? Cache.get(originExistsCache, statusCacheKey).pipe(Effect.orElseSucceed(() => false))
1645+
: originRemoteExists(cwd).pipe(Effect.orElseSucceed(() => false)),
15841646
],
15851647
{ concurrency: "unbounded" },
15861648
);
15871649
const statusStdout = statusResult.stdout;
1588-
const defaultBranch =
1589-
defaultRefResult.exitCode === 0
1590-
? defaultRefResult.stdout.trim().replace(/^refs\/remotes\/origin\//, "")
1591-
: null;
15921650

15931651
let refName: string | null = null;
15941652
let upstreamRef: string | null = null;
@@ -2809,7 +2867,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
28092867
cwd: string,
28102868
effect: Effect.Effect<A, E>,
28112869
): Effect.Effect<A, E> =>
2812-
effect.pipe(Effect.ensuring(invalidateListRefsSnapshot(cwd).pipe(Effect.ignore)));
2870+
effect.pipe(
2871+
Effect.ensuring(
2872+
Effect.all([
2873+
invalidateListRefsSnapshot(cwd).pipe(Effect.ignore),
2874+
invalidateStatusStaticCaches(cwd).pipe(Effect.ignore),
2875+
]),
2876+
),
2877+
);
28132878
const initRepoWithListRefsInvalidation: GitVcsDriver.GitVcsDriver["Service"]["initRepo"] = (
28142879
input,
28152880
) =>

0 commit comments

Comments
 (0)