Skip to main content

trueno/brick/patterns/
graph_reuse_counter.rs

1//! LCP-08: Graph Reuse Counter
2
3/// Counter for tracking graph reuse in inference optimization.
4///
5/// Tracks how many times a computation graph has been reused,
6/// enabling optimization decisions like caching or recompilation.
7#[derive(Debug, Clone, Default)]
8pub struct GraphReuseCounter {
9    /// Number of times this graph has been executed
10    reuse_count: u64,
11    /// Threshold for considering graph "hot"
12    hot_threshold: u64,
13    /// Whether to enable caching
14    cache_enabled: bool,
15}
16
17impl GraphReuseCounter {
18    /// Create a new counter with hot threshold.
19    pub fn new(hot_threshold: u64) -> Self {
20        Self { reuse_count: 0, hot_threshold, cache_enabled: false }
21    }
22
23    /// Record a graph execution.
24    pub fn record_use(&mut self) {
25        self.reuse_count += 1;
26        if self.reuse_count >= self.hot_threshold {
27            self.cache_enabled = true;
28        }
29    }
30
31    /// Check if graph is considered "hot" (heavily reused).
32    #[must_use]
33    pub fn is_hot(&self) -> bool {
34        self.reuse_count >= self.hot_threshold
35    }
36
37    /// Check if caching should be enabled.
38    #[must_use]
39    pub fn should_cache(&self) -> bool {
40        self.cache_enabled
41    }
42
43    /// Get the current reuse count.
44    #[must_use]
45    pub fn count(&self) -> u64 {
46        self.reuse_count
47    }
48
49    /// Reset the counter.
50    pub fn reset(&mut self) {
51        self.reuse_count = 0;
52        self.cache_enabled = false;
53    }
54}