Repository navigation
perf(server): resolve a branch's base once while its refs and config are unchanged #40125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1d15260
9f996af
b35c1f2
938fa6a
155fa3d
2659d77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -137,6 +137,7 @@ const LIST_REFS_REFRESH_COALESCE_TTL = Duration.seconds(5); | |
| const LIST_REFS_REFRESH_FAILURE_COOLDOWN = Duration.seconds(30); | ||
| const STATUS_DEFAULT_BRANCH_CACHE_TTL = Duration.minutes(5); | ||
| const STATUS_ORIGIN_EXISTS_CACHE_TTL = Duration.minutes(5); | ||
| const BASE_BRANCH_CACHE_TTL = Duration.minutes(5); | ||
| const STATUS_UPSTREAM_REFRESH_ENV = Object.freeze({ | ||
| GCM_INTERACTIVE: "never", | ||
| GIT_ASKPASS: "", | ||
|
|
@@ -188,6 +189,13 @@ function statusUpstreamRefreshFailureCooldown(consecutiveFailures: number): Dura | |
| return Duration.min(Duration.millis(cooldownMs), STATUS_UPSTREAM_REFRESH_FAILURE_MAX_COOLDOWN); | ||
| } | ||
|
|
||
| class BaseBranchCacheKey extends Data.Class<{ | ||
| cwd: string; | ||
| refName: string; | ||
| allowRemoteOfCurrent: boolean; | ||
| inputsStamp: string; | ||
| }> {} | ||
|
|
||
| class GitRefsSnapshotCacheKey extends Data.Class<{ | ||
| gitCommonDir: string; | ||
| epoch: number; | ||
|
|
@@ -2256,13 +2264,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* | |
| return { baseRef: comparisonRef.slice("refs/remotes/".length), comparisonRef }; | ||
| }); | ||
|
|
||
| const resolveBaseBranchForNoUpstream = Effect.fn("resolveBaseBranchForNoUpstream")(function* ( | ||
| const findBaseBranchForNoUpstream = Effect.fn("resolveBaseBranchForNoUpstream")(function* ( | ||
| cwd: string, | ||
| refName: string, | ||
| options?: { | ||
| readonly allowRemoteOfCurrent?: boolean; | ||
| readonly knownOriginDefaultBranch?: string | null; | ||
| }, | ||
| options?: { readonly allowRemoteOfCurrent?: boolean }, | ||
| trace?: { dependsOnRemoteOfCurrent: boolean; degraded: boolean }, | ||
| ) { | ||
| const configuredBaseRef = yield* readConfigValue(cwd, `branch.${refName}.t3-merge-base-ref`); | ||
| if ( | ||
|
|
@@ -2296,16 +2302,17 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* | |
| if (trackedBase !== null) return trackedBase; | ||
| } | ||
|
|
||
| const primaryRemoteName = yield* resolvePrimaryRemoteName(cwd).pipe( | ||
| Effect.orElseSucceed(() => null), | ||
| const primaryRemoteName = yield* findPrimaryRemoteName(cwd).pipe( | ||
| Effect.orElseSucceed(() => { | ||
| if (trace) trace.degraded = true; | ||
| return null; | ||
| }), | ||
| ); | ||
| const candidates = [ | ||
| Effect.succeed(configuredBaseBranch), | ||
| primaryRemoteName === null | ||
| ? Effect.succeed(null) | ||
| : primaryRemoteName === "origin" && options?.knownOriginDefaultBranch != null | ||
| ? Effect.succeed(options.knownOriginDefaultBranch) | ||
| : resolveDefaultBranchName(cwd, primaryRemoteName), | ||
| : resolveDefaultBranchName(cwd, primaryRemoteName), | ||
| ...DEFAULT_BASE_BRANCH_CANDIDATES.map(Effect.succeed), | ||
| ]; | ||
|
|
||
|
|
@@ -2326,9 +2333,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* | |
| continue; | ||
| } | ||
| if (normalizedCandidate === refName) { | ||
| // Without allowRemoteOfCurrent the answer it would have given here is unknown. | ||
| if (trace) trace.dependsOnRemoteOfCurrent = true; | ||
| if (options?.allowRemoteOfCurrent) { | ||
| const trackedRemoteBase = yield* resolveTrackedRemoteBaseRef(cwd, refName); | ||
| if (trackedRemoteBase !== null) return trackedRemoteBase; | ||
| if (trace) trace.dependsOnRemoteOfCurrent = false; | ||
| if ( | ||
| primaryRemoteName && | ||
| (yield* remoteBranchExists({ | ||
|
|
@@ -2337,6 +2347,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* | |
| refName: normalizedCandidate, | ||
| })) | ||
| ) { | ||
| if (trace) trace.dependsOnRemoteOfCurrent = true; | ||
| return { | ||
| baseRef: `${primaryRemoteName}/${normalizedCandidate}`, | ||
| comparisonRef: `refs/remotes/${primaryRemoteName}/${normalizedCandidate}`, | ||
|
|
@@ -2361,19 +2372,129 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* | |
| } | ||
|
|
||
| if (yield* branchExists(cwd, normalizedCandidate)) { | ||
| return { baseRef: normalizedCandidate, comparisonRef: `refs/heads/${normalizedCandidate}` }; | ||
| return { | ||
| baseRef: normalizedCandidate, | ||
| comparisonRef: `refs/heads/${normalizedCandidate}`, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| }); | ||
|
|
||
| // The effective config listing covers included, global, system and worktree config. Git writes | ||
| // packed-refs by rename and each loose ref by a rename in its directory, so those stats change | ||
| // whenever a ref could. Cost: one git process, plus a stat and listing per refs directory. | ||
| const readBaseBranchInputsStamp = Effect.fn("GitVcsDriverCore.readBaseBranchInputsStamp")( | ||
| function* (cwd: string, gitCommonDir: string) { | ||
| const effectiveConfig = yield* executeGit( | ||
| "GitVcsDriver.readBaseBranchInputsStamp", | ||
| cwd, | ||
| ["config", "--list", "-z"], | ||
| { allowNonZeroExit: true }, | ||
| ); | ||
| if (effectiveConfig.exitCode !== 0 || effectiveConfig.stdoutTruncated) return null; | ||
| const configHash = Hex.encode( | ||
| yield* digest.digest(Digest.sha256, new TextEncoder().encode(effectiveConfig.stdout)), | ||
| ); | ||
| const statToken = (target: string) => | ||
| fileSystem.stat(target).pipe( | ||
| Effect.map( | ||
| (info) => | ||
| `${Option.getOrElse(info.ino, () => "")}:${info.size}:${Option.match(info.mtime, { | ||
| onNone: () => "", | ||
| onSome: (mtime) => mtime.getTime(), | ||
| })}`, | ||
| ), | ||
| Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed("-")), | ||
| ); | ||
| const tokens = yield* Effect.forEach( | ||
| ["packed-refs", "reftable/tables.list"], | ||
| (name) => statToken(path.join(gitCommonDir, name)), | ||
| { concurrency: "unbounded" }, | ||
| ); | ||
| const directories = [ | ||
| path.join(gitCommonDir, "refs", "heads"), | ||
| path.join(gitCommonDir, "refs", "remotes"), | ||
| ]; | ||
| for (let index = 0; index < directories.length; index++) { | ||
| const directory = directories[index]!; | ||
| tokens.push(`${directory}=${yield* statToken(directory)}`); | ||
| const entries = yield* fileSystem | ||
| .readDirectoryEntries(directory) | ||
| .pipe(Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed([]))); | ||
| for (const entry of entries.toSorted((a, b) => a.name.localeCompare(b.name))) { | ||
| if (entry.type === "Directory") directories.push(path.join(directory, entry.name)); | ||
| } | ||
| } | ||
| tokens.push(configHash); | ||
| return tokens.join("\0"); | ||
| }, | ||
| ); | ||
| const baseBranchCache = yield* Cache.makeWith( | ||
| Effect.fnUntraced(function* (key: BaseBranchCacheKey) { | ||
| const trace = { dependsOnRemoteOfCurrent: false, degraded: false }; | ||
| const base = yield* findBaseBranchForNoUpstream( | ||
| key.cwd, | ||
| key.refName, | ||
| { allowRemoteOfCurrent: key.allowRemoteOfCurrent }, | ||
| trace, | ||
| ); | ||
| return { base, ...trace }; | ||
| }), | ||
| { | ||
| capacity: 2_048, | ||
| timeToLive: Exit.match({ | ||
| onSuccess: () => BASE_BRANCH_CACHE_TTL, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not cache a fallback caused by a failed lookup. If stamp acquisition succeeds but Previously, the next request retried resolution. Track degraded resolution and do not cache it. Keep intentional local-only results cacheable.
|
||
| onFailure: () => Duration.zero, | ||
| }), | ||
| }, | ||
| ); | ||
| const resolveBaseBranchForNoUpstream = Effect.fn("resolveBaseBranchForNoUpstream.cached")( | ||
| function* ( | ||
| cwd: string, | ||
| refName: string, | ||
| options?: { readonly allowRemoteOfCurrent?: boolean }, | ||
| ) { | ||
| const gitCommonDir = (yield* resolveRepositoryPaths(cwd).pipe( | ||
| Effect.orElseSucceed(() => null), | ||
| ))?.gitCommonDir; | ||
| const inputsStamp = | ||
| gitCommonDir === undefined | ||
| ? null | ||
| : yield* readBaseBranchInputsStamp(cwd, gitCommonDir).pipe( | ||
| Effect.orElseSucceed(() => null), | ||
| ); | ||
| if (inputsStamp === null) { | ||
| return yield* findBaseBranchForNoUpstream(cwd, refName, options); | ||
| } | ||
| const key = { | ||
| cwd: normalizeRepositoryPathsCacheKey(cwd), | ||
| refName, | ||
| inputsStamp, | ||
| }; | ||
| const allowRemoteOfCurrent = options?.allowRemoteOfCurrent === true; | ||
| const cacheKey = new BaseBranchCacheKey({ ...key, allowRemoteOfCurrent }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coalesce concurrent lookups across both option values. With Coalesce work by cwd, branch and stamp first. Resolve the second variant separately only when needed. Extend the regression test with this concurrent status path.
|
||
| const entry = yield* Cache.get(baseBranchCache, cacheKey); | ||
| // A fallback forced by a failed remote lookup must not outlive the failure. | ||
| if (entry.degraded) { | ||
| yield* Cache.invalidate(baseBranchCache, cacheKey); | ||
| } else if (!entry.dependsOnRemoteOfCurrent) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve the original expiration when sharing an entry. Every option-independent cache hit rewrites the opposite entry. Effect 4.1.3 Alternating status and review requests less than five minutes apart can therefore retain one resolution indefinitely. This also extends an incorrectly associated base beyond the stated five-minute limit. Preserve the original resolution time across both entries, and enforce expiration against that time.
|
||
| yield* Cache.set( | ||
| baseBranchCache, | ||
| new BaseBranchCacheKey({ ...key, allowRemoteOfCurrent: !allowRemoteOfCurrent }), | ||
| entry, | ||
| ); | ||
| } | ||
| return entry.base; | ||
| }, | ||
| ); | ||
|
|
||
| const computeAheadCountAgainstBase = Effect.fn("computeAheadCountAgainstBase")(function* ( | ||
| cwd: string, | ||
| refName: string, | ||
| options?: { readonly knownOriginDefaultBranch: string | null }, | ||
| ) { | ||
| const base = yield* resolveBaseBranchForNoUpstream(cwd, refName, options); | ||
| const base = yield* resolveBaseBranchForNoUpstream(cwd, refName); | ||
| if (!base) { | ||
| return 0; | ||
| } | ||
|
|
@@ -2474,9 +2595,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* | |
| branch && !isDefaultBranch | ||
| ? upstreamRef === null | ||
| ? aheadCount | ||
| : yield* computeAheadCountAgainstBase(cwd, branch, { | ||
| knownOriginDefaultBranch: defaultBranch, | ||
| }).pipe(Effect.orElseSucceed(() => 0)) | ||
| : yield* computeAheadCountAgainstBase(cwd, branch).pipe(Effect.orElseSucceed(() => 0)) | ||
| : 0; | ||
|
|
||
| return { | ||
|
|
@@ -2760,13 +2879,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* | |
| const [baseAheadCount, branchChanges] = yield* Effect.all( | ||
| [ | ||
| refName !== null && (needsFallbackAhead || needsAheadOfDefault) | ||
| ? computeAheadCountAgainstBase(cwd, refName, { | ||
| knownOriginDefaultBranch: defaultBranch, | ||
| }).pipe(Effect.orElseSucceed(() => 0)) | ||
| ? computeAheadCountAgainstBase(cwd, refName).pipe(Effect.orElseSucceed(() => 0)) | ||
| : Effect.succeed(0), | ||
| options?.includeBranchChanges | ||
| ? readBranchChangeTotals(repositoryPaths?.worktreeRoot ?? cwd, refName, { | ||
| knownOriginDefaultBranch: defaultBranch, | ||
| untrackedRoots, | ||
| headOid, | ||
| ...(index ? { indexPath: index } : {}), | ||
|
|
@@ -4004,7 +4120,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* | |
| cwd: string, | ||
| branch: string | null, | ||
| explicitBaseRef?: string, | ||
| knownOriginDefaultBranch: string | null = null, | ||
| ) { | ||
| const base = | ||
| explicitBaseRef !== undefined | ||
|
|
@@ -4018,7 +4133,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* | |
| : branch | ||
| ? yield* resolveBaseBranchForNoUpstream(cwd, branch, { | ||
| allowRemoteOfCurrent: true, | ||
| knownOriginDefaultBranch, | ||
| }).pipe(Effect.orElseSucceed(() => null)) | ||
| : null; | ||
| if (base === null) return { baseRef: null, mergeBase: "HEAD" }; | ||
|
|
@@ -4051,18 +4165,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* | |
| cwd: string, | ||
| branch: string | null, | ||
| options?: { | ||
| readonly knownOriginDefaultBranch: string | null; | ||
| readonly untrackedRoots: ReadonlyArray<string>; | ||
| readonly headOid: string | null; | ||
| readonly indexPath?: string; | ||
| }, | ||
| ) { | ||
| const resolved = yield* resolveReviewMergeBase( | ||
| cwd, | ||
| branch, | ||
| undefined, | ||
| options?.knownOriginDefaultBranch ?? null, | ||
| ); | ||
| const resolved = yield* resolveReviewMergeBase(cwd, branch); | ||
| // A rename after `git status` read HEAD moves the base config away, so the base was guessed. | ||
| const { baseRef, mergeBase } = | ||
| branch === null || (yield* branchExists(cwd, branch)) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validate the stamp after resolving the base.
A configuration change can occur between stamp acquisition and the finder’s config read. For example, the stamp records
refs/heads/main, but the finder reads a subsequent change torefs/heads/develop. This storesdevelopunder themainconfiguration stamp.If the user restores
main, the next lookup hits that entry and usesdevelopfor status and review. Previously, resolution read the restored configuration.Re-read the stamp after resolution. If it differs, do not retain or share the result under the earlier stamp.