Skip to content

Commit 099777b

Browse files
committed
perf(server): keep common transcript records on native parsing
1 parent 79634e8 commit 099777b

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

‎apps/server/src/usage/UsageService.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ describe("UsageService", () => {
619619
const { transcript, settings, home } = yield* setup;
620620
const large = claudeLine(1, 9900).replace(
621621
'"message":',
622-
'"padding":' + encodeUnknownJsonString("x".repeat(1024 * 1024)) + ',"message":',
622+
'"padding":' + encodeUnknownJsonString("x".repeat(9 * 1024 * 1024)) + ',"message":',
623623
);
624624
yield* Effect.promise(() => NodeFSP.writeFile(transcript, large));
625625
yield* Effect.gen(function* () {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ export interface TranscriptParseResult {
8080

8181
/** 64 bytes of JSONL tail is ample to distinguish a replaced file. */
8282
export const GUARD_LENGTH = 64;
83-
// Keep native JSON.parse for ordinary records; project large records before
84-
// decoding their usage. This is a switch in readers, never a record-size limit.
85-
const STREAMING_THRESHOLD_BYTES = 1024 * 1024;
83+
// Native parsing is faster for common 1–4 MiB context/tool records. Above
84+
// 8 MiB, project usage without allocating the whole record. This switches
85+
// readers; it never discards a record because of its size.
86+
const STREAMING_THRESHOLD_BYTES = 8 * 1024 * 1024;
8687
const NEWLINE = 0x0a;
8788
const CARRIAGE_RETURN = 0x0d;
8889

@@ -252,7 +253,9 @@ export async function readTranscriptRecords(
252253
filePath: string,
253254
provider: UsageProviderKind,
254255
resumeFrom?: TranscriptParsePosition,
256+
options?: { readonly streamingThresholdBytes?: number },
255257
): Promise<TranscriptParseResult | null> {
258+
const streamingThresholdBytes = options?.streamingThresholdBytes ?? STREAMING_THRESHOLD_BYTES;
256259
let handle: NodeFSP.FileHandle;
257260
try {
258261
handle = await NodeFSP.open(filePath, "r");
@@ -317,7 +320,7 @@ export async function readTranscriptRecords(
317320
const selectPath = selectUsageFields(provider);
318321

319322
const append = (segment: Buffer) => {
320-
if (!streaming && pendingBytes + segment.length <= STREAMING_THRESHOLD_BYTES) {
323+
if (!streaming && pendingBytes + segment.length <= streamingThresholdBytes) {
321324
if (segment.length > 0) pendingChunks.push(segment);
322325
pendingBytes += segment.length;
323326
return;
@@ -361,6 +364,7 @@ export async function readTranscriptRecords(
361364
const stream = handle.createReadStream({
362365
start,
363366
autoClose: false,
367+
highWaterMark: 256 * 1024,
364368
}) as AsyncIterable<Buffer>;
365369
for await (const chunk of stream) {
366370
let lineStart = 0;

‎apps/server/src/usage/usageTranscriptStreaming.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,18 @@ import * as NodePath from "node:path";
55

66
import { afterEach, beforeEach, describe, expect, it } from "@effect/vitest";
77

8-
import { readTranscriptRecords } from "./usageTranscriptReader.ts";
8+
import {
9+
readTranscriptRecords as readWithDefaultThreshold,
10+
type TranscriptParsePosition,
11+
} from "./usageTranscriptReader.ts";
12+
13+
// Exercise the same transition with compact fixtures. UsageService tests and
14+
// external 65/517 MiB fixtures also exercise the production threshold.
15+
const readTranscriptRecords = (
16+
path: string,
17+
provider: "claude" | "codex" | "grok",
18+
position?: TranscriptParsePosition,
19+
) => readWithDefaultThreshold(path, provider, position, { streamingThresholdBytes: 256 * 1024 });
920

1021
let dir: string;
1122
beforeEach(async () => {

0 commit comments

Comments
 (0)