Skip to content

Commit a06f4bc

Browse files
authored
perf(claude): halve ccusage claude report time by skipping redundant JSON work (#2058)
* perf(claude): validate nullable fields only when a guarded member is null deserialize_usage_line sent every line containing the bytes `null` through a full serde_json::Value tree (duplicate-checked BTreeMaps, owned strings for message content) before re-deserializing it into the typed entry. That fallback was meant for the rare lines where a non-nullable member such as `model` or `sessionId` is null, but current Claude Code writes always-null members (`stop_sequence`, `container`, `stop_details`, `context_management`, ...) on every assistant line, so virtually every usage line took it. On a real 8 GB transcript set it was ~45% of all CPU time. The pre-check now looks at what precedes each `null`: only a value of a member named in is_unsupported_nullable_field (or a key containing an escape, whose decoded name can't be read from the bytes) sends the line to the value-tree check. A false positive only costs the slower parse, and the test helper asserts the pre-check never skips a line the value check rejects, across all existing nullability fixtures. * perf(core): read the pricing lookup cache under a shared lock PricingMap::find memoizes lookups per model behind a Mutex, and the Claude parsers call it at least twice per entry from every parallel file worker. With a handful of distinct models the cache is read-only after warm-up, yet each read serialized all workers on one lock: ~14% of sampled CPU was spent in __psynch_mutexwait on an 18-core machine. An RwLock lets the readers proceed together; writes still happen once per newly seen model, and clearing goes through get_mut since it already holds &mut self. * perf(claude): parse daily usage lines without the untagged enum buffer DailyUsageLine was #[serde(untagged)], so serde first buffered every usage line, message content included, into its private Content tree and then replayed it for each variant. After the null pre-check fix that buffering was ~49% of the remaining CPU in `ccusage claude daily`. The direct entry and the AgentProgress wrapper are now tried against the raw bytes in the same order, keeping untagged's first-match semantics. AgentProgress lines parse twice, but they are rare and the old path already paid for the buffer plus the replay. * fix(claude): decide duplicate members by the typed parse on every line Lines that needed the nullable-field check were parsed into a UniqueJsonValue tree that rejected any duplicate member, while every other line went straight to the typed parse, which rejects only duplicate known fields. On main the split was "contains null or not"; after the pre-check change it became "contains a guarded null or not". Either way, whether a line with a duplicate unknown member loaded depended on an unrelated null. #1719 added UniqueJsonValue to keep typed Serde's duplicate-field rejection when deserializing from the shared Value, not to reject more. The null check now only validates a last-wins Value (as JSON.parse does in the TypeScript loader it mirrors), and the typed parse of the raw line decides everything else for every line. The duplicate-checking visitor is gone; the second parse only happens on the ~0.1% of lines with a guarded null. * test(claude): cover mixed-shape and AgentProgress lines in the daily CLI A line carrying both a direct entry and an AgentProgress wrapper must load as the direct entry, and a wrapped-only line must still be unwrapped; check the daily JSON per-day and total tokens and cost end to end.
1 parent de43952 commit a06f4bc

4 files changed

Lines changed: 189 additions & 143 deletions

File tree

‎rust/adapters/claude/src/daily.rs‎

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -227,28 +227,16 @@ struct DailyUsageEntry {
227227
is_sidechain: Option<bool>,
228228
}
229229

230-
#[derive(Debug, Deserialize)]
231-
#[serde(untagged)]
232-
enum DailyUsageLine {
233-
Direct(DailyUsageEntry),
234-
AgentProgress(DailyAgentProgressEntry),
235-
}
236-
237-
impl DailyUsageLine {
238-
fn into_entry(self) -> DailyUsageEntry {
239-
match self {
240-
DailyUsageLine::Direct(entry) => entry,
241-
DailyUsageLine::AgentProgress(entry) => DailyUsageEntry {
242-
timestamp: entry.data.message.timestamp,
243-
message: entry.data.message.message,
244-
version: None,
245-
session_id: entry.session_id,
246-
cost_usd: entry.data.message.cost_usd,
247-
request_id: entry.data.message.request_id,
248-
is_sidechain: entry.data.message.is_sidechain,
249-
},
250-
}
251-
}
230+
/// Parses a direct assistant line, falling back to the AgentProgress wrapper.
231+
///
232+
/// Each shape is tried against the raw bytes in the order `#[serde(untagged)]` would
233+
/// use; the derive would first buffer every line, message content included, into an
234+
/// intermediate tree.
235+
fn deserialize_daily_usage_line(line: &[u8]) -> Option<DailyUsageEntry> {
236+
deserialize_usage_line::<DailyUsageEntry>(line).or_else(|| {
237+
deserialize_usage_line::<DailyAgentProgressEntry>(line)
238+
.map(DailyAgentProgressEntry::into_entry)
239+
})
252240
}
253241

254242
#[derive(Debug, Deserialize)]
@@ -258,6 +246,20 @@ struct DailyAgentProgressEntry {
258246
data: DailyAgentProgressData,
259247
}
260248

249+
impl DailyAgentProgressEntry {
250+
fn into_entry(self) -> DailyUsageEntry {
251+
DailyUsageEntry {
252+
timestamp: self.data.message.timestamp,
253+
message: self.data.message.message,
254+
version: None,
255+
session_id: self.session_id,
256+
cost_usd: self.data.message.cost_usd,
257+
request_id: self.data.message.request_id,
258+
is_sidechain: self.data.message.is_sidechain,
259+
}
260+
}
261+
}
262+
261263
#[derive(Debug, Deserialize)]
262264
struct DailyAgentProgressData {
263265
message: DailyAgentProgressMessage,
@@ -352,10 +354,9 @@ fn read_daily_usage_file(
352354
if usage_marker.find(line).is_none() {
353355
continue;
354356
}
355-
let Some(data) = deserialize_usage_line::<DailyUsageLine>(line) else {
357+
let Some(data) = deserialize_daily_usage_line(line) else {
356358
continue;
357359
};
358-
let data = data.into_entry();
359360
let Some(timestamp) = parse_ts_timestamp(&data.timestamp) else {
360361
continue;
361362
};
@@ -902,16 +903,26 @@ mod tests {
902903

903904
#[test]
904905
fn propagates_sidechain_metadata_from_agent_progress_lines() {
905-
let data = serde_json::from_str::<super::DailyUsageLine>(
906-
r#"{"sessionId":"session-a","data":{"message":{"timestamp":"2026-03-29T07:00:00.000Z","requestId":"req-sidechain","isSidechain":true,"message":{"usage":{"input_tokens":0,"output_tokens":10,"cache_creation_input_tokens":0,"cache_read_input_tokens":20},"model":"claude-sonnet-4-20250514","id":"msg-sidechain"}}}}"#,
906+
let data = super::deserialize_daily_usage_line(
907+
br#"{"sessionId":"session-a","data":{"message":{"timestamp":"2026-03-29T07:00:00.000Z","requestId":"req-sidechain","isSidechain":true,"message":{"usage":{"input_tokens":0,"output_tokens":10,"cache_creation_input_tokens":0,"cache_read_input_tokens":20},"model":"claude-sonnet-4-20250514","id":"msg-sidechain"}}}}"#,
907908
)
908-
.unwrap()
909-
.into_entry();
909+
.unwrap();
910910

911911
assert_eq!(data.session_id.as_deref(), Some("session-a"));
912912
assert_eq!(data.is_sidechain, Some(true));
913913
}
914914

915+
#[test]
916+
fn prefers_the_direct_entry_over_an_agent_progress_wrapper() {
917+
let data = super::deserialize_daily_usage_line(
918+
br#"{"timestamp":"2026-03-29T08:00:00.000Z","requestId":"req-direct","message":{"usage":{"input_tokens":1,"output_tokens":2},"model":"direct-model","id":"msg-direct"},"data":{"message":{"timestamp":"2026-03-29T07:00:00.000Z","requestId":"req-wrapped","message":{"usage":{"input_tokens":3,"output_tokens":4},"model":"wrapped-model","id":"msg-wrapped"}}}}"#,
919+
)
920+
.unwrap();
921+
922+
assert_eq!(data.request_id.as_deref(), Some("req-direct"));
923+
assert_eq!(data.message.model.as_deref(), Some("direct-model"));
924+
}
925+
915926
#[test]
916927
fn keeps_gateway_daily_usage_from_distinct_sessions_with_reused_message_id() {
917928
let fixture = fs_fixture!({

‎rust/adapters/claude/src/lib.rs‎

Lines changed: 102 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ use std::{
1515
use jiff::tz::TimeZone as JiffTimeZone;
1616
use memchr::memmem;
1717
use rustc_hash::FxHasher;
18-
use serde::{
19-
Deserialize,
20-
de::{DeserializeOwned, Error as _, MapAccess, SeqAccess, Visitor},
21-
};
18+
use serde::{Deserialize, de::DeserializeOwned};
2219
use smallvec::SmallVec;
2320

2421
use crate::{
@@ -815,115 +812,59 @@ fn has_unsupported_null_field(line: &[u8]) -> bool {
815812
let Ok(root) = serde_json::from_slice::<serde_json::Value>(line) else {
816813
return false;
817814
};
818-
has_unsupported_null_field_in_value(&root)
815+
let unsupported = has_unsupported_null_field_in_value(&root);
816+
assert!(
817+
!unsupported || may_have_unsupported_null_field(line),
818+
"the raw pre-check must not skip a line the value check rejects"
819+
);
820+
unsupported
819821
}
820822

821823
/// Deserializes a transcript line while enforcing Claude's nullable-field contract.
822-
/// Lines containing `null` are parsed into a value once so structural validation and
823-
/// typed deserialization can share the same parse.
824+
///
825+
/// Every other rejection, such as a duplicate known field, comes from the typed parse
826+
/// of the raw line, so a line is loaded or skipped the same way whether or not it
827+
/// also needed the null check.
824828
pub(crate) fn deserialize_usage_line<T: DeserializeOwned>(line: &[u8]) -> Option<T> {
825-
if memmem::find(line, b"null").is_none() {
826-
return serde_json::from_slice(line).ok();
827-
}
828-
let root = serde_json::from_slice::<UniqueJsonValue>(line).ok()?.0;
829-
if has_unsupported_null_field_in_value(&root) {
830-
return None;
829+
if may_have_unsupported_null_field(line) {
830+
let root = serde_json::from_slice::<serde_json::Value>(line).ok()?;
831+
if has_unsupported_null_field_in_value(&root) {
832+
return None;
833+
}
831834
}
832-
serde_json::from_value(root).ok()
835+
serde_json::from_slice(line).ok()
833836
}
834837

835-
/// A JSON value parser that preserves typed Serde's rejection of duplicate members.
836-
/// Parsing through `serde_json::Value` normally keeps only the final duplicate, which could
837-
/// make a malformed transcript valid before the in-memory typed deserialization runs.
838-
struct UniqueJsonValue(serde_json::Value);
839-
840-
impl<'de> Deserialize<'de> for UniqueJsonValue {
841-
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
842-
where
843-
D: serde::Deserializer<'de>,
844-
{
845-
deserializer.deserialize_any(UniqueJsonValueVisitor)
846-
}
838+
/// Whether a `null` in `line` could be the value of a member that
839+
/// [`is_unsupported_nullable_field`] guards.
840+
///
841+
/// Claude Code writes always-null members such as `stop_sequence` and `container` on
842+
/// every assistant line, so the mere presence of `null` would send nearly every line
843+
/// through the value-tree check. A key holding an escape sequence counts as guarded,
844+
/// because its decoded name cannot be compared from the raw bytes; any other false
845+
/// positive only costs the slower validated parse.
846+
fn may_have_unsupported_null_field(line: &[u8]) -> bool {
847+
memmem::find_iter(line, b"null").any(|null_start| {
848+
let Some(before_value) = trim_json_whitespace_end(&line[..null_start]).strip_suffix(b":")
849+
else {
850+
return false;
851+
};
852+
let Some(before_closing_quote) = trim_json_whitespace_end(before_value).strip_suffix(b"\"")
853+
else {
854+
return false;
855+
};
856+
let key_start = memchr::memrchr(b'"', before_closing_quote).map_or(0, |index| index + 1);
857+
let key = &before_closing_quote[key_start..];
858+
key.contains(&b'\\') || is_unsupported_nullable_field(key)
859+
})
847860
}
848861

849-
struct UniqueJsonValueVisitor;
850-
851-
impl<'de> Visitor<'de> for UniqueJsonValueVisitor {
852-
type Value = UniqueJsonValue;
853-
854-
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
855-
formatter.write_str("a JSON value without duplicate object members")
856-
}
857-
858-
fn visit_bool<E>(self, value: bool) -> std::result::Result<Self::Value, E> {
859-
Ok(UniqueJsonValue(serde_json::Value::Bool(value)))
860-
}
861-
862-
fn visit_i64<E>(self, value: i64) -> std::result::Result<Self::Value, E> {
863-
Ok(UniqueJsonValue(serde_json::Value::Number(value.into())))
864-
}
865-
866-
fn visit_u64<E>(self, value: u64) -> std::result::Result<Self::Value, E> {
867-
Ok(UniqueJsonValue(serde_json::Value::Number(value.into())))
868-
}
869-
870-
fn visit_f64<E: serde::de::Error>(self, value: f64) -> std::result::Result<Self::Value, E> {
871-
serde_json::Number::from_f64(value)
872-
.map(serde_json::Value::Number)
873-
.map(UniqueJsonValue)
874-
.ok_or_else(|| E::custom("non-finite JSON number"))
875-
}
876-
877-
fn visit_str<E>(self, value: &str) -> std::result::Result<Self::Value, E> {
878-
Ok(UniqueJsonValue(serde_json::Value::String(value.to_owned())))
879-
}
880-
881-
fn visit_string<E>(self, value: String) -> std::result::Result<Self::Value, E> {
882-
Ok(UniqueJsonValue(serde_json::Value::String(value)))
883-
}
884-
885-
fn visit_none<E>(self) -> std::result::Result<Self::Value, E> {
886-
Ok(UniqueJsonValue(serde_json::Value::Null))
887-
}
888-
889-
fn visit_unit<E>(self) -> std::result::Result<Self::Value, E> {
890-
Ok(UniqueJsonValue(serde_json::Value::Null))
891-
}
892-
893-
fn visit_some<D>(self, deserializer: D) -> std::result::Result<Self::Value, D::Error>
894-
where
895-
D: serde::Deserializer<'de>,
896-
{
897-
UniqueJsonValue::deserialize(deserializer)
898-
}
899-
900-
fn visit_seq<A>(self, mut sequence: A) -> std::result::Result<Self::Value, A::Error>
901-
where
902-
A: SeqAccess<'de>,
903-
{
904-
let mut values = Vec::new();
905-
while let Some(value) = sequence.next_element::<UniqueJsonValue>()? {
906-
values.push(value.0);
907-
}
908-
Ok(UniqueJsonValue(serde_json::Value::Array(values)))
909-
}
910-
911-
fn visit_map<A>(self, mut object: A) -> std::result::Result<Self::Value, A::Error>
912-
where
913-
A: MapAccess<'de>,
914-
{
915-
let mut fields = serde_json::Map::new();
916-
while let Some(field) = object.next_key::<String>()? {
917-
if fields.contains_key(&field) {
918-
return Err(A::Error::custom(format!(
919-
"duplicate object member `{field}`"
920-
)));
921-
}
922-
let value = object.next_value::<UniqueJsonValue>()?;
923-
fields.insert(field, value.0);
924-
}
925-
Ok(UniqueJsonValue(serde_json::Value::Object(fields)))
926-
}
862+
fn trim_json_whitespace_end(bytes: &[u8]) -> &[u8] {
863+
let end = bytes
864+
.iter()
865+
.rposition(|byte| !matches!(byte, b' ' | b'\t' | b'\n' | b'\r'))
866+
.map_or(0, |index| index + 1);
867+
&bytes[..end]
927868
}
928869

929870
fn has_unsupported_null_field_in_value(root: &serde_json::Value) -> bool {
@@ -1045,8 +986,8 @@ mod tests {
1045986

1046987
use super::{
1047988
deserialize_usage_line, extract_session_parts, has_unsupported_null_field,
1048-
load_deduped_entries, paths::is_project_path_segment, push_deduped_entry, read_usage_file,
1049-
usage_files,
989+
load_deduped_entries, may_have_unsupported_null_field, paths::is_project_path_segment,
990+
push_deduped_entry, read_usage_file, usage_files,
1050991
};
1051992
use crate::{
1052993
LoadedEntry, PricingMap, TimestampMs, TokenUsageRaw, UsageEntry, UsageMessage,
@@ -1231,6 +1172,61 @@ mod tests {
12311172
));
12321173
}
12331174

1175+
#[test]
1176+
fn null_precheck_skips_only_unguarded_members() {
1177+
assert!(!may_have_unsupported_null_field(
1178+
br#"{"message":{"model":"m","stop_sequence":null,"container":null,"content":"null","usage":{"input_tokens":1,"iterations":null}}}"#
1179+
));
1180+
assert!(!may_have_unsupported_null_field(
1181+
br#"{"message":{"content":[null],"usage":{"input_tokens":1}}}"#
1182+
));
1183+
assert!(may_have_unsupported_null_field(
1184+
br#"{"message":{"model" :
1185+
null,"usage":{"input_tokens":1}}}"#
1186+
));
1187+
assert!(may_have_unsupported_null_field(
1188+
br#"{"message":{"model":null,"usage":{"input_tokens":1}}}"#
1189+
));
1190+
assert!(has_unsupported_null_field(
1191+
br#"{"message":{"model":null,"usage":{"input_tokens":1}}}"#
1192+
));
1193+
assert!(may_have_unsupported_null_field(
1194+
br#"{"message":{"content":"\"id\":null","usage":{"input_tokens":1}}}"#
1195+
));
1196+
}
1197+
1198+
/// Duplicate handling is the typed parse's alone, so it must not depend on whether
1199+
/// the line also carries an allowed or exempted `null`.
1200+
#[test]
1201+
fn handles_duplicate_members_the_same_with_or_without_nulls() {
1202+
for nulls in [
1203+
"",
1204+
r#","stop_sequence":null"#,
1205+
r#","iterations":[{"type":"message","model":null}]"#,
1206+
] {
1207+
let (message_nulls, usage_nulls) = if nulls.contains("iterations") {
1208+
("", nulls)
1209+
} else {
1210+
(nulls, "")
1211+
};
1212+
let duplicate_known = format!(
1213+
r#"{{"timestamp":"2026-09-12T04:38:42.296Z","message":{{"id":"msg_1","model":"first","model":"second"{message_nulls},"usage":{{"input_tokens":2,"output_tokens":1{usage_nulls}}}}}}}"#
1214+
);
1215+
let duplicate_unknown = format!(
1216+
r#"{{"timestamp":"2026-09-12T04:38:42.296Z","extra":1,"extra":2,"message":{{"id":"msg_1","model":"m"{message_nulls},"usage":{{"input_tokens":2,"output_tokens":1{usage_nulls}}}}}}}"#
1217+
);
1218+
1219+
assert!(
1220+
deserialize_usage_line::<UsageEntry>(duplicate_known.as_bytes()).is_none(),
1221+
"{duplicate_known}"
1222+
);
1223+
assert!(
1224+
deserialize_usage_line::<UsageEntry>(duplicate_unknown.as_bytes()).is_some(),
1225+
"{duplicate_unknown}"
1226+
);
1227+
}
1228+
}
1229+
12341230
#[test]
12351231
fn rejects_duplicate_members_before_typed_deserialization() {
12361232
assert!(deserialize_usage_line::<UsageEntry>(

0 commit comments

Comments
 (0)