adze-stack-pool-core 0.1.0

Thread-local and reusable stack pool utilities for parser workloads.
Documentation
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use adze_stack_pool_core::{StackPool, get_thread_local_pool, init_thread_local_pool};

// ── 1. Pool allocation and deallocation ──────────────────────────────────

#[test]
fn fresh_pool_has_zero_stats() {
    let pool: StackPool<u32> = StackPool::new(8);
    let stats = pool.stats();
    assert_eq!(stats.total_allocations, 0);
    assert_eq!(stats.reuse_count, 0);
    assert_eq!(stats.pool_hits, 0);
    assert_eq!(stats.pool_misses, 0);
    assert_eq!(stats.max_pool_depth, 0);
}

#[test]
fn multiple_allocations_tracked_correctly() {
    let pool: StackPool<u32> = StackPool::new(4);
    let s1 = pool.acquire();
    let s2 = pool.acquire();
    let s3 = pool.acquire();
    assert_eq!(pool.stats().total_allocations, 3);
    assert_eq!(pool.stats().pool_misses, 3);
    assert_eq!(pool.stats().pool_hits, 0);
    // Clean up to avoid leak warnings
    drop(s1);
    drop(s2);
    drop(s3);
}

#[test]
fn release_then_reacquire_does_not_increment_total_allocations() {
    let pool: StackPool<u32> = StackPool::new(4);
    let s = pool.acquire();
    pool.release(s);
    let _reused = pool.acquire();
    // Only 1 real allocation; the second was a reuse
    assert_eq!(pool.stats().total_allocations, 1);
    assert_eq!(pool.stats().pool_hits, 1);
}

// ── 2. Stack push/pop operations ─────────────────────────────────────────

#[test]
fn push_and_pop_on_acquired_stack() {
    let pool: StackPool<i32> = StackPool::new(4);
    let mut stack = pool.acquire();
    stack.push(10);
    stack.push(20);
    stack.push(30);
    assert_eq!(stack.pop(), Some(30));
    assert_eq!(stack.pop(), Some(20));
    assert_eq!(stack.pop(), Some(10));
    assert_eq!(stack.pop(), None);
    pool.release(stack);
}

#[test]
fn push_pop_cycle_then_reuse_yields_empty_stack() {
    let pool: StackPool<u64> = StackPool::new(4);
    let mut stack = pool.acquire();
    for i in 0..100 {
        stack.push(i);
    }
    for _ in 0..50 {
        stack.pop();
    }
    assert_eq!(stack.len(), 50);
    pool.release(stack);

    let reused = pool.acquire();
    assert!(reused.is_empty());
    pool.release(reused);
}

#[test]
fn stack_retains_capacity_after_push_pop_and_reuse() {
    let pool: StackPool<u32> = StackPool::new(4);
    let mut stack = pool.acquire();
    // Push enough to potentially grow beyond initial 256
    for i in 0..300 {
        stack.push(i);
    }
    let grown_capacity = stack.capacity();
    assert!(grown_capacity >= 300);
    pool.release(stack);

    let reused = pool.acquire();
    // Reused stack keeps the grown capacity
    assert!(reused.capacity() >= grown_capacity);
    assert!(reused.is_empty());
    pool.release(reused);
}

// ── 3. Pool capacity growth ──────────────────────────────────────────────

#[test]
fn pool_depth_grows_up_to_max() {
    let pool: StackPool<u32> = StackPool::new(3);
    let s1 = pool.acquire();
    let s2 = pool.acquire();
    let s3 = pool.acquire();
    pool.release(s1);
    assert_eq!(pool.stats().max_pool_depth, 1);
    pool.release(s2);
    assert_eq!(pool.stats().max_pool_depth, 2);
    pool.release(s3);
    assert_eq!(pool.stats().max_pool_depth, 3);
}

#[test]
fn pool_depth_stops_at_max_size() {
    let pool: StackPool<u32> = StackPool::new(2);
    let s1 = pool.acquire();
    let s2 = pool.acquire();
    let s3 = pool.acquire();
    pool.release(s1);
    pool.release(s2);
    pool.release(s3); // exceeds max_pool_size=2, should be dropped
    assert_eq!(pool.stats().max_pool_depth, 2);
}

#[test]
fn acquire_with_capacity_prefers_best_fit() {
    let pool: StackPool<u8> = StackPool::new(4);
    // Seed pool with various capacities
    let small: Vec<u8> = Vec::with_capacity(32);
    let medium: Vec<u8> = Vec::with_capacity(128);
    let large: Vec<u8> = Vec::with_capacity(512);
    pool.release(small);
    pool.release(medium);
    pool.release(large);

    // Request capacity 100: should pick the first >= 100 (128)
    let acquired = pool.acquire_with_capacity(100);
    assert!(acquired.capacity() >= 100);
}

// ── 4. Stack reuse after return ──────────────────────────────────────────

#[test]
fn reused_stacks_are_always_cleared() {
    let pool: StackPool<String> = StackPool::new(4);
    let mut stack = pool.acquire();
    stack.push("hello".to_string());
    stack.push("world".to_string());
    pool.release(stack);

    let reused = pool.acquire();
    assert!(reused.is_empty(), "reused stack should be empty");
    pool.release(reused);
}

#[test]
fn repeated_acquire_release_cycles_reuse_correctly() {
    let pool: StackPool<u32> = StackPool::new(2);
    for i in 0..10 {
        let mut s = pool.acquire();
        s.push(i);
        pool.release(s);
    }
    let stats = pool.stats();
    // First acquire is a miss, subsequent 9 are hits
    assert_eq!(stats.pool_misses, 1);
    assert_eq!(stats.pool_hits, 9);
    assert_eq!(stats.reuse_count, 9);
    assert_eq!(stats.total_allocations, 1);
}

#[test]
fn clone_stack_reuses_pooled_stack_when_available() {
    let pool: StackPool<u32> = StackPool::new(4);
    // Seed pool with a large-capacity stack
    let big: Vec<u32> = Vec::with_capacity(512);
    pool.release(big);
    pool.reset_stats();

    let source = vec![1, 2, 3, 4, 5];
    let cloned = pool.clone_stack(&source);
    assert_eq!(&cloned[..], &source[..]);
    // Should have reused the pooled stack (hit)
    assert_eq!(pool.stats().pool_hits, 1);
}

// ── 5. Multiple stacks from same pool ────────────────────────────────────

#[test]
fn multiple_simultaneous_stacks_from_one_pool() {
    let pool: StackPool<u32> = StackPool::new(4);
    let mut s1 = pool.acquire();
    let mut s2 = pool.acquire();
    let mut s3 = pool.acquire();

    s1.push(1);
    s2.push(2);
    s3.push(3);

    // They are independent
    assert_eq!(s1, vec![1]);
    assert_eq!(s2, vec![2]);
    assert_eq!(s3, vec![3]);

    pool.release(s1);
    pool.release(s2);
    pool.release(s3);

    assert_eq!(pool.stats().total_allocations, 3);
    assert_eq!(pool.stats().max_pool_depth, 3);
}

#[test]
fn interleaved_acquire_and_release() {
    let pool: StackPool<u32> = StackPool::new(4);

    let s1 = pool.acquire(); // miss
    let s2 = pool.acquire(); // miss
    pool.release(s1);
    let s3 = pool.acquire(); // hit (reuses s1)
    pool.release(s2);
    pool.release(s3);

    let stats = pool.stats();
    assert_eq!(stats.pool_misses, 2);
    assert_eq!(stats.pool_hits, 1);
    assert_eq!(stats.total_allocations, 2);
}

// ── 6. Edge cases ────────────────────────────────────────────────────────

#[test]
fn zero_capacity_pool_never_pools() {
    let pool: StackPool<u32> = StackPool::new(0);
    let s = pool.acquire();
    pool.release(s);
    // Pool can't hold anything, so release just drops
    assert_eq!(pool.stats().max_pool_depth, 0);

    let _ = pool.acquire(); // must allocate fresh
    assert_eq!(pool.stats().pool_misses, 2);
    assert_eq!(pool.stats().pool_hits, 0);
}

#[test]
fn acquire_with_zero_capacity() {
    let pool: StackPool<u32> = StackPool::new(4);
    let s = pool.acquire_with_capacity(0);
    // Should still work; Vec::with_capacity(0) is valid
    assert!(s.is_empty());
    pool.release(s);
}

#[test]
fn empty_stack_operations() {
    let pool: StackPool<u32> = StackPool::new(4);
    let stack = pool.acquire();
    assert!(stack.is_empty());
    assert_eq!(stack.len(), 0);
    pool.release(stack);
}

#[test]
fn release_empty_vec_with_no_capacity() {
    let pool: StackPool<u32> = StackPool::new(4);
    let empty: Vec<u32> = Vec::new();
    pool.release(empty);
    // Should be accepted (capacity 0 <= 4096)
    assert_eq!(pool.stats().max_pool_depth, 1);
}

#[test]
fn release_exactly_at_max_capacity_boundary() {
    let pool: StackPool<u8> = StackPool::new(4);
    let at_limit: Vec<u8> = Vec::with_capacity(4096);
    pool.release(at_limit);
    assert_eq!(pool.stats().max_pool_depth, 1);
}

#[test]
fn release_one_over_max_capacity_boundary() {
    let pool: StackPool<u8> = StackPool::new(4);
    let over_limit: Vec<u8> = Vec::with_capacity(4097);
    pool.release(over_limit);
    assert_eq!(pool.stats().max_pool_depth, 0);
}

#[test]
fn clone_empty_slice() {
    let pool: StackPool<u32> = StackPool::new(4);
    let empty: &[u32] = &[];
    let cloned = pool.clone_stack(empty);
    assert!(cloned.is_empty());
}

#[test]
fn clone_single_element() {
    let pool: StackPool<u32> = StackPool::new(4);
    let single = vec![42u32];
    let cloned = pool.clone_stack(&single);
    assert_eq!(cloned, vec![42]);
}

#[test]
fn debug_format_includes_pool_info() {
    let pool: StackPool<u32> = StackPool::new(4);
    let _ = pool.acquire();
    let debug = format!("{:?}", pool);
    assert!(debug.contains("StackPool"));
    assert!(debug.contains("max_pool_size"));
}

// ── 7. Thread safety (thread-local isolation) ────────────────────────────

#[test]
fn thread_local_pool_auto_initializes() {
    let pool = get_thread_local_pool();
    let s = pool.acquire();
    assert!(s.is_empty());
    pool.release(s);
}

#[test]
fn thread_local_pool_returns_same_instance() {
    init_thread_local_pool(8);
    let a = get_thread_local_pool();
    let b = get_thread_local_pool();
    assert!(std::rc::Rc::ptr_eq(&a, &b));
}

#[test]
fn thread_local_pools_are_isolated_across_threads() {
    use std::sync::mpsc;

    let (tx1, rx1) = mpsc::channel();
    let (tx2, rx2) = mpsc::channel();

    let t1 = std::thread::spawn(move || {
        init_thread_local_pool(4);
        let pool = get_thread_local_pool();
        let s = pool.acquire();
        pool.release(s);
        tx1.send(pool.stats().total_allocations).unwrap();
    });

    let t2 = std::thread::spawn(move || {
        init_thread_local_pool(4);
        let pool = get_thread_local_pool();
        let _s1 = pool.acquire();
        let _s2 = pool.acquire();
        tx2.send(pool.stats().total_allocations).unwrap();
    });

    t1.join().unwrap();
    t2.join().unwrap();

    let allocs_t1 = rx1.recv().unwrap();
    let allocs_t2 = rx2.recv().unwrap();
    // Thread 1 made 1 allocation, thread 2 made 2 — they're independent
    assert_eq!(allocs_t1, 1);
    assert_eq!(allocs_t2, 2);
}

// ── 8. Memory efficiency ─────────────────────────────────────────────────

#[test]
fn pool_reuse_avoids_repeated_allocation() {
    let pool: StackPool<u32> = StackPool::new(4);
    for _ in 0..100 {
        let s = pool.acquire();
        pool.release(s);
    }
    let stats = pool.stats();
    // Only 1 real allocation for 100 acquire/release cycles
    assert_eq!(stats.total_allocations, 1);
    assert_eq!(stats.pool_hits, 99);
}

#[test]
fn capacity_preserved_across_reuse_cycles() {
    let pool: StackPool<u32> = StackPool::new(4);
    let mut s = pool.acquire();
    // Grow the stack well beyond default 256
    for i in 0..500 {
        s.push(i);
    }
    let cap = s.capacity();
    pool.release(s);

    let reused = pool.acquire();
    assert_eq!(
        reused.capacity(),
        cap,
        "capacity should be preserved on reuse"
    );
    pool.release(reused);
}

#[test]
fn oversized_stack_not_pooled_saves_memory() {
    let pool: StackPool<u32> = StackPool::new(4);
    // Release a huge stack — it should be dropped, not pooled
    let huge: Vec<u32> = Vec::with_capacity(10_000);
    pool.release(huge);

    // Next acquire must allocate fresh (miss)
    let _ = pool.acquire();
    assert_eq!(pool.stats().pool_hits, 0);
    assert_eq!(pool.stats().pool_misses, 1);
}

#[test]
fn clear_frees_all_pooled_stacks() {
    let pool: StackPool<u32> = StackPool::new(4);
    for _ in 0..4 {
        let s = pool.acquire();
        pool.release(s);
    }
    pool.clear();
    pool.reset_stats();

    // After clear, everything is a miss
    let _ = pool.acquire();
    assert_eq!(pool.stats().pool_hits, 0);
    assert_eq!(pool.stats().pool_misses, 1);
}

#[test]
fn acquire_with_capacity_falls_back_to_fresh_when_none_big_enough() {
    let pool: StackPool<u32> = StackPool::new(4);
    // Seed pool with small stacks
    for _ in 0..3 {
        let small: Vec<u32> = Vec::with_capacity(16);
        pool.release(small);
    }
    pool.reset_stats();

    // Request much larger capacity — no pooled stack qualifies
    let s = pool.acquire_with_capacity(1024);
    assert!(s.capacity() >= 1024);
    assert_eq!(pool.stats().pool_misses, 1);
    assert_eq!(pool.stats().pool_hits, 0);
}

// ── PoolStats equality / hash ────────────────────────────────────────────

#[test]
fn pool_stats_default_is_all_zeros() {
    let stats = adze_stack_pool_core::PoolStats::default();
    assert_eq!(stats.total_allocations, 0);
    assert_eq!(stats.reuse_count, 0);
    assert_eq!(stats.pool_hits, 0);
    assert_eq!(stats.pool_misses, 0);
    assert_eq!(stats.max_pool_depth, 0);
}

#[test]
fn pool_stats_equality() {
    let pool: StackPool<u32> = StackPool::new(4);
    let s = pool.acquire();
    pool.release(s);
    let _ = pool.acquire();

    let a = pool.stats();
    let b = pool.stats();
    assert_eq!(a, b);
}