Skip to content

Commit 641f7a1

Browse files
committed
perf(ccusage): use native JSONL discovery
Replace tinyglobby usage in the ccusage hot path with a small native readdir-based JSONL walker. The loader now avoids starting the glob engine for daily, session-by-id, block, and debug mismatch discovery, keeps deterministic file order with path sorting, and removes tinyglobby from the ccusage app dependencies. Reliable runtime benchmark data is not included in this commit because process checks still showed cmux at roughly 150% CPU and another node ccusage process. The built bundle remains 515.86 kB total after this change. Verified with pnpm run format, pnpm typecheck, pnpm --filter ccusage exec vitest run src/data-loader.ts src/debug.ts, pnpm --filter ccusage run build, pnpm run test, and JS/Rust token-total parity checks for daily/session/monthly/weekly/blocks.
1 parent 8051465 commit 641f7a1

5 files changed

Lines changed: 40 additions & 46 deletions

File tree

‎apps/ccusage/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
"pretty-ms": "catalog:runtime",
9696
"publint": "catalog:lint",
9797
"string-width": "catalog:runtime",
98-
"tinyglobby": "catalog:runtime",
9998
"tsdown": "catalog:build",
10099
"type-fest": "catalog:runtime",
101100
"unplugin-macros": "catalog:build",

‎apps/ccusage/src/_consts.ts‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,6 @@ export const CLAUDE_CONFIG_DIR_ENV = 'CLAUDE_CONFIG_DIR';
6868
*/
6969
export const CLAUDE_PROJECTS_DIR_NAME = 'projects';
7070

71-
/**
72-
* JSONL file glob pattern for finding usage data files
73-
* Used to recursively find all JSONL files in project directories
74-
*/
75-
export const USAGE_DATA_GLOB_PATTERN = '**/*.jsonl';
76-
7771
/**
7872
* Default port for MCP server HTTP transport
7973
* Used when no port is specified for MCP server communication

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

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type { LoadedUsageEntry, SessionBlock } from './_session-blocks.ts';
1313
import type { ActivityDate, Bucket, CostMode, ModelName, SortOrder, Version } from './_types.ts';
1414
import { Buffer } from 'node:buffer';
1515
import { createReadStream, createWriteStream } from 'node:fs';
16-
import { readFile, stat, utimes } from 'node:fs/promises';
16+
import { readdir, readFile, stat, utimes } from 'node:fs/promises';
1717
import os from 'node:os';
1818
import path from 'node:path';
1919
import process from 'node:process';
@@ -23,14 +23,12 @@ import { Result } from '@praha/byethrow';
2323
import { sort } from 'fast-sort';
2424
import { createFixture } from 'fs-fixture';
2525
import { isDirectorySync } from 'path-type';
26-
import { glob } from 'tinyglobby';
2726
import * as v from 'valibot';
2827
import {
2928
CLAUDE_CONFIG_DIR_ENV,
3029
CLAUDE_PROJECTS_DIR_NAME,
3130
DEFAULT_CLAUDE_CODE_PATH,
3231
DEFAULT_CLAUDE_CONFIG_PATH,
33-
USAGE_DATA_GLOB_PATTERN,
3432
USER_HOME_DIR,
3533
} from './_consts.ts';
3634
import {
@@ -1125,6 +1123,30 @@ export type GlobResult = {
11251123
baseDir: string;
11261124
};
11271125

1126+
export async function collectJsonlFiles(root: string): Promise<string[]> {
1127+
const files: string[] = [];
1128+
const walk = async (dir: string): Promise<void> => {
1129+
let entries;
1130+
try {
1131+
entries = await readdir(dir, { withFileTypes: true });
1132+
} catch {
1133+
return;
1134+
}
1135+
await Promise.all(
1136+
entries.map(async (entry) => {
1137+
const filePath = path.join(dir, entry.name);
1138+
if (entry.isDirectory()) {
1139+
await walk(filePath);
1140+
} else if (entry.isFile() && entry.name.endsWith('.jsonl')) {
1141+
files.push(filePath);
1142+
}
1143+
}),
1144+
);
1145+
};
1146+
await walk(root);
1147+
return files.sort((a, b) => a.localeCompare(b));
1148+
}
1149+
11281150
/**
11291151
* Glob files from multiple Claude paths in parallel
11301152
* @param claudePaths - Array of Claude base paths
@@ -1133,12 +1155,8 @@ export type GlobResult = {
11331155
export async function globUsageFiles(claudePaths: string[]): Promise<GlobResult[]> {
11341156
const filePromises = claudePaths.map(async (claudePath) => {
11351157
const claudeDir = path.join(claudePath, CLAUDE_PROJECTS_DIR_NAME);
1136-
const files = await glob([USAGE_DATA_GLOB_PATTERN], {
1137-
cwd: claudeDir,
1138-
absolute: true,
1139-
}).catch(() => []); // Gracefully handle errors for individual paths
1158+
const files = await collectJsonlFiles(claudeDir);
11401159

1141-
// Map each file to include its base directory
11421160
return files.map((file) => ({ file, baseDir: claudeDir }));
11431161
});
11441162
return (await Promise.all(filePromises)).flat();
@@ -1629,19 +1647,17 @@ export async function loadSessionUsageById(
16291647
): Promise<{ totalCost: number; entries: UsageData[] } | null> {
16301648
const claudePaths = getClaudePaths();
16311649

1632-
// Find the JSONL file for this session ID
1633-
// On Windows, replace backslashes from path.join with forward slashes for tinyglobby compatibility
1634-
const patterns = claudePaths.map((p) =>
1635-
path.join(p, 'projects', '**', `${sessionId}.jsonl`).replace(/\\/g, '/'),
1636-
);
1637-
// Absolute paths important on Windows - relative paths will break if session file is on different disk.
1638-
const jsonlFiles = await glob(patterns, { absolute: true });
1639-
1640-
if (jsonlFiles.length === 0) {
1641-
return null;
1650+
const targetFile = `${sessionId}.jsonl`;
1651+
let file: string | undefined;
1652+
for (const claudePath of claudePaths) {
1653+
const claudeDir = path.join(claudePath, CLAUDE_PROJECTS_DIR_NAME);
1654+
file = (await collectJsonlFiles(claudeDir)).find(
1655+
(candidate) => path.basename(candidate) === targetFile,
1656+
);
1657+
if (file != null) {
1658+
break;
1659+
}
16421660
}
1643-
1644-
const file = jsonlFiles[0];
16451661
if (file == null) {
16461662
return null;
16471663
}
@@ -1871,11 +1887,7 @@ export async function loadSessionBlockData(options?: LoadOptions): Promise<Sessi
18711887
const allFiles: string[] = [];
18721888
for (const claudePath of claudePaths) {
18731889
const claudeDir = path.join(claudePath, CLAUDE_PROJECTS_DIR_NAME);
1874-
const files = await glob([USAGE_DATA_GLOB_PATTERN], {
1875-
cwd: claudeDir,
1876-
absolute: true,
1877-
});
1878-
allFiles.push(...files);
1890+
allFiles.push(...(await collectJsonlFiles(claudeDir)));
18791891
}
18801892

18811893
if (allFiles.length === 0) {

‎apps/ccusage/src/debug.ts‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,10 @@ import { readFile } from 'node:fs/promises';
1111
import path from 'node:path';
1212
import { Result } from '@praha/byethrow';
1313
import { createFixture } from 'fs-fixture';
14-
import { glob } from 'tinyglobby';
1514
import * as v from 'valibot';
16-
import {
17-
CLAUDE_PROJECTS_DIR_NAME,
18-
DEBUG_MATCH_THRESHOLD_PERCENT,
19-
USAGE_DATA_GLOB_PATTERN,
20-
} from './_consts.ts';
15+
import { CLAUDE_PROJECTS_DIR_NAME, DEBUG_MATCH_THRESHOLD_PERCENT } from './_consts.ts';
2116
import { PricingFetcher } from './_pricing-fetcher.ts';
22-
import { getClaudePaths, usageDataSchema } from './data-loader.ts';
17+
import { collectJsonlFiles, getClaudePaths, usageDataSchema } from './data-loader.ts';
2318
import { logger } from './logger.ts';
2419

2520
/**
@@ -87,10 +82,7 @@ export async function detectMismatches(claudePath?: string): Promise<MismatchSta
8782
}
8883
claudeDir = path.join(paths[0]!, CLAUDE_PROJECTS_DIR_NAME);
8984
}
90-
const files = await glob([USAGE_DATA_GLOB_PATTERN], {
91-
cwd: claudeDir,
92-
absolute: true,
93-
});
85+
const files = await collectJsonlFiles(claudeDir);
9486

9587
// Use PricingFetcher with using statement for automatic cleanup
9688
using fetcher = new PricingFetcher();

‎pnpm-lock.yaml‎

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)