reth_trie/
stats.rs

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