Skip to content

perf(usage-store): loadUsageRecords reads the entire usage.jsonl into memory before filtering #72

@github-actions

Description

@github-actions

Problem

apps/desktop/src/main/usage/store.ts L176–193

const lines = readFileSync(USAGE_FILE, 'utf8').split('\n').filter(Boolean);
// then a for-loop applies fromTs/toTs/agentId filters

usage.jsonl grows over time (the comment notes ~1–2 MB per 10,000 requests), but every query reads the entire file into memory, parses all lines, then discards most of them. For time-range queries such as "today's usage," the vast majority of records are thrown away.

Suggested fix

Maintain an in-memory index sorted by timestamp (line offset → ts) and use binary search to locate the fromTs starting point, reading and parsing only the relevant segment. Alternatively, switch to streaming reads for large files.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions