Skip to content

Commit 6b2e2c9

Browse files
committed
perf(ccusage): restore agent JSONL hot paths
Keep the shared JSONL marker helper while restoring the agent-specific hot paths that were lost during the scanner consolidation. Claude now uses the shared helper single-marker byte scanner with latin1 decoding and byte marker indexes, matching the previous optimized loader behavior. Codex uses line scanning for marker-dense logs instead of the generic multi-marker candidate map. Adapter date keys now keep the default timezone as the local system timezone without constructing Intl formatters on the hot path. Explicit --timezone values still use Intl when needed, while UTC uses direct UTC formatting. Validation: pnpm run format; pnpm typecheck; pnpm run test; pnpm --filter ccusage build. Local parity against main through 2026-05-14 matched for claude/codex JSON in both TZ=UTC and default local timezone.
1 parent 2e86d0c commit 6b2e2c9

4 files changed

Lines changed: 252 additions & 48 deletions

File tree

‎apps/ccusage/src/adapter/claude/data-loader.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,9 +1192,17 @@ async function processJSONLUsageFileByLine(
11921192
filePath: string,
11931193
processLine: (line: string, usageMarkerIndex: number) => void,
11941194
): Promise<void> {
1195-
await processJSONLFileByMarkers(filePath, [USAGE_LINE_MARKER], (line, usageMarkerIndex) => {
1196-
processLine(line, usageMarkerIndex);
1197-
});
1195+
await processJSONLFileByMarkers(
1196+
filePath,
1197+
[USAGE_LINE_MARKER],
1198+
(line, usageMarkerIndex) => {
1199+
processLine(line, usageMarkerIndex);
1200+
},
1201+
{
1202+
bufferedEncoding: 'latin1',
1203+
markerIndex: 'byte',
1204+
},
1205+
);
11981206
}
11991207

12001208
/**

‎apps/ccusage/src/adapter/codex/parser.ts‎

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -229,52 +229,60 @@ async function parseCodexSessionFile(
229229
};
230230

231231
try {
232-
await processJSONLFileByMarkers(file, CODEX_JSONL_MARKERS, (line) => {
233-
const contextModel = extractTurnContextModelFast(line);
234-
if (contextModel != null) {
235-
currentModel = contextModel;
236-
currentModelIsFallback = false;
237-
return;
238-
}
239-
240-
const parsedFast = parseTokenCountLineFast(line);
241-
if (parsedFast != null) {
242-
addTokenCountEvent(parsedFast);
243-
return;
244-
}
245-
246-
try {
247-
const entry = asRecord(JSON.parse(line) as unknown);
248-
if (entry == null) {
249-
return;
250-
}
251-
const entryType = typeof entry.type === 'string' ? entry.type : undefined;
252-
const payload = entry.payload;
253-
const timestamp = typeof entry.timestamp === 'string' ? entry.timestamp : undefined;
254-
if (entryType === 'turn_context') {
255-
const model = extractModel(payload);
256-
if (model != null) {
257-
currentModel = model;
258-
currentModelIsFallback = false;
259-
}
260-
return;
261-
}
262-
if (entryType !== 'event_msg' || timestamp == null) {
232+
await processJSONLFileByMarkers(
233+
file,
234+
CODEX_JSONL_MARKERS,
235+
(line) => {
236+
const contextModel = extractTurnContextModelFast(line);
237+
if (contextModel != null) {
238+
currentModel = contextModel;
239+
currentModelIsFallback = false;
263240
return;
264241
}
265-
const payloadRecord = asRecord(payload);
266-
if (payloadRecord?.type !== 'token_count') {
242+
243+
const parsedFast = parseTokenCountLineFast(line);
244+
if (parsedFast != null) {
245+
addTokenCountEvent(parsedFast);
267246
return;
268247
}
269-
const info = asRecord(payloadRecord.info);
270-
addTokenCountEvent({
271-
timestamp,
272-
lastUsage: normalizeRawUsage(info?.last_token_usage),
273-
totalUsage: normalizeRawUsage(info?.total_token_usage),
274-
model: extractModel({ info, ...payloadRecord }),
275-
});
276-
} catch {}
277-
});
248+
249+
try {
250+
const entry = asRecord(JSON.parse(line) as unknown);
251+
if (entry == null) {
252+
return;
253+
}
254+
const entryType = typeof entry.type === 'string' ? entry.type : undefined;
255+
const payload = entry.payload;
256+
const timestamp = typeof entry.timestamp === 'string' ? entry.timestamp : undefined;
257+
if (entryType === 'turn_context') {
258+
const model = extractModel(payload);
259+
if (model != null) {
260+
currentModel = model;
261+
currentModelIsFallback = false;
262+
}
263+
return;
264+
}
265+
if (entryType !== 'event_msg' || timestamp == null) {
266+
return;
267+
}
268+
const payloadRecord = asRecord(payload);
269+
if (payloadRecord?.type !== 'token_count') {
270+
return;
271+
}
272+
const info = asRecord(payloadRecord.info);
273+
addTokenCountEvent({
274+
timestamp,
275+
lastUsage: normalizeRawUsage(info?.last_token_usage),
276+
totalUsage: normalizeRawUsage(info?.total_token_usage),
277+
model: extractModel({ info, ...payloadRecord }),
278+
});
279+
} catch {}
280+
},
281+
{
282+
bufferedEncoding: 'latin1',
283+
scanMode: 'line',
284+
},
285+
);
278286
} catch (error) {
279287
logger.debug('Failed to read Codex session file', error);
280288
}

‎apps/ccusage/src/adapter/shared.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@ const isoUtcTimestampPattern = /^\d{4}-\d{2}-\d{2}T.*Z$/u;
2020
const dateKeyPattern = /^\d{4}-\d{2}-\d{2}$/u;
2121
const monthKeyPattern = /^\d{4}-\d{2}$/u;
2222

23+
function formatDateParts(year: number, month: number, day: number): string {
24+
return `${year.toString().padStart(4, '0')}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
25+
}
26+
27+
function formatLocalDateKey(date: Date): string {
28+
return formatDateParts(date.getFullYear(), date.getMonth() + 1, date.getDate());
29+
}
30+
31+
function formatUTCDateKey(date: Date): string {
32+
return formatDateParts(date.getUTCFullYear(), date.getUTCMonth() + 1, date.getUTCDate());
33+
}
34+
35+
function formatLocalMonthKey(date: Date): string {
36+
return `${date.getFullYear().toString().padStart(4, '0')}-${(date.getMonth() + 1).toString().padStart(2, '0')}`;
37+
}
38+
39+
function formatUTCMonthKey(date: Date): string {
40+
return `${date.getUTCFullYear().toString().padStart(4, '0')}-${(date.getUTCMonth() + 1).toString().padStart(2, '0')}`;
41+
}
42+
2343
export function normalizeDateFilter(value: string | undefined): string | undefined {
2444
if (value == null || value === '') {
2545
return undefined;
@@ -137,11 +157,25 @@ function getDateTimeFormatter(timezone: string): Intl.DateTimeFormat {
137157
}
138158

139159
export function formatDateKey(timestamp: string, timezone?: string): string {
160+
if (timezone == null || timezone.trim() === '') {
161+
const cacheKey = `local:${timestamp.slice(0, 13)}`;
162+
const cached = dateKeyCache.get(cacheKey);
163+
if (cached != null) {
164+
return cached;
165+
}
166+
const formatted = formatLocalDateKey(new Date(timestamp));
167+
dateKeyCache.set(cacheKey, formatted);
168+
return formatted;
169+
}
170+
140171
const tz = safeTimeZone(timezone);
141172
if (tz === 'UTC' && isoUtcTimestampPattern.test(timestamp)) {
142173
return timestamp.slice(0, 10);
143174
}
144175
const date = new Date(timestamp);
176+
if (tz === 'UTC') {
177+
return formatUTCDateKey(date);
178+
}
145179
const formatter = getDateFormatter(tz);
146180
const formatted = formatDateKeyWithFormatter(formatter, date);
147181
const cacheKey = `${tz}:${formatted}`;
@@ -155,11 +189,25 @@ export function formatDateKey(timestamp: string, timezone?: string): string {
155189
}
156190

157191
export function formatMonthKey(timestamp: string, timezone?: string): string {
192+
if (timezone == null || timezone.trim() === '') {
193+
const cacheKey = `local:${timestamp.slice(0, 13)}`;
194+
const cached = monthKeyCache.get(cacheKey);
195+
if (cached != null) {
196+
return cached;
197+
}
198+
const formatted = formatLocalMonthKey(new Date(timestamp));
199+
monthKeyCache.set(cacheKey, formatted);
200+
return formatted;
201+
}
202+
158203
const tz = safeTimeZone(timezone);
159204
if (tz === 'UTC' && isoUtcTimestampPattern.test(timestamp)) {
160205
return timestamp.slice(0, 7);
161206
}
162207
const date = new Date(timestamp);
208+
if (tz === 'UTC') {
209+
return formatUTCMonthKey(date);
210+
}
163211
const formatter = getMonthFormatter(tz);
164212
const formatted = formatMonthKeyWithFormatter(formatter, date);
165213
const cacheKey = `${tz}:${formatted}`;
@@ -405,6 +453,7 @@ if (import.meta.vitest != null) {
405453

406454
afterEach(() => {
407455
vi.unstubAllGlobals();
456+
vi.unstubAllEnvs();
408457
});
409458

410459
it('rejects impossible calendar date filters', () => {
@@ -438,6 +487,27 @@ if (import.meta.vitest != null) {
438487
expect(formatMonthKey('2026-05-16T12:34:56.000Z', 'UTC')).toBe('2026-05');
439488
});
440489

490+
it('uses local Date getters when timezone is omitted', () => {
491+
vi.stubEnv('TZ', 'Asia/Tokyo');
492+
const DateTimeFormat = Intl.DateTimeFormat;
493+
function DateTimeFormatMock(
494+
locale?: string | string[],
495+
options?: Intl.DateTimeFormatOptions,
496+
): Intl.DateTimeFormat {
497+
if (options?.timeZone != null) {
498+
throw new Error('date formatter should not be created');
499+
}
500+
return new DateTimeFormat(locale, options);
501+
}
502+
vi.stubGlobal('Intl', {
503+
...Intl,
504+
DateTimeFormat: DateTimeFormatMock,
505+
});
506+
507+
expect(formatDateKey('2026-05-16T15:34:56.000Z')).toBe('2026-05-17');
508+
expect(formatMonthKey('2026-05-16T15:34:56.000Z')).toBe('2026-05');
509+
});
510+
441511
it('uses locale formatting directly when it already produces machine date keys', () => {
442512
const DateTimeFormat = Intl.DateTimeFormat;
443513
const formatToParts = vi.fn((formatter: Intl.DateTimeFormat, date: Date) =>

0 commit comments

Comments
 (0)