Repository navigation
Commit a06f4bc
authored
perf(claude): halve
* 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.ccusage claude report time by skipping redundant JSON work (#2058)1 parent de43952 commit a06f4bc
4 files changed
Lines changed: 189 additions & 143 deletions
File tree
- rust
- adapters/claude/src
- crates
- ccusage-core/src
- ccusage/tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
227 | 227 | | |
228 | 228 | | |
229 | 229 | | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
252 | 240 | | |
253 | 241 | | |
254 | 242 | | |
| |||
258 | 246 | | |
259 | 247 | | |
260 | 248 | | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
261 | 263 | | |
262 | 264 | | |
263 | 265 | | |
| |||
352 | 354 | | |
353 | 355 | | |
354 | 356 | | |
355 | | - | |
| 357 | + | |
356 | 358 | | |
357 | 359 | | |
358 | | - | |
359 | 360 | | |
360 | 361 | | |
361 | 362 | | |
| |||
902 | 903 | | |
903 | 904 | | |
904 | 905 | | |
905 | | - | |
906 | | - | |
| 906 | + | |
| 907 | + | |
907 | 908 | | |
908 | | - | |
909 | | - | |
| 909 | + | |
910 | 910 | | |
911 | 911 | | |
912 | 912 | | |
913 | 913 | | |
914 | 914 | | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
915 | 926 | | |
916 | 927 | | |
917 | 928 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
| 18 | + | |
22 | 19 | | |
23 | 20 | | |
24 | 21 | | |
| |||
815 | 812 | | |
816 | 813 | | |
817 | 814 | | |
818 | | - | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
819 | 821 | | |
820 | 822 | | |
821 | 823 | | |
822 | | - | |
823 | | - | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
824 | 828 | | |
825 | | - | |
826 | | - | |
827 | | - | |
828 | | - | |
829 | | - | |
830 | | - | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
831 | 834 | | |
832 | | - | |
| 835 | + | |
833 | 836 | | |
834 | 837 | | |
835 | | - | |
836 | | - | |
837 | | - | |
838 | | - | |
839 | | - | |
840 | | - | |
841 | | - | |
842 | | - | |
843 | | - | |
844 | | - | |
845 | | - | |
846 | | - | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
847 | 860 | | |
848 | 861 | | |
849 | | - | |
850 | | - | |
851 | | - | |
852 | | - | |
853 | | - | |
854 | | - | |
855 | | - | |
856 | | - | |
857 | | - | |
858 | | - | |
859 | | - | |
860 | | - | |
861 | | - | |
862 | | - | |
863 | | - | |
864 | | - | |
865 | | - | |
866 | | - | |
867 | | - | |
868 | | - | |
869 | | - | |
870 | | - | |
871 | | - | |
872 | | - | |
873 | | - | |
874 | | - | |
875 | | - | |
876 | | - | |
877 | | - | |
878 | | - | |
879 | | - | |
880 | | - | |
881 | | - | |
882 | | - | |
883 | | - | |
884 | | - | |
885 | | - | |
886 | | - | |
887 | | - | |
888 | | - | |
889 | | - | |
890 | | - | |
891 | | - | |
892 | | - | |
893 | | - | |
894 | | - | |
895 | | - | |
896 | | - | |
897 | | - | |
898 | | - | |
899 | | - | |
900 | | - | |
901 | | - | |
902 | | - | |
903 | | - | |
904 | | - | |
905 | | - | |
906 | | - | |
907 | | - | |
908 | | - | |
909 | | - | |
910 | | - | |
911 | | - | |
912 | | - | |
913 | | - | |
914 | | - | |
915 | | - | |
916 | | - | |
917 | | - | |
918 | | - | |
919 | | - | |
920 | | - | |
921 | | - | |
922 | | - | |
923 | | - | |
924 | | - | |
925 | | - | |
926 | | - | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
927 | 868 | | |
928 | 869 | | |
929 | 870 | | |
| |||
1045 | 986 | | |
1046 | 987 | | |
1047 | 988 | | |
1048 | | - | |
1049 | | - | |
| 989 | + | |
| 990 | + | |
1050 | 991 | | |
1051 | 992 | | |
1052 | 993 | | |
| |||
1231 | 1172 | | |
1232 | 1173 | | |
1233 | 1174 | | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + | |
| 1202 | + | |
| 1203 | + | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
1234 | 1230 | | |
1235 | 1231 | | |
1236 | 1232 | | |
| |||
0 commit comments