grift_arena/stats.rs
1//! Statistics types for arena usage monitoring.
2
3// — GcStats —
4
5/// Statistics returned by garbage collection.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
7pub struct GcStats {
8 /// Number of objects that were marked as reachable.
9 pub marked: usize,
10
11 /// Number of objects that were collected (freed).
12 pub collected: usize,
13
14 /// Number of objects that existed before collection.
15 pub total_before: usize,
16}
17
18impl GcStats {
19 /// Check if any garbage was collected.
20 pub const fn did_collect(&self) -> bool {
21 self.collected > 0
22 }
23
24 /// Get the number of objects remaining after collection.
25 pub const fn remaining(&self) -> usize {
26 self.total_before - self.collected
27 }
28
29 /// Get the collection ratio (0.0 to 1.0).
30 ///
31 /// Returns 0.0 if no objects existed before collection.
32 pub fn collection_ratio(&self) -> f32 {
33 if self.total_before == 0 {
34 0.0
35 } else {
36 self.collected as f32 / self.total_before as f32
37 }
38 }
39
40 /// Get the survival ratio (0.0 to 1.0).
41 ///
42 /// Returns 1.0 if no objects existed before collection.
43 pub fn survival_ratio(&self) -> f32 {
44 if self.total_before == 0 {
45 1.0
46 } else {
47 self.marked as f32 / self.total_before as f32
48 }
49 }
50}
51
52// — ArenaStats —
53
54/// Statistics about arena usage.
55#[derive(Debug, Clone, Copy, PartialEq, Default)]
56pub struct ArenaStats {
57 /// Total capacity of the arena.
58 pub capacity: usize,
59
60 /// Number of currently allocated cells.
61 pub allocated: usize,
62
63 /// Number of free cells.
64 pub free: usize,
65
66 /// Fragmentation ratio (0.0 = not fragmented, 1.0 = highly fragmented).
67 pub fragmentation: f32,
68}
69
70impl ArenaStats {
71 /// Get usage as a percentage (0-100).
72 pub fn usage_percent(&self) -> f32 {
73 if self.capacity == 0 {
74 0.0
75 } else {
76 (self.allocated as f32 / self.capacity as f32) * 100.0
77 }
78 }
79
80 /// Get free space as a percentage (0-100).
81 pub fn free_percent(&self) -> f32 {
82 100.0 - self.usage_percent()
83 }
84
85 /// Check if the arena is empty.
86 pub const fn is_empty(&self) -> bool {
87 self.allocated == 0
88 }
89
90 /// Check if the arena is full.
91 pub const fn is_full(&self) -> bool {
92 self.free == 0
93 }
94
95 /// Check if fragmentation is above a threshold.
96 pub fn is_fragmented(&self, threshold: f32) -> bool {
97 self.fragmentation > threshold
98 }
99}