Skip to content

Optimize JSONL scanner chunk parsing to reduce CPU usage#392

Merged
ratulsarna merged 2 commits intosteipete:mainfrom
asonawalla:codex/reduce-cpu-jsonl-scan
Feb 24, 2026
Merged

Optimize JSONL scanner chunk parsing to reduce CPU usage#392
ratulsarna merged 2 commits intosteipete:mainfrom
asonawalla:codex/reduce-cpu-jsonl-scan

Conversation

@asonawalla
Copy link
Contributor

@asonawalla asonawalla commented Feb 17, 2026

Summary

This PR reduces CPU usage in the local cost-usage scanner by optimizing JSONL line parsing in CostUsageJsonl.

The previous scanner loop repeatedly removed bytes from the front of a growing buffer while splitting lines. On large Claude logs, that led to a lot of extra per-chunk work.

What changed

  • Reworked chunk parsing in CostUsageJsonl.scan to process segment slices directly.
  • Replaced front-buffer-removal behavior with an append-segment approach that avoids repeated shifting of buffer contents.
  • Kept existing behavior for partial lines and truncation limits.
  • Added regression tests for:
    • lines that span read chunk boundaries
    • prefix-limited lines flagged as truncated

Why

On large local Claude histories, stack samples showed scanner hotspots in the cost scan path (CostUsageFetcher.loadTokenSnapshot -> CostUsageScanner.scanClaudeRoot -> CostUsageJsonl.scan).

This change targets that hotspot by making newline splitting and carryover handling linear and lower-overhead for large files.

Validation

  • pnpm check
  • swift test (passed: 746 tests)

Anecdotal runtime observation

After running this build locally for several hours, @asonawalla observed that CodexBar no longer appears among top CPU processes (previously it was frequently visible). This is anecdotal but consistent with the expected scanner improvement.

AI disclosure

This code and PR were generated with Codex and reviewed by @asonawalla

@ratulsarna ratulsarna self-assigned this Feb 24, 2026
@ratulsarna ratulsarna merged commit 2511016 into steipete:main Feb 24, 2026
4 checks passed
@ratulsarna
Copy link
Collaborator

Thanks @asonawalla !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants