Skip to content

Commit 32984e9

Browse files
StiensWoutclaudecodexGrok 4.7
committed
perf(server): usage history scans reuse one read buffer per transcript
A cold usage scan is CPU-bound on the server's main thread, and reading each transcript through createReadStream allocated a fresh 256 KB buffer and ran the Readable async iterator for every chunk. Plain transcripts now read with handle.read into one buffer per file; the few bytes of a line that continue past a chunk are copied before the next read. Compressed rollouts keep the zstd stream. Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: GPT 6.1 Sol <[email protected]> Co-authored-by: Grok 4.7 <[email protected]>
1 parent 43ce395 commit 32984e9

1 file changed

Lines changed: 27 additions & 10 deletions

File tree

‎apps/server/src/usage/usageTranscriptReader.ts‎

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export interface TranscriptParseResult {
4343

4444
export const GUARD_LENGTH = 64;
4545
const STREAMING_THRESHOLD_BYTES = 8 * 1024 * 1024;
46+
const READ_CHUNK_BYTES = 256 * 1024;
4647
const NEWLINE = 0x0a;
4748
const STAT_CONCURRENCY = 32;
4849
const WALK_CONCURRENCY = 16;
@@ -168,6 +169,29 @@ async function guardMatches(
168169
}
169170
}
170171

172+
/** Plain files reuse one buffer, so a chunk is only valid until the next one is requested. */
173+
async function* readChunks(
174+
handle: NodeFSP.FileHandle,
175+
start: number,
176+
compressed: boolean,
177+
): AsyncGenerator<Buffer> {
178+
if (compressed) {
179+
// Compressed snapshots cache a full parse; decoded byte offsets cannot
180+
// resume the compressed input.
181+
yield* handle
182+
.createReadStream({ start, autoClose: true, highWaterMark: READ_CHUNK_BYTES })
183+
.compose(NodeZlib.createZstdDecompress());
184+
return;
185+
}
186+
const buffer = Buffer.allocUnsafe(READ_CHUNK_BYTES);
187+
for (let position = start; ;) {
188+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, position);
189+
if (bytesRead === 0) return;
190+
position += bytesRead;
191+
yield buffer.subarray(0, bytesRead);
192+
}
193+
}
194+
171195
// Match markers in one latin1 alternation; one character per byte preserves exact ASCII offsets.
172196
export function transcriptLineGate(markers: readonly [string, ...string[]]) {
173197
if (markers.length === 1) {
@@ -244,7 +268,8 @@ export async function readTranscriptRecords<State>(
244268

245269
const append = (segment: Buffer) => {
246270
if (!streaming && pendingBytes + segment.length <= streamingThresholdBytes) {
247-
if (segment.length > 0) pendingChunks.push(segment);
271+
// Copy: the next read overwrites the chunk this segment points into.
272+
if (segment.length > 0) pendingChunks.push(Buffer.from(segment));
248273
pendingBytes += segment.length;
249274
return;
250275
}
@@ -275,15 +300,7 @@ export async function readTranscriptRecords<State>(
275300
streaming = undefined;
276301
decoder = undefined;
277302
};
278-
const input = handle.createReadStream({
279-
start,
280-
autoClose: compressed,
281-
highWaterMark: 256 * 1024,
282-
});
283-
// Compressed snapshots cache a full parse; decoded byte offsets cannot
284-
// resume the compressed input.
285-
const stream = compressed ? input.compose(NodeZlib.createZstdDecompress()) : input;
286-
for await (const chunk of stream) {
303+
for await (const chunk of readChunks(handle, start, compressed)) {
287304
let lineStart = 0;
288305
while (lineStart < chunk.length) {
289306
const newlineIndex = chunk.indexOf(NEWLINE, lineStart);

0 commit comments

Comments
 (0)