Skip to content

Commit f5020ef

Browse files
committed
perf(ccusage): parallelise JSONL loading by CPU count
Parallelise JSONL file loading across daily, session, and blocks reports using os.availableParallelism() by default. This keeps the worker count tied to the host CPU instead of a fixed cap, while --single-thread and CCUSAGE_JSONL_READ_CONCURRENCY provide deterministic overrides for benchmarking and constrained environments. Small JSONL files are now read in one buffered pass, with the existing streaming path retained for files larger than 32 MiB. Cost calculation also keeps a synchronous fast path when --offline or cached costUSD values make async pricing unnecessary. Dedupe is still applied after each file result is collected, preserving the original file-order replacement behaviour and keeping JS/Rust token totals identical for daily, session, monthly, and blocks JSON output. Benchmark on Apple M3 Pro, Node 24.15.0, os.availableParallelism()=11, built JS CLI at apps/ccusage/dist/index.js, Rust release CLI at /Users/ryoppippi/ghq/github.com/ryoppippi/ccusage/target/release/ccusage, LOG_LEVEL=0, --offline --json, mitata min_samples=5: JS: daily 3.51s, session 3.07s, monthly 3.17s, blocks 6.51s. Rust: daily 640.16ms, session 639.67ms, monthly 660.97ms, blocks 649.93ms. Previous JS PR baseline from bd7d2d4: daily 3.85s, session 3.99s, monthly 3.94s, blocks 8.36s. Verified with pnpm run format, pnpm typecheck, pnpm --filter ccusage exec vitest run src/data-loader.ts, pnpm --filter ccusage run build, pnpm run test, and JS/Rust token-total parity checks for daily/session/monthly/blocks.
1 parent bd7d2d4 commit f5020ef

5 files changed

Lines changed: 303 additions & 114 deletions

File tree

‎apps/ccusage/config-schema.json‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@
6666
"markdownDescription": "Use cached pricing data for Claude models instead of fetching from API",
6767
"default": false
6868
},
69+
"singleThread": {
70+
"type": "boolean",
71+
"description": "Disable parallel JSONL file loading",
72+
"markdownDescription": "Disable parallel JSONL file loading",
73+
"default": false
74+
},
6975
"color": {
7076
"type": "boolean",
7177
"description": "Enable colored output (default: auto). FORCE_COLOR=1 has the same effect.",
@@ -157,6 +163,12 @@
157163
"markdownDescription": "Use cached pricing data for Claude models instead of fetching from API",
158164
"default": false
159165
},
166+
"singleThread": {
167+
"type": "boolean",
168+
"description": "Disable parallel JSONL file loading",
169+
"markdownDescription": "Disable parallel JSONL file loading",
170+
"default": false
171+
},
160172
"color": {
161173
"type": "boolean",
162174
"description": "Enable colored output (default: auto). FORCE_COLOR=1 has the same effect.",
@@ -259,6 +271,12 @@
259271
"markdownDescription": "Use cached pricing data for Claude models instead of fetching from API",
260272
"default": false
261273
},
274+
"singleThread": {
275+
"type": "boolean",
276+
"description": "Disable parallel JSONL file loading",
277+
"markdownDescription": "Disable parallel JSONL file loading",
278+
"default": false
279+
},
262280
"color": {
263281
"type": "boolean",
264282
"description": "Enable colored output (default: auto). FORCE_COLOR=1 has the same effect.",
@@ -345,6 +363,12 @@
345363
"markdownDescription": "Use cached pricing data for Claude models instead of fetching from API",
346364
"default": false
347365
},
366+
"singleThread": {
367+
"type": "boolean",
368+
"description": "Disable parallel JSONL file loading",
369+
"markdownDescription": "Disable parallel JSONL file loading",
370+
"default": false
371+
},
348372
"color": {
349373
"type": "boolean",
350374
"description": "Enable colored output (default: auto). FORCE_COLOR=1 has the same effect.",
@@ -439,6 +463,12 @@
439463
"markdownDescription": "Use cached pricing data for Claude models instead of fetching from API",
440464
"default": false
441465
},
466+
"singleThread": {
467+
"type": "boolean",
468+
"description": "Disable parallel JSONL file loading",
469+
"markdownDescription": "Disable parallel JSONL file loading",
470+
"default": false
471+
},
442472
"color": {
443473
"type": "boolean",
444474
"description": "Enable colored output (default: auto). FORCE_COLOR=1 has the same effect.",
@@ -530,6 +560,12 @@
530560
"markdownDescription": "Use cached pricing data for Claude models instead of fetching from API",
531561
"default": false
532562
},
563+
"singleThread": {
564+
"type": "boolean",
565+
"description": "Disable parallel JSONL file loading",
566+
"markdownDescription": "Disable parallel JSONL file loading",
567+
"default": false
568+
},
533569
"color": {
534570
"type": "boolean",
535571
"description": "Enable colored output (default: auto). FORCE_COLOR=1 has the same effect.",

‎apps/ccusage/src/_shared-args.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ export const sharedArgs = {
7373
description: 'Use cached pricing data for Claude models instead of fetching from API',
7474
default: false,
7575
},
76+
singleThread: {
77+
type: 'boolean',
78+
description: 'Disable parallel JSONL file loading',
79+
default: false,
80+
},
7681
color: {
7782
// --color and FORCE_COLOR=1 is handled by picocolors
7883
type: 'boolean',

‎apps/ccusage/src/commands/blocks.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ export const blocksCommand = define({
169169
mode: ctx.values.mode,
170170
order: ctx.values.order,
171171
offline: ctx.values.offline,
172+
singleThread: ctx.values.singleThread,
172173
sessionDurationHours: ctx.values.sessionLength,
173174
timezone: ctx.values.timezone,
174175
});

‎apps/ccusage/src/commands/session.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export const sessionCommand = define({
6868
until: ctx.values.until,
6969
mode: ctx.values.mode,
7070
offline: ctx.values.offline,
71+
singleThread: ctx.values.singleThread,
7172
timezone: ctx.values.timezone,
7273
});
7374

0 commit comments

Comments
 (0)