Skip to content

Commit cdeb62a

Browse files
jay-tauCopilot
andcommitted
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

File tree

  • rust/crates/ccusage/src/adapter/copilot

‎rust/crates/ccusage/src/adapter/copilot/parser.rs‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{
22
collections::{HashMap, HashSet},
33
fs,
4+
io::{BufRead, BufReader},
45
path::Path,
56
};
67

@@ -565,6 +566,17 @@ struct ShutdownEvent {
565566
struct ShutdownData {
566567
#[serde(default)]
567568
model_metrics: HashMap<String, ModelMetrics>,
569+
// `total_nano_aiu` (and its per-model sibling on `ModelMetrics`) are
570+
// typed as `u64` rather than `f64` because "nano" units are integer by
571+
// definition — there's no meaningful fractional nano-AIU. This means
572+
// a future Copilot CLI that emits the field as a JSON float (e.g.
573+
// `1.5e11` or `100000000000.0`) would fail the whole `ShutdownEvent`
574+
// deserialize and drop every model in that shutdown — the exact
575+
// failure class fixed for `requests.cost` (which IS legitimately
576+
// fractional). Real local data only ever ships integer JSON for AIU,
577+
// so the risk is low; if it ever changes, switch to a tolerant
578+
// deserializer that accepts either form and rejects non-zero
579+
// fractional parts.
568580
#[serde(default)]
569581
total_nano_aiu: Option<u64>,
570582
}
@@ -576,6 +588,8 @@ struct ModelMetrics {
576588
usage: SessionUsage,
577589
#[serde(default)]
578590
requests: SessionRequests,
591+
// See the `total_nano_aiu` note on `ShutdownData` above for why this
592+
// is `u64` rather than `f64`.
579593
#[serde(default)]
580594
total_nano_aiu: Option<u64>,
581595
}
@@ -619,11 +633,20 @@ struct SessionRequests {
619633
}
620634

621635
pub(super) fn parse_session_state_file(path: &Path) -> Result<Vec<CopilotUsageEntry>> {
622-
let bytes = fs::read(path)?;
636+
// Stream the file via `BufReader::split(b'\n')` rather than
637+
// `fs::read` so peak memory stays bounded by line size (a few KB) and
638+
// not file size. Real-world `events.jsonl` files reach 69MB+ for
639+
// heavy users, where the one-shot read would briefly allocate a
640+
// whole-file `Vec<u8>` per session-state file. Byte-level splitting
641+
// (rather than `.lines()`) preserves the existing
642+
// tolerate-invalid-UTF-8 / tolerate-truncated-trailing-line semantics.
643+
let file = fs::File::open(path)?;
644+
let reader = BufReader::new(file);
623645
let session_id = session_id_from_path(path).unwrap_or_else(|| "unknown-session".to_string());
624646

625647
let mut entries = Vec::new();
626-
for line_bytes in bytes.split(|&byte| byte == b'\n') {
648+
for line_result in reader.split(b'\n') {
649+
let line_bytes = line_result?;
627650
let trimmed: &[u8] = match line_bytes.iter().position(|&b| !b.is_ascii_whitespace()) {
628651
Some(start) => &line_bytes[start..],
629652
None => continue,

0 commit comments

Comments
 (0)