Skip to content

Commit 7bf9e14

Browse files
committed
perf(ccusage): reuse usage JSONL scanner for blocks
Blocks no longer need non-usage JSONL timestamps for file ordering, but the block Bun byte path still iterated every line and manually checked whether the current line contained the usage marker. Reuse the shared usage-line scanner instead. This keeps blocks on the same marker-driven path as daily/session, removes the redundant per-line marker guard, and shrinks the ccusage build from 405.25 kB to 404.44 kB. Same-host A/B, Apple M3 Pro, real local Claude data, LOG_LEVEL=0, COLUMNS=200, built Bun dist, hyperfine via comma: - blocks --offline --json: 419.2ms +/- 20.1ms -> 412.0ms +/- 5.8ms (~1.02x) - blocks --offline table: 420.7ms +/- 21.2ms -> 416.9ms +/- 9.4ms (~1.01x) Latest JS/Rust #977 reference after this change: - JS daily/session/blocks JSON: 352.9ms / 368.8ms / 411.4ms - Rust daily/session/blocks JSON: 245.9ms / 253.1ms / 256.1ms Validation: pnpm run format; pnpm typecheck; pnpm run test; pnpm --filter ccusage run build; baseline/current JSON parity for daily/session/blocks/monthly/weekly; baseline/current table parity for daily/session/blocks; Bun/Node JSON parity for daily/session/blocks/monthly/weekly.
1 parent f58638b commit 7bf9e14

1 file changed

Lines changed: 1 addition & 26 deletions

File tree

apps/ccusage/src/data-loader.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,10 +2007,6 @@ async function collectBlockFileResult(
20072007

20082008
const processLine = (line: string): void => {
20092009
try {
2010-
if (!line.includes(USAGE_LINE_MARKER)) {
2011-
return;
2012-
}
2013-
20142010
const data = parseUsageDataLine(line);
20152011
if (data == null) {
20162012
return;
@@ -2074,28 +2070,7 @@ async function collectBlockFileResult(
20742070
}
20752071
};
20762072

2077-
const bytes = await readBufferedJSONLBytes(file);
2078-
if (bytes != null) {
2079-
const content = Buffer.isBuffer(bytes)
2080-
? bytes
2081-
: Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
2082-
let lineStart = 0;
2083-
let markerIndex = content.indexOf(USAGE_LINE_MARKER_BUFFER);
2084-
while (lineStart < content.length) {
2085-
let lineEnd = content.indexOf(10, lineStart);
2086-
if (lineEnd === -1) {
2087-
lineEnd = content.length;
2088-
}
2089-
const decodeEnd = lineEnd > lineStart && content[lineEnd - 1] === 13 ? lineEnd - 1 : lineEnd;
2090-
if (markerIndex !== -1 && markerIndex < decodeEnd) {
2091-
processLine(content.subarray(lineStart, decodeEnd).toString('utf8'));
2092-
markerIndex = content.indexOf(USAGE_LINE_MARKER_BUFFER, lineEnd + 1);
2093-
}
2094-
lineStart = lineEnd + 1;
2095-
}
2096-
} else {
2097-
await processJSONLFileByLine(file, processLine);
2098-
}
2073+
await processJSONLUsageFileByLine(file, processLine);
20992074

21002075
return {
21012076
file,

0 commit comments

Comments
 (0)