Skip to content

Commit

Permalink
Use StableHashMap instead of HashMap + BTreeSet for diagnostics
Browse files Browse the repository at this point in the history
This is a significant reduction in the release mode compile times of
bevy_diagnostics

```
Benchmark #1: touch crates/bevy_diagnostic/src/lib.rs && cargo build --release -p bevy_diagnostic -j1
  Time (mean ± σ):      3.645 s ±  0.009 s    [User: 3.551 s, System: 0.094 s]
  Range (min … max):    3.632 s …  3.658 s    20 runs
```

```
Benchmark #1: touch crates/bevy_diagnostic/src/lib.rs && cargo build --release -p bevy_diagnostic -j1
  Time (mean ± σ):      2.938 s ±  0.012 s    [User: 2.850 s, System: 0.090 s]
  Range (min … max):    2.919 s …  2.969 s    20 runs
```
  • Loading branch information
bjorn3 committed Jan 12, 2021
1 parent fe9c1f4 commit 9ecd533
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
16 changes: 5 additions & 11 deletions crates/bevy_diagnostic/src/diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy_utils::{Duration, HashMap, Instant, Uuid};
use std::collections::{BTreeSet, VecDeque};
use bevy_utils::{Duration, Instant, StableHashMap, Uuid};
use std::collections::VecDeque;

/// Unique identifier for a [Diagnostic]
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, PartialOrd, Ord)]
Expand Down Expand Up @@ -101,13 +101,13 @@ impl Diagnostic {
/// A collection of [Diagnostic]s
#[derive(Debug, Default)]
pub struct Diagnostics {
diagnostics: HashMap<DiagnosticId, Diagnostic>,
ordered_diagnostics: BTreeSet<DiagnosticId>,
// This uses a [`StableHashMap`] to ensure that the iteration order is deterministic between
// runs when all diagnostics are inserted in the same order.
diagnostics: StableHashMap<DiagnosticId, Diagnostic>,
}

impl Diagnostics {
pub fn add(&mut self, diagnostic: Diagnostic) {
self.ordered_diagnostics.insert(diagnostic.id);
self.diagnostics.insert(diagnostic.id, diagnostic);
}

Expand All @@ -134,10 +134,4 @@ impl Diagnostics {
pub fn iter(&self) -> impl Iterator<Item = &Diagnostic> {
self.diagnostics.values()
}

pub fn ordered_iter(&self) -> impl Iterator<Item = &Diagnostic> {
self.ordered_diagnostics
.iter()
.filter_map(move |k| self.diagnostics.get(k))
}
}
4 changes: 2 additions & 2 deletions crates/bevy_diagnostic/src/log_diagnostics_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl LogDiagnosticsPlugin {
Self::log_diagnostic(diagnostic);
}
} else {
for diagnostic in diagnostics.ordered_iter() {
for diagnostic in diagnostics.iter() {
Self::log_diagnostic(diagnostic);
}
}
Expand All @@ -96,7 +96,7 @@ impl LogDiagnosticsPlugin {
debug!("{:#?}\n", diagnostic);
}
} else {
for diagnostic in diagnostics.ordered_iter() {
for diagnostic in diagnostics.iter() {
debug!("{:#?}\n", diagnostic);
}
}
Expand Down

0 comments on commit 9ecd533

Please sign in to comment.