From 63cab6e631afa81c580e2a163c2d19c83eac99c6 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sun, 18 May 2025 08:36:51 +0300 Subject: [PATCH] daemon: clean up weighted average calculation --- src/daemon.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/daemon.rs b/src/daemon.rs index 630bcbe..3b91bb5 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -126,8 +126,14 @@ fn compute_new(params: &IntervalParams, system_history: &SystemHistory) -> u64 { let blended_interval = if let Some(cached) = system_history.last_computed_interval { // Use a weighted average: 70% previous value, 30% new value // This smooths out drastic changes in polling frequency + const PREVIOUS_VALUE_WEIGHT: u128 = 7; // 70% + const NEW_VALUE_WEIGHT: u128 = 3; // 30% + const TOTAL_WEIGHT: u128 = PREVIOUS_VALUE_WEIGHT + NEW_VALUE_WEIGHT; // 10 + // XXX: Use u128 arithmetic to avoid overflow with large interval values - let result = (cached as u128 * 7 + new_interval as u128 * 3) / 10; + let result = (cached as u128 * PREVIOUS_VALUE_WEIGHT + + new_interval as u128 * NEW_VALUE_WEIGHT) + / TOTAL_WEIGHT; result as u64 } else {