From 567ad7046ca72bbf33a6c866d534b753cb94c551 Mon Sep 17 00:00:00 2001 From: elhoim Date: Wed, 30 Sep 2026 17:52:04 +0000 Subject: [PATCH] perf(git-review): key the PR cache through the cached runGit getCachedGitReviewData derived its cache key from `git symbolic-ref --short HEAD` (and `rev-parse --short HEAD` when detached) with a bare execFileSync, so every render spawned git once per git-pr / git-ci-status widget even when the PR data itself was a cache hit. The widgets now pass their render context and the ref lookup goes through git.ts's runGit, whose persistent cache (invalidated by HEAD/index mtime) is already shared with the git-branch and git-sha widgets. Without a context (the detached refresh child, fetchGitReviewData) the lookup is unchanged. Measured on a warm render with no refresh due (model, git-branch, git-pr, git-ci-status): 2 git spawns -> 0. bench2 CPU median 1363 -> 1319 ms (-3.3%, 40 passes) and 1443 -> 1375 ms (-4.7%, 30 passes), wall median -12% / -4%, p90 overlapping on this heavily loaded host. stdout is byte-identical. Co-Authored-By: Claude Opus 5.5 --- src/utils/__tests__/git-review-cache.test.ts | 43 ++++++++++++++++++++ src/utils/git-review-cache.ts | 41 +++++++++++++++++-- src/widgets/GitCiStatus.ts | 2 +- src/widgets/GitPr.ts | 5 ++- src/widgets/__tests__/GitCiStatus.test.ts | 5 ++- src/widgets/__tests__/GitPr.test.ts | 10 ++++- 6 files changed, 98 insertions(+), 8 deletions(-) diff --git a/src/utils/__tests__/git-review-cache.test.ts b/src/utils/__tests__/git-review-cache.test.ts index 7c8b2cb4a..81c804baf 100644 --- a/src/utils/__tests__/git-review-cache.test.ts +++ b/src/utils/__tests__/git-review-cache.test.ts @@ -898,4 +898,47 @@ describe('git-review-cache', () => { ); expect(glabMrCalls).toHaveLength(1); }); + + it('keys the cache through the render context runGit without spawning git for the ref', () => { + const harness = createHarness(); + const lockPathFromSpawn = prepareCachePath(harness) + '.lock'; + harness.execCalls.length = 0; + + const runGitCalls: string[] = []; + const deps: GitReviewCacheDeps = { + ...harness.deps, + runGit: (command) => { + runGitCalls.push(command); + return command === 'symbolic-ref --short HEAD' ? 'feature/cache-a' : null; + } + }; + + expect(getCachedGitReviewData('/tmp/repo', { context: { data: { cwd: '/tmp/repo' } } }, deps)).toBeNull(); + + expect(runGitCalls).toEqual(['symbolic-ref --short HEAD']); + expect(harness.execCalls.filter(call => call.cmd === 'git')).toEqual([]); + // Same cache file (and so the same lock the refresh child releases) + // as the uncached lookup. + expect(harness.cacheFiles.has(lockPathFromSpawn)).toBe(true); + }); + + it('falls back to rev-parse through runGit on a detached HEAD', () => { + const harness = createHarness(); + harness.setCurrentRef(''); + const lockPathFromSpawn = prepareCachePath(harness) + '.lock'; + + const runGitCalls: string[] = []; + const deps: GitReviewCacheDeps = { + ...harness.deps, + runGit: (command) => { + runGitCalls.push(command); + return command === 'rev-parse --short HEAD' ? 'abc123' : null; + } + }; + + getCachedGitReviewData('/tmp/repo', { context: {} }, deps); + + expect(runGitCalls).toEqual(['symbolic-ref --short HEAD', 'rev-parse --short HEAD']); + expect(harness.cacheFiles.has(lockPathFromSpawn)).toBe(true); + }); }); diff --git a/src/utils/git-review-cache.ts b/src/utils/git-review-cache.ts index a846b3c0f..3b8f9c806 100644 --- a/src/utils/git-review-cache.ts +++ b/src/utils/git-review-cache.ts @@ -16,6 +16,9 @@ import { createHash } from 'node:crypto'; import os from 'node:os'; import path from 'node:path'; +import type { RenderContext } from '../types/RenderContext'; + +import { runGit } from './git'; import { parseRemoteUrl } from './git-remote'; export type GitReviewProvider = 'gh' | 'glab'; @@ -41,6 +44,13 @@ export interface GitReviewData { export interface GitReviewFetchOptions { includeChecks?: boolean } +export interface GitReviewCachedOptions extends GitReviewFetchOptions { + // The widget's render context. When given, the branch/HEAD lookup that + // keys the cache goes through git.ts's cached runGit (shared with the + // git-branch and git-sha widgets) instead of spawning git every render. + context?: RenderContext; +} + interface StoredGitReviewCache { version: 1; data: GitReviewData | null; @@ -134,6 +144,7 @@ export interface GitReviewCacheDeps { writeFileSync: typeof writeFileSync; getHomedir: typeof os.homedir; now: typeof Date.now; + runGit?: typeof runGit; } const DEFAULT_GIT_REVIEW_CACHE_DEPS: GitReviewCacheDeps = { @@ -150,7 +161,8 @@ const DEFAULT_GIT_REVIEW_CACHE_DEPS: GitReviewCacheDeps = { unlinkSync, writeFileSync, getHomedir: os.homedir, - now: Date.now + now: Date.now, + runGit }; function getCacheDir(deps: GitReviewCacheDeps): string { @@ -194,6 +206,26 @@ function getCacheRef(cwd: string, deps: GitReviewCacheDeps): string { return 'unknown'; } +// Same ref as getCacheRef (which the refresh child still uses to derive the +// lock path it may release), read through the render's cached git commands. +function getCacheRefFromContext(context: RenderContext, cwd: string, deps: GitReviewCacheDeps): string { + if (!deps.runGit) { + return getCacheRef(cwd, deps); + } + + const branch = deps.runGit('symbolic-ref --short HEAD', context)?.trim() ?? ''; + if (branch.length > 0) { + return `branch:${branch}`; + } + + const head = deps.runGit('rev-parse --short HEAD', context)?.trim() ?? ''; + if (head.length > 0) { + return `head:${head}`; + } + + return 'unknown'; +} + function getCachePath(cwd: string, ref: string, deps: GitReviewCacheDeps): string { const hash = createHash('sha256') .update(cwd) @@ -718,11 +750,14 @@ function scheduleRefresh( export function getCachedGitReviewData( cwd: string, - options: GitReviewFetchOptions = {}, + options: GitReviewCachedOptions = {}, deps: GitReviewCacheDeps = DEFAULT_GIT_REVIEW_CACHE_DEPS ): GitReviewData | null { const includeChecks = options.includeChecks ?? false; - const cachePath = getCachePath(cwd, getCacheRef(cwd, deps), deps); + const ref = options.context + ? getCacheRefFromContext(options.context, cwd, deps) + : getCacheRef(cwd, deps); + const cachePath = getCachePath(cwd, ref, deps); const cached = readCache(cachePath, deps); const needsRefresh = cached === 'miss' || cached.stale diff --git a/src/widgets/GitCiStatus.ts b/src/widgets/GitCiStatus.ts index 89d8cd4f8..9818eed58 100644 --- a/src/widgets/GitCiStatus.ts +++ b/src/widgets/GitCiStatus.ts @@ -106,7 +106,7 @@ export class GitCiStatusWidget implements Widget { } const cwd = this.deps.resolveGitCwd(context) ?? this.deps.getProcessCwd(); - const checks = this.deps.getCachedGitReviewData(cwd, { includeChecks: true })?.checks; + const checks = this.deps.getCachedGitReviewData(cwd, { includeChecks: true, context })?.checks; if (!checks) { return isHidden(item, NO_DATA_HIDEABLE_STATE.key) ? null : NO_CHECKS; } diff --git a/src/widgets/GitPr.ts b/src/widgets/GitPr.ts index dda7dc00d..22653ca45 100644 --- a/src/widgets/GitPr.ts +++ b/src/widgets/GitPr.ts @@ -126,7 +126,10 @@ export class GitPrWidget implements Widget { } const cwd = this.deps.resolveGitCwd(context) ?? this.deps.getProcessCwd(); - const prData = this.deps.getCachedGitReviewData(cwd, { includeChecks: context.gitReviewNeedsChecks ?? false }); + const prData = this.deps.getCachedGitReviewData(cwd, { + includeChecks: context.gitReviewNeedsChecks ?? false, + context + }); if (!prData) { return isHidden(item, NO_DATA_HIDEABLE_STATE.key) ? null : `(no ${resolvePrNoun(null, context, this.deps)})`; } diff --git a/src/widgets/__tests__/GitCiStatus.test.ts b/src/widgets/__tests__/GitCiStatus.test.ts index de76b2204..53e2b21a6 100644 --- a/src/widgets/__tests__/GitCiStatus.test.ts +++ b/src/widgets/__tests__/GitCiStatus.test.ts @@ -136,6 +136,9 @@ describe('GitCiStatusWidget', () => { it('uses process cwd when repo path is omitted', () => { const getCachedGitReviewData = vi.fn(() => PASSING_PR); render({}, { getCachedGitReviewData, resolveGitCwd: () => undefined }); - expect(getCachedGitReviewData).toHaveBeenCalledWith('/tmp/process-cwd', { includeChecks: true }); + expect(getCachedGitReviewData).toHaveBeenCalledWith('/tmp/process-cwd', { + includeChecks: true, + context: expect.objectContaining({}) as unknown + }); }); }); diff --git a/src/widgets/__tests__/GitPr.test.ts b/src/widgets/__tests__/GitPr.test.ts index 3411a4b8c..8593c1721 100644 --- a/src/widgets/__tests__/GitPr.test.ts +++ b/src/widgets/__tests__/GitPr.test.ts @@ -148,7 +148,10 @@ describe('GitPrWidget', () => { expect(result).toBe( `${renderOsc8Link('https://github.com/owner/repo/pull/123', 'PR #123')} OPEN Fix authentication bug` ); - expect(getCachedGitReviewData).toHaveBeenCalledWith('/tmp/process-cwd', { includeChecks: false }); + expect(getCachedGitReviewData).toHaveBeenCalledWith('/tmp/process-cwd', { + includeChecks: false, + context: expect.objectContaining({}) as unknown + }); }); it('should request checks when a CI widget shares the render context', () => { @@ -156,7 +159,10 @@ describe('GitPrWidget', () => { render({ cwd: '/tmp/repo', needsChecks: true }, { getCachedGitReviewData }); - expect(getCachedGitReviewData).toHaveBeenCalledWith('/tmp/repo', { includeChecks: true }); + expect(getCachedGitReviewData).toHaveBeenCalledWith('/tmp/repo', { + includeChecks: true, + context: expect.objectContaining({}) as unknown + }); }); it('should truncate long titles', () => {