Skip to main content

reth_trie/
stats.rs

1use reth_primitives_traits::FastInstant as Instant;
2use std::time::Duration;
3
4/// Trie stats.
5#[derive(Clone, Copy, Debug)]
6pub struct TrieStats {
7    duration: Duration,
8    branches_added: u64,
9    leaves_added: u64,
10}
11
12impl TrieStats {
13    /// Duration for root calculation.
14    pub const fn duration(&self) -> Duration {
15        self.duration
16    }
17
18    /// Number of leaves added to the hash builder during the calculation.
19    pub const fn leaves_added(&self) -> u64 {
20        self.leaves_added
21    }
22
23    /// Number of branches added to the hash builder during the calculation.
24    pub const fn branches_added(&self) -> u64 {
25        self.branches_added
26    }
27}
28
29/// Trie metrics tracker.
30#[derive(Debug)]
31pub struct TrieTracker {
32    started_at: Instant,
33    branches_added: u64,
34    leaves_added: u64,
35}
36
37impl Default for TrieTracker {
38    fn default() -> Self {
39        Self { started_at: Instant::now(), branches_added: 0, leaves_added: 0 }
40    }
41}
42
43impl TrieTracker {
44    /// Increment the number of branches added to the hash builder during the calculation.
45    pub const fn inc_branch(&mut self) {
46        self.branches_added += 1;
47    }
48
49    /// Increment the number of leaves added to the hash builder during the calculation.
50    pub const fn inc_leaf(&mut self) {
51        self.leaves_added += 1;
52    }
53
54    /// Called when root calculation is finished to return trie statistics.
55    pub fn finish(self) -> TrieStats {
56        TrieStats {
57            duration: self.started_at.elapsed(),
58            branches_added: self.branches_added,
59            leaves_added: self.leaves_added,
60        }
61    }
62}