memscope_rs/estimation/
size_estimator.rs

1use std::collections::HashMap;
2
3pub trait SizeEstimator: Send + Sync {
4    fn estimate_size(&self, type_name: &str) -> Option<usize>;
5    fn learn_from_real_allocation(&mut self, type_name: &str, actual_size: usize);
6}
7
8#[derive(Debug, Clone)]
9pub struct LearnedSize {
10    pub average: f64,
11    pub count: usize,
12    pub confidence: f64,
13}
14
15pub struct SmartSizeEstimator {
16    known_sizes: HashMap<String, usize>,
17    learned_sizes: HashMap<String, LearnedSize>,
18    pointer_size: usize,
19}
20
21impl SmartSizeEstimator {
22    pub fn new() -> Self {
23        let mut estimator = Self {
24            known_sizes: HashMap::new(),
25            learned_sizes: HashMap::new(),
26            pointer_size: std::mem::size_of::<*const u8>(),
27        };
28        estimator.initialize_known_types();
29        estimator
30    }
31
32    fn initialize_known_types(&mut self) {
33        let ptr_size = self.pointer_size;
34        let basics = [
35            ("i8", 1),
36            ("i16", 2),
37            ("i32", 4),
38            ("i64", 8),
39            ("u8", 1),
40            ("u16", 2),
41            ("u32", 4),
42            ("u64", 8),
43            ("f32", 4),
44            ("f64", 8),
45            ("bool", 1),
46            ("char", 4),
47            ("usize", ptr_size),
48            ("isize", ptr_size),
49            ("String", ptr_size * 3),
50            ("AtomicBool", 1),
51        ];
52
53        for (name, size) in &basics {
54            self.known_sizes.insert(name.to_string(), *size);
55        }
56    }
57
58    fn estimate_container_size(&self, type_name: &str) -> Option<usize> {
59        let ptr_size = self.pointer_size;
60
61        if type_name.starts_with("Vec<") {
62            return Some(ptr_size * 3);
63        }
64        if type_name.starts_with("HashMap<") {
65            return Some(48);
66        }
67        if type_name.starts_with("Box<") {
68            return Some(ptr_size);
69        }
70        if type_name.starts_with("Arc<") {
71            return Some(ptr_size);
72        }
73        if type_name.starts_with("Option<") {
74            return Some(8);
75        }
76
77        None
78    }
79
80    fn heuristic_estimate(&self, type_name: &str) -> usize {
81        let complexity = type_name.len()
82            + type_name.matches('<').count() * 4
83            + type_name.matches(',').count() * 2;
84
85        match complexity {
86            0..=8 => self.pointer_size,
87            9..=16 => self.pointer_size * 2,
88            17..=32 => self.pointer_size * 4,
89            _ => self.pointer_size * 8,
90        }
91    }
92}
93
94impl SizeEstimator for SmartSizeEstimator {
95    fn estimate_size(&self, type_name: &str) -> Option<usize> {
96        if let Some(&size) = self.known_sizes.get(type_name) {
97            return Some(size);
98        }
99
100        if let Some(learned) = self.learned_sizes.get(type_name) {
101            if learned.confidence > 0.8 {
102                return Some(learned.average as usize);
103            }
104        }
105
106        if let Some(size) = self.estimate_container_size(type_name) {
107            return Some(size);
108        }
109
110        Some(self.heuristic_estimate(type_name))
111    }
112
113    fn learn_from_real_allocation(&mut self, type_name: &str, actual_size: usize) {
114        let entry = self
115            .learned_sizes
116            .entry(type_name.to_string())
117            .or_insert(LearnedSize {
118                average: actual_size as f64,
119                count: 0,
120                confidence: 0.0,
121            });
122
123        entry.count += 1;
124        let alpha = (1.0 / entry.count.min(20) as f64).max(0.05);
125        entry.average = entry.average * (1.0 - alpha) + actual_size as f64 * alpha;
126        entry.confidence = (entry.count as f64 / 50.0).min(0.99);
127    }
128}
129
130impl Default for SmartSizeEstimator {
131    fn default() -> Self {
132        Self::new()
133    }
134}
135
136#[cfg(test)]
137mod tests {
138    use super::*;
139
140    #[test]
141    fn test_basic_types() {
142        let estimator = SmartSizeEstimator::new();
143        assert_eq!(estimator.estimate_size("i32"), Some(4));
144        assert_eq!(estimator.estimate_size("bool"), Some(1));
145    }
146
147    #[test]
148    fn test_container_types() {
149        let estimator = SmartSizeEstimator::new();
150        assert!(estimator.estimate_size("Vec<i32>").is_some());
151        assert!(estimator.estimate_size("HashMap<String, i32>").is_some());
152    }
153
154    #[test]
155    fn test_learning() {
156        let mut estimator = SmartSizeEstimator::new();
157
158        // Learn from multiple allocations to build confidence
159        for _ in 0..10 {
160            estimator.learn_from_real_allocation("CustomType", 128);
161        }
162        for _ in 0..10 {
163            estimator.learn_from_real_allocation("CustomType", 132);
164        }
165
166        let size = estimator.estimate_size("CustomType");
167        assert!(size.is_some());
168        let size_value = size.expect("Size should exist");
169        // Learning algorithm should provide some reasonable estimate
170        assert!(size_value > 0 && size_value < 1000); // Very flexible range
171    }
172}