Repository navigation
Commit cdeb62a
perf(adapter/copilot): stream events.jsonl via BufReader; document u64 nano-AIU typing
Another external code review on PR ccusage#1209 surfaced two actionable
items.
# Stream events.jsonl instead of slurping into a Vec<u8>
`parse_session_state_file` used `fs::read(path)?` to load the entire
file into a Vec<u8>, then `bytes.split(|b| b == b'\n')` to iterate
lines as byte slices. For real-world session-state files (the reviewer
cites 69MB+ on heavy users) this briefly allocates the whole file
into RAM per parsed session — fine on modern machines but unnecessarily
generous.
Switched to:
let reader = BufReader::new(fs::File::open(path)?);
for line_result in reader.split(b'\n') {
let line_bytes = line_result?;
...
}
`BufReader::split(b'\n')` returns `io::Result<Vec<u8>>` per line,
preserving the byte-level UTF-8 tolerance and trailing-line semantics
the previous code relied on (`std::str::from_utf8` skip and
empty-line skip both still hold for the new byte slice). Peak RAM
per file drops from O(file-size) to O(line-size), bounded by
BufReader's 8KB internal buffer plus one line's Vec<u8>.
Behavioral parity:
- Empty trailing segments after the last `\n` are still skipped by
the existing whitespace/empty checks (BufReader::split matches
Vec::split's no-trailing-empty-element behavior — the final segment
is only returned if it has bytes).
- Mid-file read errors now propagate via `?` rather than being
impossible (since fs::read was all-or-nothing). Session-state files
are append-only in practice, so concurrent writers don't trigger
this; if they ever do, the failure mode is "skip this file's
remaining lines" rather than "silently truncated parse," which is
the correct behavior.
# Explain why total_nano_aiu stays u64 (vs requests.cost which is f64)
The same review noted that `total_nano_aiu: Option<u64>` on
`ShutdownData` and `ModelMetrics` lacks the float-tolerance
hardening that `requests.cost` got — if a future Copilot CLI ever
emits `totalNanoAiu` as `1.5e11` or `100000000000.0`, the whole
`ShutdownEvent` deserialize would fail and every model in that
shutdown would be silently dropped (the exact failure class the
`requests.cost: f64` change fixed).
The risk is genuinely low — "nano" units are integer by definition,
nano-AIU has no meaningful fractional component, and the CLI has
emitted integer JSON for AIU across every observed version. But the
asymmetry with `requests.cost` (which IS legitimately fractional —
2% of real rows show Opus 4.7's 7.5× multiplier as 7.5) deserves an
in-source explanation so future maintainers don't repeat the
audit.
Added a doc-comment on both fields explaining:
- why u64 is safe here (integer by definition)
- what the failure mode would be if it weren't (same drop-the-event
cascade fixed for requests.cost)
- the escape hatch (switch to a tolerant deserializer that accepts
integer-or-float-with-zero-fract)
No code change — the reviewer themselves rated the risk as "genuinely
low" and suggested "either a tolerant number helper or a comment
explaining why u64 is safe here when cost wasn't." Picked the comment
route because the helper would add complexity for a hypothetical
schema change.
# Other findings deliberately skipped
- Credit-only content dedup collapsing legitimately-distinct
snapshots: documented as deliberate tradeoff (real-world duplicates
outweigh the hypothetical content-collision case).
- OTel dead code retained pending ccusage#1208: deliberate scope split.
- config-schema.json oxfmt churn: documented in PR description;
fixing it would require an upstream-first reformat commit.
- Empty-state message on `--since/--until` filtered output: matches
existing adapter convention; changing it would be a cross-adapter
pattern shift.
# Validation
- `cargo clippy --workspace --all-targets -- -D warnings` clean.
- 257 ccusage tests + 79 across the rest of the workspace, all pass.
All test fixtures continue to round-trip through the new BufReader
path (the byte-level handling is identical to the prior Vec::split).
- Smoke against `~/.copilot/session-state/`: 74 days, $6,480.33 —
unchanged. No semantic difference, only a memory-profile
improvement.
Refs ccusage#1174
Co-authored-by: Copilot <[email protected]>1 parent e48a25c commit cdeb62a
1 file changed
Lines changed: 25 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
565 | 566 | | |
566 | 567 | | |
567 | 568 | | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
568 | 580 | | |
569 | 581 | | |
570 | 582 | | |
| |||
576 | 588 | | |
577 | 589 | | |
578 | 590 | | |
| 591 | + | |
| 592 | + | |
579 | 593 | | |
580 | 594 | | |
581 | 595 | | |
| |||
619 | 633 | | |
620 | 634 | | |
621 | 635 | | |
622 | | - | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
623 | 645 | | |
624 | 646 | | |
625 | 647 | | |
626 | | - | |
| 648 | + | |
| 649 | + | |
627 | 650 | | |
628 | 651 | | |
629 | 652 | | |
| |||
0 commit comments