The best way to control Claude Code, Codex and more from any computer or phone
perf(server): reduce allocation while reading usage history (#36009)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: GPT 6.1 Sol <codex@openai.com> Co-authored-by: Grok 4.7 <noreply@x.ai>
Wout Stiens committed
Nov 13, 2026 at 06:51 UTC
0b0a61a01f2fd35319cfc55cb9a4145ec3c27593
1 file changed
+27
-10
apps/server/src/usage/usageTranscriptReader.ts
+27
-10
@@ -43,6 +43,7 @@ export interface TranscriptParseResult {
43
44
export const GUARD_LENGTH = 64;
45
const STREAMING_THRESHOLD_BYTES = 8 * 1024 * 1024;
46
+const READ_CHUNK_BYTES = 256 * 1024;
47
const NEWLINE = 0x0a;
48
const STAT_CONCURRENCY = 32;
49
const WALK_CONCURRENCY = 16;
@@ -168,6 +169,29 @@ async function guardMatches(
169
}
170
}
171
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
+
195
// Match markers in one latin1 alternation; one character per byte preserves exact ASCII offsets.
196
export function transcriptLineGate(markers: readonly [string, ...string[]]) {
197
if (markers.length === 1) {
@@ -244,7 +268,8 @@ export async function readTranscriptRecords<State>(
268
269
const append = (segment: Buffer) => {
270
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));
273
pendingBytes += segment.length;
274
return;
275
}
@@ -275,15 +300,7 @@ export async function readTranscriptRecords<State>(
300
streaming = undefined;
301
decoder = undefined;
302
};
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)) {
304
let lineStart = 0;
305
while (lineStart < chunk.length) {
306
const newlineIndex = chunk.indexOf(NEWLINE, lineStart);