Skip to content

Commit 84e1a29

Browse files
committed
perf(patches): cache blob git diff results
1 parent 8c86d3a commit 84e1a29

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

src/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ const remoteFileContentCache = new LRU<string, string>({
3535
sizeCalculation: (value) => value.length,
3636
});
3737

38+
const gitDiffCache = new LRU<string, string>({
39+
max: 500,
40+
maxSize: 10485760,
41+
sizeCalculation: (value) => value.length,
42+
});
43+
3844
const EMPTY_BLOB_SHA = "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391";
3945

4046
export const patchedFilenameRegex =
@@ -902,6 +908,12 @@ export async function gitDiffBlobs(
902908
blobIdA: string,
903909
blobIdB: string,
904910
) {
911+
const cacheKey = `${filename}:${blobIdA}..${blobIdB}`;
912+
913+
if (gitDiffCache.has(cacheKey)) {
914+
return gitDiffCache.get(cacheKey)!;
915+
}
916+
905917
if (!/^[0-9a-f]+$/.test(blobIdA)) {
906918
throw new Error(`Invalid blob ID: ${blobIdA}`);
907919
}
@@ -933,10 +945,12 @@ export async function gitDiffBlobs(
933945
`new file mode 100644\nindex ${blobIdA}..${blobIdB}`,
934946
);
935947

948+
gitDiffCache.set(cacheKey, fileDiff);
936949
return fileDiff;
937950
}
938951

939952
const stdout = await gitDiffBlobsInternal(cwd, filename, blobIdA, blobIdB);
953+
gitDiffCache.set(cacheKey, stdout);
940954

941955
return stdout;
942956
}

0 commit comments

Comments
 (0)