Skip to main content

rapier2d/counters/
stages_counters.rs

1use crate::counters::Timer;
2use core::fmt::{Display, Formatter, Result};
3
4/// Performance counters related to each stage of the time step.
5#[derive(Default, Clone, Copy, Debug)]
6pub struct StagesCounters {
7    /// Time spent for updating the kinematic and dynamics of every body.
8    pub update_time: Timer,
9    /// Total time spent for the collision detection (including both broad- and narrow- phases).
10    pub collision_detection_time: Timer,
11    /// Time spent for the computation of collision island and body activation/deactivation (sleeping).
12    pub island_construction_time: Timer,
13    /// Time spent maintaining the persistent solver-facing structures (solver
14    /// contact graph reconciliation for the pairs that changed this step,
15    /// frontier solver-body slots, force-event pair list). O(changed pairs) —
16    /// the solvers consume the persistent buckets directly, so there is no
17    /// per-step constraints-collection pass anymore.
18    pub island_constraints_collection_time: Timer,
19    /// Total time spent for the constraints resolution and position update.t
20    pub solver_time: Timer,
21    /// Total time spent for CCD and CCD resolution.
22    pub ccd_time: Timer,
23    /// Total time spent propagating user changes.
24    pub user_changes: Timer,
25}
26
27impl StagesCounters {
28    /// Create a new counter initialized to zero.
29    pub fn new() -> Self {
30        StagesCounters {
31            update_time: Timer::new(),
32            collision_detection_time: Timer::new(),
33            island_construction_time: Timer::new(),
34            island_constraints_collection_time: Timer::new(),
35            solver_time: Timer::new(),
36            ccd_time: Timer::new(),
37            user_changes: Timer::new(),
38        }
39    }
40
41    /// Resets all the counters and timers.
42    pub fn reset(&mut self) {
43        self.update_time.reset();
44        self.collision_detection_time.reset();
45        self.island_construction_time.reset();
46        self.island_constraints_collection_time.reset();
47        self.solver_time.reset();
48        self.ccd_time.reset();
49        self.user_changes.reset();
50    }
51}
52
53impl Display for StagesCounters {
54    fn fmt(&self, f: &mut Formatter) -> Result {
55        writeln!(f, "Update time: {}", self.update_time)?;
56        writeln!(
57            f,
58            "Collision detection time: {}",
59            self.collision_detection_time
60        )?;
61        writeln!(
62            f,
63            "Island construction time: {}",
64            self.island_construction_time
65        )?;
66        writeln!(
67            f,
68            "Constraints maintenance time: {}",
69            self.island_constraints_collection_time
70        )?;
71        writeln!(f, "Solver time: {}", self.solver_time)?;
72        writeln!(f, "CCD time: {}", self.ccd_time)?;
73        writeln!(f, "User changes time: {}", self.user_changes)
74    }
75}