Skip to content

Commit

Permalink
Handle NaNs in diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
Brezak committed Mar 21, 2024
1 parent 7b842e3 commit 27a9b30
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions crates/bevy_diagnostic/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl std::fmt::Display for DiagnosticPath {
}

/// A single measurement of a [`Diagnostic`].
#[derive(Debug)]
#[derive(Debug, Copy, Clone)]
pub struct DiagnosticMeasurement {
pub time: Instant,
pub value: f64,
Expand All @@ -130,7 +130,9 @@ pub struct Diagnostic {
impl Diagnostic {
/// Add a new value as a [`DiagnosticMeasurement`].
pub fn add_measurement(&mut self, measurement: DiagnosticMeasurement) {
if let Some(previous) = self.measurement() {
if measurement.value.is_nan() {
// Do nothing about the ema.
} else if let Some(previous) = self.measurement() {
let delta = (measurement.time - previous.time).as_secs_f64();
let alpha = (delta / self.ema_smoothing_factor).clamp(0.0, 1.0);
self.ema += alpha * (measurement.value - self.ema);
Expand All @@ -139,16 +141,24 @@ impl Diagnostic {
}

if self.max_history_length > 1 {
if self.history.len() == self.max_history_length {
if self.history.len() >= self.max_history_length {
if let Some(removed_diagnostic) = self.history.pop_front() {
self.sum -= removed_diagnostic.value;
if !removed_diagnostic.value.is_nan() {
self.sum -= removed_diagnostic.value;
}
}
}

self.sum += measurement.value;
if measurement.value.is_nan() {
self.sum += measurement.value;
}
} else {
self.history.clear();
self.sum = measurement.value;
if measurement.value.is_nan() {
self.sum = 0.0;
} else {
self.sum = measurement.value;
}
}

self.history.push_back(measurement);
Expand All @@ -172,8 +182,13 @@ impl Diagnostic {
#[must_use]
pub fn with_max_history_length(mut self, max_history_length: usize) -> Self {
self.max_history_length = max_history_length;
self.history.reserve(self.max_history_length);
self.history.shrink_to(self.max_history_length);

// reserve/reserve_exact reserve space for n *additional* elements.
let expected_capacity = self
.max_history_length
.saturating_sub(self.history.capacity());
self.history.reserve_exact(expected_capacity);
self.history.shrink_to(expected_capacity);
self
}

Expand Down

0 comments on commit 27a9b30

Please sign in to comment.