Skip to content

Commit 251137e

Browse files
committed
perf(ccusage): skip redundant cost reassignment
Keep costUSD assignment in the parallel file parsing path for display and auto mode. The post-dedupe pricing pass now only runs when calculate mode is requested or auto mode has entries missing costUSD.
1 parent 936fdb7 commit 251137e

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

‎crates/ccusage/src/main.rs‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ async fn load_entries(
648648
let mut entries = files
649649
.par_iter()
650650
.enumerate()
651-
.flat_map_iter(|(file_index, file)| read_usage_file(file, tz, file_index))
651+
.flat_map_iter(|(file_index, file)| read_usage_file(file, tz, shared.mode, file_index))
652652
.collect::<Vec<_>>();
653653
entries.sort_by_key(|entry| (entry.file_index, entry.line_number));
654654

@@ -678,20 +678,24 @@ async fn assign_costs(entries: &mut [LoadedEntry], shared: &SharedArgs) {
678678
|| (shared.mode == CostMode::Auto
679679
&& entries.iter().all(|entry| entry.data.cost_usd.is_some()))
680680
{
681-
for entry in entries {
682-
entry.cost = entry.data.cost_usd.unwrap_or(0.0);
683-
}
684681
return;
685682
}
686683

687684
let pricing =
688685
PricingRegistry::load(shared.offline && !shared.no_offline, wants_json(shared)).await;
689686
for entry in entries {
690-
entry.cost = calculate_cost(&entry.data, shared.mode, &pricing);
687+
if shared.mode == CostMode::Calculate || entry.data.cost_usd.is_none() {
688+
entry.cost = calculate_cost(&entry.data, shared.mode, &pricing);
689+
}
691690
}
692691
}
693692

694-
fn read_usage_file_with(path: &Path, tz: Option<Tz>, file_index: usize) -> Vec<LoadedEntry> {
693+
fn read_usage_file_with(
694+
path: &Path,
695+
tz: Option<Tz>,
696+
mode: CostMode,
697+
file_index: usize,
698+
) -> Vec<LoadedEntry> {
695699
let file = match File::open(path) {
696700
Ok(file) => file,
697701
Err(_) => return Vec::new(),
@@ -719,6 +723,10 @@ fn read_usage_file_with(path: &Path, tz: Option<Tz>, file_index: usize) -> Vec<L
719723
.ok()?
720724
.with_timezone(&Utc);
721725
let date = format_date_tz(timestamp, tz);
726+
let cost = match mode {
727+
CostMode::Auto | CostMode::Display => data.cost_usd.unwrap_or(0.0),
728+
CostMode::Calculate => 0.0,
729+
};
722730
let model = data.message.model.as_ref().and_then(|model| {
723731
if model == "<synthetic>" {
724732
None
@@ -735,7 +743,7 @@ fn read_usage_file_with(path: &Path, tz: Option<Tz>, file_index: usize) -> Vec<L
735743
project: project.clone(),
736744
session_id: session_id.clone(),
737745
project_path: project_path.clone(),
738-
cost: 0.0,
746+
cost,
739747
model,
740748
file_index,
741749
line_number,
@@ -906,8 +914,13 @@ fn is_semver_prefix(value: &str) -> bool {
906914
&& patch.chars().next().is_some_and(|ch| ch.is_ascii_digit())
907915
}
908916

909-
fn read_usage_file(path: &Path, tz: Option<Tz>, file_index: usize) -> Vec<LoadedEntry> {
910-
read_usage_file_with(path, tz, file_index)
917+
fn read_usage_file(
918+
path: &Path,
919+
tz: Option<Tz>,
920+
mode: CostMode,
921+
file_index: usize,
922+
) -> Vec<LoadedEntry> {
923+
read_usage_file_with(path, tz, mode, file_index)
911924
}
912925

913926
fn claude_paths() -> Result<Vec<PathBuf>> {

0 commit comments

Comments
 (0)