chore: justify the cast_precision_loss allows with reasons (#1474 slice)

This commit is contained in:
damocles 2026-06-06 13:06:57 +02:00
commit c819eab947
3 changed files with 40 additions and 10 deletions

View file

@ -367,9 +367,15 @@ fn fill_buckets(
let avg = if sorted.is_empty() { let avg = if sorted.is_empty() {
0.0 0.0
} else { } else {
#[allow(clippy::cast_precision_loss)] #[allow(
clippy::cast_precision_loss,
reason = "stat magnitudes (counts, token + duration sums) stay well under f64's 2^53 exact-integer range, so this averaging cast loses no precision in practice"
)]
let sum_f = sorted.iter().sum::<i64>() as f64; let sum_f = sorted.iter().sum::<i64>() as f64;
#[allow(clippy::cast_precision_loss)] #[allow(
clippy::cast_precision_loss,
reason = "stat magnitudes (counts, token + duration sums) stay well under f64's 2^53 exact-integer range, so this averaging cast loses no precision in practice"
)]
let len_f = sorted.len() as f64; let len_f = sorted.len() as f64;
sum_f / len_f sum_f / len_f
}; };
@ -378,9 +384,15 @@ fn fill_buckets(
let avg_ctx = if acc.turn_count == 0 { let avg_ctx = if acc.turn_count == 0 {
0.0 0.0
} else { } else {
#[allow(clippy::cast_precision_loss)] #[allow(
clippy::cast_precision_loss,
reason = "stat magnitudes (counts, token + duration sums) stay well under f64's 2^53 exact-integer range, so this averaging cast loses no precision in practice"
)]
let sum_f = acc.ctx_sum as f64; let sum_f = acc.ctx_sum as f64;
#[allow(clippy::cast_precision_loss)] #[allow(
clippy::cast_precision_loss,
reason = "stat magnitudes (counts, token + duration sums) stay well under f64's 2^53 exact-integer range, so this averaging cast loses no precision in practice"
)]
let cnt_f = acc.turn_count as f64; let cnt_f = acc.turn_count as f64;
sum_f / cnt_f sum_f / cnt_f
}; };
@ -427,9 +439,15 @@ fn summarize_durations(all: &mut [i64]) -> DurationSummary {
return DurationSummary::default(); return DurationSummary::default();
} }
all.sort_unstable(); all.sort_unstable();
#[allow(clippy::cast_precision_loss)] #[allow(
clippy::cast_precision_loss,
reason = "stat magnitudes (counts, token + duration sums) stay well under f64's 2^53 exact-integer range, so this averaging cast loses no precision in practice"
)]
let sum_f = all.iter().sum::<i64>() as f64; let sum_f = all.iter().sum::<i64>() as f64;
#[allow(clippy::cast_precision_loss)] #[allow(
clippy::cast_precision_loss,
reason = "stat magnitudes (counts, token + duration sums) stay well under f64's 2^53 exact-integer range, so this averaging cast loses no precision in practice"
)]
let len_f = all.len() as f64; let len_f = all.len() as f64;
DurationSummary { DurationSummary {
avg_ms: sum_f / len_f, avg_ms: sum_f / len_f,

View file

@ -141,16 +141,25 @@ pub async fn gather() -> Vec<ContainerResource> {
let t0: Vec<Option<u64>> = candidates.iter().map(|(_, d)| read_usage_usec(d)).collect(); let t0: Vec<Option<u64>> = candidates.iter().map(|(_, d)| read_usage_usec(d)).collect();
sleep(CPU_SAMPLE).await; sleep(CPU_SAMPLE).await;
#[allow(clippy::cast_precision_loss)] #[allow(
clippy::cast_precision_loss,
reason = "CPU-utilisation math; the operands (core count, microsecond sample interval, cgroup time delta) stay well under f64's 2^53 exact-integer range, so no precision is lost"
)]
let nproc = host_nproc() as f64; let nproc = host_nproc() as f64;
#[allow(clippy::cast_precision_loss)] #[allow(
clippy::cast_precision_loss,
reason = "CPU-utilisation math; the operands (core count, microsecond sample interval, cgroup time delta) stay well under f64's 2^53 exact-integer range, so no precision is lost"
)]
let interval_usec = CPU_SAMPLE.as_micros() as f64; let interval_usec = CPU_SAMPLE.as_micros() as f64;
let mut out: Vec<ContainerResource> = Vec::with_capacity(candidates.len()); let mut out: Vec<ContainerResource> = Vec::with_capacity(candidates.len());
for (i, (name, dir)) in candidates.iter().enumerate() { for (i, (name, dir)) in candidates.iter().enumerate() {
let cpu_pct = match (t0[i], read_usage_usec(dir)) { let cpu_pct = match (t0[i], read_usage_usec(dir)) {
(Some(a), Some(b)) => { (Some(a), Some(b)) => {
#[allow(clippy::cast_precision_loss)] #[allow(
clippy::cast_precision_loss,
reason = "CPU-utilisation math; the operands (core count, microsecond sample interval, cgroup time delta) stay well under f64's 2^53 exact-integer range, so no precision is lost"
)]
let delta = b.saturating_sub(a) as f64; let delta = b.saturating_sub(a) as f64;
(delta / (interval_usec * nproc)) * 100.0 (delta / (interval_usec * nproc)) * 100.0
} }

View file

@ -205,7 +205,10 @@ fn read_agent(path: &Path, from: i64) -> rusqlite::Result<AgentAgg> {
agg.cache_read = agg.cache_read.saturating_add(cache_read); agg.cache_read = agg.cache_read.saturating_add(cache_read);
agg.cache_creation = agg.cache_creation.saturating_add(cache_creation); agg.cache_creation = agg.cache_creation.saturating_add(cache_creation);
let p = model_prices(&model); let p = model_prices(&model);
#[allow(clippy::cast_precision_loss)] #[allow(
clippy::cast_precision_loss,
reason = "token counts stay well under f64's 2^53 exact-integer range, so this cost computation loses no precision"
)]
{ {
agg.cost += (input as f64 * p.input agg.cost += (input as f64 * p.input
+ output as f64 * p.output + output as f64 * p.output