Skip to content

Commit 32c27ca

Browse files
committed
perf(ccusage): reduce native JSONL parse work
Avoid parsing JSON for transcript lines that cannot contain usage tokens in the native loader. The file-order timestamp prepass now extracts compact JSONL timestamps directly and falls back to the existing serde_json path when that fast path does not apply. This keeps daily, session, monthly, and blocks JSON output identical while cutting the local offline daily benchmark from roughly 1.25s median to roughly 0.65s median. Refs #977
1 parent 61e12b8 commit 32c27ca

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

‎crates/ccusage/src/main.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,9 @@ fn read_usage_file_with(
683683
if line.trim().is_empty() {
684684
return None;
685685
}
686+
if !line.contains("\"input_tokens\"") {
687+
return None;
688+
}
686689
let value = serde_json::from_str::<Value>(&line).ok()?;
687690
if !is_ts_usage_value(&value) {
688691
return None;
@@ -982,6 +985,9 @@ fn earliest_timestamp(path: &Path) -> Option<DateTime<Utc>> {
982985
.lines()
983986
.map_while(Result::ok)
984987
.filter_map(|line| {
988+
if let Some(timestamp) = timestamp_from_line(&line) {
989+
return Some(timestamp);
990+
}
985991
let value = serde_json::from_str::<Value>(&line).ok()?;
986992
let timestamp = value.get("timestamp")?.as_str()?;
987993
DateTime::parse_from_rfc3339(timestamp)
@@ -991,6 +997,14 @@ fn earliest_timestamp(path: &Path) -> Option<DateTime<Utc>> {
991997
.min()
992998
}
993999

1000+
fn timestamp_from_line(line: &str) -> Option<DateTime<Utc>> {
1001+
let start = line.find("\"timestamp\":\"")? + "\"timestamp\":\"".len();
1002+
let end = line[start..].find('"')? + start;
1003+
DateTime::parse_from_rfc3339(&line[start..end])
1004+
.ok()
1005+
.and_then(|value| DateTime::from_timestamp_millis(value.timestamp_millis()))
1006+
}
1007+
9941008
fn extract_project(path: &Path) -> String {
9951009
let mut saw_projects = false;
9961010
for part in path
@@ -2006,4 +2020,14 @@ mod tests {
20062020
"2024-01-01"
20072021
);
20082022
}
2023+
2024+
#[test]
2025+
fn extracts_compact_jsonl_timestamp() {
2026+
let timestamp =
2027+
timestamp_from_line(r#"{"timestamp":"2026-05-11T12:34:56.789Z","message":{}}"#)
2028+
.unwrap();
2029+
2030+
assert_eq!(timestamp.to_rfc3339(), "2026-05-11T12:34:56.789+00:00");
2031+
assert!(timestamp_from_line(r#"{"timestamp": "2026-05-11T12:34:56.789Z"}"#).is_none());
2032+
}
20092033
}

0 commit comments

Comments
 (0)