perf(git): read both diff blobs concurrently (#10781)
* perf(git): read both diff blobs concurrently The diff loaders awaited their two sides in series, so the second `git show` could not start until the first had returned. The reads are independent, so that was pure added latency on every diff the review panel opens: ~47 ms sequential vs ~24 ms concurrent, a saving of ~23 ms per diff. Covers the merge-base, commit, and staged loaders, plus the unstaged path where the working-tree read is independent of the index->HEAD chain. The unstaged left chain itself stays sequential because its second step depends on the first. The staged coalescing test asserted the sequential shape (one spawn, then the next); it now pins the contract that actually matters — eight identical reads still collapse to two spawns, one per side. * test(perf): interleave the diff-blob benchmark arms Running one strategy's whole batch before the other's lets cache warming, CPU frequency drift, and background load correlate with the strategy being measured. Alternate the arms per iteration, alternate which goes first, and report medians so that drift stays common to both. Also reject malformed env settings rather than truncating them — Number.parseInt accepts "10foo" and 3.5. Interleaved result confirms the original: 1.90x-2.03x, ~24 ms saved per diff.
This commit is contained in:
@@ -543,11 +543,12 @@ describe('getDiff', () => {
|
||||
|
||||
const reads = Array.from({ length: 8 }, () => getDiff('/repo', 'src/file.ts', true))
|
||||
|
||||
await waitForMockCalls(gitExecFileAsyncBufferMock, 1)
|
||||
expect(gitExecFileAsyncBufferMock).toHaveBeenCalledTimes(1)
|
||||
// Why both up front: the two sides are independent spawns issued concurrently,
|
||||
// so 8 identical reads still collapse to exactly 2 — one per side, not per read.
|
||||
await waitForMockCalls(gitExecFileAsyncBufferMock, 2)
|
||||
expect(gitExecFileAsyncBufferMock).toHaveBeenCalledTimes(2)
|
||||
|
||||
leftBlob.resolve()
|
||||
await waitForMockCalls(gitExecFileAsyncBufferMock, 2)
|
||||
rightBlob.resolve()
|
||||
|
||||
const results = await Promise.all(reads)
|
||||
|
||||
+32
-21
@@ -1244,21 +1244,29 @@ async function loadDiff(
|
||||
let modifiedDeleted = false
|
||||
|
||||
try {
|
||||
const leftBlob = staged
|
||||
? await readGitBlobAtOidPath(worktreePath, 'HEAD', filePath, options)
|
||||
: compareAgainstHead
|
||||
? await readGitBlobAtOidPath(worktreePath, 'HEAD', filePath, options)
|
||||
: await readUnstagedLeftBlob(worktreePath, filePath, options)
|
||||
originalContent = leftBlob.content
|
||||
originalIsBinary = leftBlob.isBinary
|
||||
|
||||
if (staged) {
|
||||
const rightBlob = await readGitBlobAtIndexPath(worktreePath, filePath, options)
|
||||
// Why concurrent: HEAD and the index are independent `git show` spawns.
|
||||
// Only this branch qualifies — the unstaged left read chains index→HEAD.
|
||||
const [leftBlob, rightBlob] = await Promise.all([
|
||||
readGitBlobAtOidPath(worktreePath, 'HEAD', filePath, options),
|
||||
readGitBlobAtIndexPath(worktreePath, filePath, options)
|
||||
])
|
||||
originalContent = leftBlob.content
|
||||
originalIsBinary = leftBlob.isBinary
|
||||
modifiedContent = rightBlob.content
|
||||
modifiedIsBinary = rightBlob.isBinary
|
||||
modifiedDeleted = !rightBlob.exists
|
||||
} else {
|
||||
const workingTreeBlob = await readWorkingTreeFile(path.join(worktreePath, filePath))
|
||||
// The left chain (index→HEAD) is sequential within itself, but the working
|
||||
// tree read is a plain fs read that does not depend on it.
|
||||
const [leftBlob, workingTreeBlob] = await Promise.all([
|
||||
compareAgainstHead
|
||||
? readGitBlobAtOidPath(worktreePath, 'HEAD', filePath, options)
|
||||
: readUnstagedLeftBlob(worktreePath, filePath, options),
|
||||
readWorkingTreeFile(path.join(worktreePath, filePath))
|
||||
])
|
||||
originalContent = leftBlob.content
|
||||
originalIsBinary = leftBlob.isBinary
|
||||
modifiedContent = workingTreeBlob.content
|
||||
modifiedIsBinary = workingTreeBlob.isBinary
|
||||
modifiedDeleted = !workingTreeBlob.exists
|
||||
@@ -1397,8 +1405,12 @@ async function loadBranchDiff(
|
||||
): Promise<GitDiffResult> {
|
||||
try {
|
||||
const leftPath = args.oldPath ?? args.filePath
|
||||
const leftBlob = await readGitBlobAtOidPath(worktreePath, args.mergeBase, leftPath, options)
|
||||
const rightBlob = await readGitBlobAtOidPath(worktreePath, args.headOid, args.filePath, options)
|
||||
// Why concurrent: the two sides are independent `git show` spawns, so awaiting
|
||||
// them in series doubles the latency of every diff the review panel opens.
|
||||
const [leftBlob, rightBlob] = await Promise.all([
|
||||
readGitBlobAtOidPath(worktreePath, args.mergeBase, leftPath, options),
|
||||
readGitBlobAtOidPath(worktreePath, args.headOid, args.filePath, options)
|
||||
])
|
||||
|
||||
return buildDiffResult(
|
||||
leftBlob.content,
|
||||
@@ -1510,15 +1522,14 @@ async function loadCommitDiff(
|
||||
): Promise<GitDiffResult> {
|
||||
try {
|
||||
const leftPath = args.oldPath ?? args.filePath
|
||||
const leftBlob = args.parentOid
|
||||
? await readGitBlobAtOidPath(worktreePath, args.parentOid, leftPath, options)
|
||||
: { content: '', isBinary: false }
|
||||
const rightBlob = await readGitBlobAtOidPath(
|
||||
worktreePath,
|
||||
args.commitOid,
|
||||
args.filePath,
|
||||
options
|
||||
)
|
||||
// Why concurrent: the two sides are independent `git show` spawns. A root
|
||||
// commit has no parent to read, so that side resolves without a spawn.
|
||||
const [leftBlob, rightBlob] = await Promise.all([
|
||||
args.parentOid
|
||||
? readGitBlobAtOidPath(worktreePath, args.parentOid, leftPath, options)
|
||||
: Promise.resolve({ content: '', isBinary: false }),
|
||||
readGitBlobAtOidPath(worktreePath, args.commitOid, args.filePath, options)
|
||||
])
|
||||
|
||||
return buildDiffResult(
|
||||
leftBlob.content,
|
||||
|
||||
Reference in New Issue
Block a user