For performance-critical code, avoid unbounded or duplicated expensive work. Prefer (1) reuse of already-computed snapshots/results, (2) batching instead of per-item subprocess calls, and (3) strict size/time caps with streaming and early termination.
Apply: 1) Reuse computed snapshots/results
2) Batch expensive subprocess operations
git show/similar patterns with batched or bulk operations (e.g., ls-tree + git cat-file --batch) to avoid spawning a git process per file.3) Enforce caps with streaming/early stop
Example (batching + single pass concept):
async function readBlobsBatch(cwd: string, oids: Iterable<string>): Promise<Map<string, string>> {
const contents = new Map<string, string>();
const input = [...new Set(oids)].join("\n");
if (!input) return contents;
// One git process for many objects.
const pending = execFile("git", ["-C", cwd, "cat-file", "--batch"], {
windowsHide: true,
maxBuffer: Number.POSITIVE_INFINITY,
encoding: "buffer",
});
pending.child.stdin?.end(`${input}\n`);
const { stdout } = await pending;
// Parse stdout into contents (implementation omitted)
return contents;
}
Example (reuse computed snapshot; avoid duplicate capture):
const snapshot = await getLatestTerminalOutput();
telemetryService.captureTerminalOutputFailure(reason, { snapshot });
await returnCurrentTerminalContentsFromSnapshot(snapshot);
Checklist for code review: