|
1 | 1 | use std::{ |
2 | | - collections::{hash_map::DefaultHasher, BTreeMap, HashSet}, |
| 2 | + collections::BTreeMap, |
3 | 3 | env, fs, |
4 | 4 | hash::{Hash, Hasher}, |
5 | 5 | path::{Path, PathBuf}, |
6 | 6 | sync::Mutex, |
7 | 7 | thread, |
8 | 8 | }; |
9 | 9 |
|
| 10 | +use compact_str::CompactString; |
10 | 11 | use jiff::tz::TimeZone as JiffTimeZone; |
| 12 | +use rustc_hash::FxHasher; |
11 | 13 | use serde_json::{json, Value}; |
12 | 14 |
|
13 | 15 | use crate::{ |
14 | 16 | cli::{AgentCommandArgs, AgentReportKind, CodexSpeed, SharedArgs, WeekDay}, |
15 | | - color, format_currency, format_date_tz, format_models_multiline, format_number, json_float, |
| 17 | + color, |
| 18 | + fast::FxHashSet, |
| 19 | + format_currency, format_date_tz, format_models_multiline, format_number, json_float, |
16 | 20 | json_value_u64, log_level, parse_ts_timestamp, parse_tz, print_box_title, print_json_or_jq, |
17 | 21 | wants_json, week_start, Align, CodexGroup, CodexModelUsage, CodexTokenUsageEvent, Color, |
18 | 22 | PricingMap, Result, SimpleTable, |
19 | 23 | }; |
20 | 24 |
|
21 | | -type CodexEventKey = (String, Option<String>, u64, u64, u64, u64, u64); |
22 | | -type CodexDedupeShards = [Mutex<HashSet<CodexEventKey>>]; |
| 25 | +type CodexEventKey = ( |
| 26 | + CompactString, |
| 27 | + Option<CompactString>, |
| 28 | + u64, |
| 29 | + u64, |
| 30 | + u64, |
| 31 | + u64, |
| 32 | + u64, |
| 33 | +); |
| 34 | +type CodexDedupeShards = [Mutex<FxHashSet<CodexEventKey>>]; |
23 | 35 |
|
24 | 36 | pub(crate) fn run(args: AgentCommandArgs) -> Result<()> { |
25 | 37 | let shared = args.shared; |
@@ -293,26 +305,26 @@ fn accumulate_codex_event_into_group( |
293 | 305 | model_usage.is_fallback |= event.is_fallback_model; |
294 | 306 | } |
295 | 307 |
|
296 | | -fn create_dedupe_shards() -> Vec<Mutex<HashSet<CodexEventKey>>> { |
| 308 | +fn create_dedupe_shards() -> Vec<Mutex<FxHashSet<CodexEventKey>>> { |
297 | 309 | let shard_count = thread::available_parallelism() |
298 | 310 | .map(usize::from) |
299 | 311 | .unwrap_or(1); |
300 | 312 | (0..shard_count.max(1)) |
301 | | - .map(|_| Mutex::new(HashSet::new())) |
| 313 | + .map(|_| Mutex::new(FxHashSet::default())) |
302 | 314 | .collect() |
303 | 315 | } |
304 | 316 |
|
305 | 317 | fn insert_event_key(event: &CodexTokenUsageEvent, seen: &CodexDedupeShards) -> bool { |
306 | 318 | let key = ( |
307 | | - event.timestamp.clone(), |
308 | | - event.model.clone(), |
| 319 | + CompactString::new(&event.timestamp), |
| 320 | + event.model.as_deref().map(CompactString::new), |
309 | 321 | event.input_tokens, |
310 | 322 | event.cached_input_tokens, |
311 | 323 | event.output_tokens, |
312 | 324 | event.reasoning_output_tokens, |
313 | 325 | event.total_tokens, |
314 | 326 | ); |
315 | | - let mut hasher = DefaultHasher::new(); |
| 327 | + let mut hasher = FxHasher::default(); |
316 | 328 | key.hash(&mut hasher); |
317 | 329 | let shard_index = hasher.finish() as usize % seen.len(); |
318 | 330 | seen[shard_index].lock().unwrap().insert(key) |
|
0 commit comments