Skip to content

Commit 5d43989

Browse files
committed
perf(server): checkpoint capture scans the index listing by record instead of by byte
Each capture streams `git ls-files --full-name --sparse -v -z` (one `<tag> <path>\0` record per index entry, 2 MB for t3code's 28k files) through a callback that visits every byte on the server's main thread. Only each record's tag, and skip-worktree paths in a sparse checkout, are used, so the callback now jumps from one NUL to the next with indexOf. On t3code's real listing the callback takes 1.2 ms instead of 5.8 ms (17-33 ms on a cold first call), and a capture's main-thread busy time drops by about 5 ms. The chunk-boundary test also runs with 3-byte chunks, so records that continue across chunks and end mid-chunk are covered.
1 parent d8e9fd2 commit 5d43989

2 files changed

Lines changed: 35 additions & 18 deletions

File tree

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -707,9 +707,13 @@ it.effect("checkpoint capture keeps the legacy path when Git lacks add --sparse"
707707
}).pipe(Effect.scoped, Effect.provide(layerGitContract)),
708708
);
709709

710-
it.effect.each(["normal", "flags", "sparse"] as const)(
711-
"checkpoint index inspection handles entries beyond the output cap (index=%s)",
712-
(indexMode) =>
710+
it.effect.each(
711+
(["normal", "flags", "sparse"] as const).flatMap((indexMode) =>
712+
[1, 3].map((chunkSize) => ({ indexMode, chunkSize })),
713+
),
714+
)(
715+
"checkpoint index inspection handles entries beyond the output cap (index=$indexMode, chunk=$chunkSize)",
716+
({ indexMode, chunkSize }) =>
713717
Effect.gen(function* () {
714718
const fs = yield* FileSystem.FileSystem;
715719
const path = yield* Path.Path;
@@ -745,8 +749,8 @@ it.effect.each(["normal", "flags", "sparse"] as const)(
745749
...input,
746750
maxOutputBytes: 8,
747751
onStdoutChunk: (chunk) => {
748-
for (let i = 0; i < chunk.length; i++)
749-
input.onStdoutChunk?.(chunk.subarray(i, i + 1));
752+
for (let i = 0; i < chunk.length; i += chunkSize)
753+
input.onStdoutChunk?.(chunk.subarray(i, i + chunkSize));
750754
},
751755
}
752756
: input,

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

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
866866
let specialFlags = false;
867867
let recordStart = true;
868868
let skipped = false;
869-
let skippedRecord: number[] = [];
869+
let skippedRecord: Uint8Array[] = [];
870870
const skippedPaths: string[] = [];
871871
yield* vcsProcess.run({
872872
operation,
@@ -876,27 +876,40 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
876876
env: commitEnv,
877877
maxOutputBytes: 4_096,
878878
outputMode: "truncate",
879+
// The listing has a `<tag> <path>\0` record per index entry (2 MB for 28k files).
880+
// Only tags, and skip-worktree paths in a sparse checkout, are read, so jump
881+
// from one NUL to the next instead of visiting every byte.
879882
onStdoutChunk: (chunk) => {
880-
for (const byte of chunk) {
881-
if (recordStart) skipped = byte === 83;
883+
let start = 0;
884+
while (start < chunk.length) {
885+
const end = chunk.indexOf(0, start);
886+
if (recordStart) {
887+
const tag = chunk[start]!;
888+
skipped = tag === 83;
889+
if ((tag >= 97 && tag <= 122) || (!sparseCheckout && tag === 83)) {
890+
specialFlags = true;
891+
}
892+
}
882893
if (skipped && sparseCheckout) {
883-
if (byte !== 0) skippedRecord.push(byte);
884-
else {
885-
if (skippedRecord.at(-1) !== 47) {
886-
const name = Buffer.from(skippedRecord).subarray(2);
894+
skippedRecord.push(
895+
Buffer.from(chunk.subarray(start, end === -1 ? undefined : end)),
896+
);
897+
if (end !== -1) {
898+
const record = Buffer.concat(skippedRecord);
899+
if (record.at(-1) !== 47) {
900+
const name = record.subarray(2);
887901
if (!NodeBuffer.isUtf8(name)) specialFlags = true;
888902
else skippedPaths.push(name.toString("utf8"));
889903
}
890904
skippedRecord = [];
891905
}
892906
}
893-
if (
894-
recordStart &&
895-
((byte >= 97 && byte <= 122) || (!sparseCheckout && byte === 83))
896-
) {
897-
specialFlags = true;
907+
if (end === -1) {
908+
recordStart = false;
909+
break;
898910
}
899-
recordStart = byte === 0;
911+
recordStart = true;
912+
start = end + 1;
900913
}
901914
},
902915
});

0 commit comments

Comments
 (0)