Skip to content

Commit 0ff2742

Browse files
only21milclaude
andcommitted
perf(server): sort the working-tree file list once per status read
The local status read sorted the numstat files, appended files that have no numstat (untracked paths), then sorted the whole list again. The final sort alone gives the same order, so drop the first one. The new test spies on Array sort calls during a status read with tracked text, binary and untracked files. It checks the returned files and totals, and that the file list is sorted in one pass. On main it sees two passes ([5, 3]). Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ec58572 commit 0ff2742

2 files changed

Lines changed: 57 additions & 7 deletions

File tree

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
44
import * as HostProcess from "@t3tools/shared/HostProcess";
55
import { writeFakeCli } from "@t3tools/provider-testing/fakeCli";
66
import { assert, it, describe } from "@effect/vitest";
7+
import { vi } from "vite-plus/test";
78
import * as Deferred from "effect/Deferred";
89
import * as Effect from "effect/Effect";
910
import * as FileSystem from "effect/FileSystem";
@@ -3359,6 +3360,57 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
33593360
}),
33603361
);
33613362

3363+
it.effect("sorts changed files once after adding files without numstat", () =>
3364+
Effect.gen(function* () {
3365+
const cwd = yield* makeTmpDir();
3366+
yield* initRepoWithCommit(cwd);
3367+
yield* writeTextFile(cwd, "d.txt", "one\n");
3368+
yield* writeTextFile(cwd, "c.bin", "binary\0one");
3369+
yield* writeTextFile(cwd, "b.txt", "one\n");
3370+
yield* git(cwd, ["add", "."]);
3371+
yield* git(cwd, ["commit", "-m", "add files"]);
3372+
yield* writeTextFile(cwd, "d.txt", "two\n");
3373+
yield* writeTextFile(cwd, "c.bin", "binary\0two");
3374+
yield* writeTextFile(cwd, "b.txt", "one\ntwo\n");
3375+
yield* writeTextFile(cwd, "e-new.txt", "new\n");
3376+
yield* writeTextFile(cwd, "a-new.txt", "new\n");
3377+
const driver = yield* GitVcsDriver.GitVcsDriver;
3378+
const sorts = [vi.spyOn(Array.prototype, "sort"), vi.spyOn(Array.prototype, "toSorted")];
3379+
const sortedLists: Array<ReadonlyArray<unknown>> = [];
3380+
3381+
const status = yield* driver.statusDetailsLocal(cwd).pipe(
3382+
Effect.ensuring(
3383+
Effect.sync(() => {
3384+
for (const sort of sorts) {
3385+
// Read the recorded receivers first: mockRestore clears them.
3386+
sortedLists.push(...sort.mock.contexts.filter((list) => Array.isArray(list)));
3387+
sort.mockRestore();
3388+
}
3389+
}),
3390+
),
3391+
);
3392+
3393+
assert.deepEqual(status.workingTree.files, [
3394+
{ path: "a-new.txt", insertions: 0, deletions: 0 },
3395+
{ path: "b.txt", insertions: 1, deletions: 0 },
3396+
{ path: "c.bin", insertions: 0, deletions: 0 },
3397+
{ path: "d.txt", insertions: 1, deletions: 1 },
3398+
{ path: "e-new.txt", insertions: 0, deletions: 0 },
3399+
]);
3400+
assert.equal(status.workingTree.insertions, 2);
3401+
assert.equal(status.workingTree.deletions, 1);
3402+
// Every array sorted while reading status that holds a returned file entry.
3403+
const statusFiles = new Set<unknown>(status.workingTree.files);
3404+
const fileListSorts = sortedLists.filter((list) =>
3405+
list.some((file) => statusFiles.has(file)),
3406+
);
3407+
assert.deepEqual(
3408+
fileListSorts.map((list) => list.length),
3409+
[status.workingTree.files.length],
3410+
);
3411+
}),
3412+
);
3413+
33623414
it.effect("commits selected files from a nested project directory", () =>
33633415
Effect.gen(function* () {
33643416
const path = yield* Path.Path;

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,13 +2221,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
22212221

22222222
let insertions = 0;
22232223
let deletions = 0;
2224-
const files = Array.from(fileStatMap.entries())
2225-
.map(([filePath, stat]) => {
2226-
insertions += stat.insertions;
2227-
deletions += stat.deletions;
2228-
return { path: filePath, insertions: stat.insertions, deletions: stat.deletions };
2229-
})
2230-
.toSorted((a, b) => a.path.localeCompare(b.path));
2224+
const files = Array.from(fileStatMap.entries()).map(([filePath, stat]) => {
2225+
insertions += stat.insertions;
2226+
deletions += stat.deletions;
2227+
return { path: filePath, insertions: stat.insertions, deletions: stat.deletions };
2228+
});
22312229

22322230
for (const filePath of changedFilesWithoutNumstat) {
22332231
if (fileStatMap.has(filePath)) continue;

0 commit comments

Comments
 (0)