diff --git a/src/systems/input.rs b/src/systems/input.rs index 56bf96b..fd4f655 100644 --- a/src/systems/input.rs +++ b/src/systems/input.rs @@ -297,14 +297,15 @@ pub fn input_system( simple_key_events.push(SimpleKeyEvent::KeyUp(key)); } } - Event::Window { win_event, .. } => match win_event { - WindowEvent::Resized(w, h) => { - tracing::info!("Window resized to {}x{}", w, h); + Event::Window { win_event, .. } => { + if let WindowEvent::Resized(w, h) = win_event { + tracing::info!(width = w, height = h, event = ?win_event, "Window Resized"); } - _ => {} - }, + } + // Despite disabling this event, it's still received, so we ignore it explicitly. + Event::RenderTargetsReset { .. } => {} _ => { - tracing::warn!("Unhandled event, consider disabling: {:?}", event); + tracing::warn!(event = ?event, "Unhandled Event"); } } } diff --git a/src/systems/profiling.rs b/src/systems/profiling.rs index 5a60a41..2b8d707 100644 --- a/src/systems/profiling.rs +++ b/src/systems/profiling.rs @@ -102,28 +102,36 @@ impl SystemTimings { .expect("SystemId not found in pre-populated map - this is a bug"); let queue_guard = queue.lock(); - if queue_guard.is_empty() { - // Return zero timing for systems that haven't submitted any data - stats.insert(id, (Duration::ZERO, Duration::ZERO)); - continue; + // Welford's algorithm for a single-pass mean and variance calculation. + + let mut sample_count = 0.0; + let mut running_mean = 0.0; + let mut sum_squared_diff = 0.0; + + for duration in queue_guard.iter() { + let duration_secs = duration.as_secs_f64(); + sample_count += 1.0; + let diff_from_mean = duration_secs - running_mean; + running_mean += diff_from_mean / sample_count; + let diff_from_new_mean = duration_secs - running_mean; + sum_squared_diff += diff_from_mean * diff_from_new_mean; } - let durations: Vec = queue_guard.iter().map(|d| d.as_secs_f64() * 1000.0).collect(); - let count = durations.len() as f64; - - let sum: f64 = durations.iter().sum(); - let mean = sum / count; - - let variance = durations.iter().map(|x| (x - mean).powi(2)).sum::() / (count - 1.0).max(1.0); - let std_dev = variance.sqrt(); - - stats.insert( - id, + let (average, standard_deviation) = if sample_count > 0.0 { + let variance = if sample_count > 1.0 { + sum_squared_diff / (sample_count - 1.0) + } else { + 0.0 + }; ( - Duration::from_secs_f64(mean / 1000.0), - Duration::from_secs_f64(std_dev / 1000.0), - ), - ); + Duration::from_secs_f64(running_mean), + Duration::from_secs_f64(variance.sqrt()), + ) + } else { + (Duration::ZERO, Duration::ZERO) + }; + + stats.insert(id, (average, standard_deviation)); } stats