1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use std::collections::VecDeque;
/// Memory pool for efficient allocation and reuse of parser nodes
///
/// This module provides object pooling to reduce allocation overhead
/// during GLR parsing, especially for frequently allocated/deallocated
/// nodes during fork and merge operations.
use std::sync::Arc;
use std::sync::Mutex;
/// Default pool capacity
const DEFAULT_POOL_CAPACITY: usize = 256;
/// A thread-safe object pool using Arc for shared ownership
pub struct NodePool<T> {
/// Queue of available nodes
queue: Mutex<VecDeque<Arc<T>>>,
/// Maximum capacity of the pool
capacity: usize,
/// Statistics
stats: PoolStats,
}
/// Pool statistics for monitoring and tuning
#[derive(Default, Debug)]
pub struct PoolStats {
/// Total allocations from pool
pub gets: std::sync::atomic::AtomicU64,
/// Total returns to pool
pub puts: std::sync::atomic::AtomicU64,
/// Times pool was empty (had to allocate new)
pub misses: std::sync::atomic::AtomicU64,
/// Times pool was full (had to drop returned item)
pub drops: std::sync::atomic::AtomicU64,
}
impl<T> NodePool<T> {
/// Create a new pool with default capacity
pub fn new() -> Self {
Self::with_capacity(DEFAULT_POOL_CAPACITY)
}
/// Create a new pool with specified capacity
pub fn with_capacity(capacity: usize) -> Self {
Self {
queue: Mutex::new(VecDeque::with_capacity(capacity)),
capacity,
stats: PoolStats::default(),
}
}
/// Get a node from the pool or create a new one
pub fn get_or<F>(&self, factory: F) -> Arc<T>
where
F: FnOnce() -> T,
{
self.stats
.gets
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let mut queue = self.queue.lock().unwrap_or_else(|err| err.into_inner());
if let Some(node) = queue.pop_front() {
node
} else {
self.stats
.misses
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
Arc::new(factory())
}
}
/// Get a node from the pool or use default
pub fn get_or_default(&self) -> Arc<T>
where
T: Default,
{
self.get_or(T::default)
}
/// Return a node to the pool for reuse
pub fn put(&self, node: Arc<T>) {
// Only return to pool if there's a single reference
if Arc::strong_count(&node) == 1 {
self.stats
.puts
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let mut queue = self.queue.lock().unwrap_or_else(|err| err.into_inner());
if queue.len() < self.capacity {
queue.push_back(node);
} else {
self.stats
.drops
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
// Drop the node (let it be deallocated)
}
}
}
/// Clear all nodes from the pool
pub fn clear(&self) {
let mut queue = self.queue.lock().unwrap_or_else(|err| err.into_inner());
queue.clear();
}
/// Get current pool size
pub fn size(&self) -> usize {
let queue = self.queue.lock().unwrap_or_else(|err| err.into_inner());
queue.len()
}
/// Get pool statistics
pub fn stats(&self) -> PoolStatsSnapshot {
PoolStatsSnapshot {
gets: self.stats.gets.load(std::sync::atomic::Ordering::Relaxed),
puts: self.stats.puts.load(std::sync::atomic::Ordering::Relaxed),
misses: self.stats.misses.load(std::sync::atomic::Ordering::Relaxed),
drops: self.stats.drops.load(std::sync::atomic::Ordering::Relaxed),
current_size: self.size(),
capacity: self.capacity,
}
}
}
impl<T> Default for NodePool<T> {
fn default() -> Self {
Self::new()
}
}
/// Snapshot of pool statistics
#[derive(Debug, Clone)]
pub struct PoolStatsSnapshot {
/// Total number of objects requested from the pool.
pub gets: u64,
/// Total number of objects returned to the pool.
pub puts: u64,
/// Number of requests that were not satisfied from the pool.
pub misses: u64,
/// Number of objects dropped instead of pooled.
pub drops: u64,
/// Current number of objects stored in the pool.
pub current_size: usize,
/// Maximum capacity of the pool.
pub capacity: usize,
}
impl PoolStatsSnapshot {
/// Calculate hit rate (percentage of gets that were satisfied from pool)
pub fn hit_rate(&self) -> f64 {
if self.gets == 0 {
0.0
} else {
((self.gets - self.misses) as f64 / self.gets as f64) * 100.0
}
}
/// Calculate reuse rate (percentage of puts that were kept in pool)
pub fn reuse_rate(&self) -> f64 {
if self.puts == 0 {
0.0
} else {
((self.puts - self.drops) as f64 / self.puts as f64) * 100.0
}
}
}
impl std::fmt::Display for PoolStatsSnapshot {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Pool Stats: {} gets, {} puts, {:.1}% hit rate, {:.1}% reuse rate, {}/{} capacity",
self.gets,
self.puts,
self.hit_rate(),
self.reuse_rate(),
self.current_size,
self.capacity
)
}
}
// Note: StackNodePool removed as stack module is in glr-core
// To use pool with StackNode, import both modules in glr-core
#[cfg(test)]
mod tests {
use super::*;
#[derive(Default, Debug)]
struct TestNode {
#[allow(dead_code)]
value: u32,
}
#[test]
fn test_pool_basic() {
let pool = NodePool::<TestNode>::with_capacity(2);
// Get a node
let node1 = pool.get_or_default();
assert_eq!(pool.size(), 0);
// Return it
pool.put(node1);
assert_eq!(pool.size(), 1);
// Get it again (should reuse)
let _node2 = pool.get_or_default();
assert_eq!(pool.size(), 0);
let stats = pool.stats();
assert_eq!(stats.gets, 2);
assert_eq!(stats.puts, 1);
assert_eq!(stats.misses, 1); // First get was a miss
}
#[test]
fn test_pool_capacity() {
let pool = NodePool::<TestNode>::with_capacity(2);
let node1 = Arc::new(TestNode { value: 1 });
let node2 = Arc::new(TestNode { value: 2 });
let node3 = Arc::new(TestNode { value: 3 });
pool.put(node1);
pool.put(node2);
pool.put(node3); // Should be dropped
assert_eq!(pool.size(), 2);
let stats = pool.stats();
assert_eq!(stats.drops, 1);
}
#[test]
fn test_pool_shared_references() {
let pool = NodePool::<TestNode>::new();
let node = Arc::new(TestNode { value: 42 });
let node_clone = node.clone();
// Should not be added to pool (has multiple references)
pool.put(node);
assert_eq!(pool.size(), 0);
// Drop the clone
drop(node_clone);
// Now the original node could be pooled if we still had a reference
}
}