book_scratch_pool/
book_scratch_pool.rs1extern crate mnemosyne;
14
15use mnemosyne::{MAX_POOL_SLOTS, ScratchBank, ScratchPool};
16
17fn dot_product(a: &[f64], b: &[f64]) -> f64 {
18 assert_eq!(a.len(), b.len());
19 a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
20}
21
22fn main() {
23 let f64_pool: ScratchPool<f64> = ScratchPool::new();
28 let f32_pool: ScratchPool<f32> = ScratchPool::new();
29
30 f64_pool.with_scratch(1024, |scratch| {
32 for (i, slot) in scratch.iter_mut().enumerate() {
33 *slot = i as f64;
34 }
35 let partial: f64 = scratch.iter().sum();
36 println!(
37 "sum of 0..1024 via scratch: {} (expected {})",
38 partial,
39 (0..1024usize).sum::<usize>() as f64,
40 );
41 assert_eq!(partial, (0..1024usize).sum::<usize>() as f64);
42 });
43
44 f64_pool.with_scratch(256, |signal| {
46 for (i, s) in signal.iter_mut().enumerate() {
47 *s = (i as f64).sin();
48 }
49 f64_pool.with_scratch(16, |window| {
50 window.copy_from_slice(&signal[..16]);
51 let dp = dot_product(window, &signal[..16]);
52 println!("windowed dot-product: {dp:.6}");
53 assert!(dp.is_finite());
54 });
55 });
56
57 f32_pool.with_scratch(64, |buf| {
59 buf.fill(1.0_f32);
60 let total: f32 = buf.iter().sum();
61 println!("f32 scratch sum: {total}");
62 assert_eq!(total, 64.0_f32);
63 });
64
65 let bank: ScratchBank<f64, 2> = ScratchBank::new();
69 bank.with_scratch::<1, _>(32, |scratch| {
70 scratch.fill(3.5);
71 assert!(scratch.iter().all(|v| *v == 3.5));
72 });
73
74 let bounded_pool: ScratchPool<u32> = ScratchPool::new();
77 bounded_pool.with_scratch_bounded(1024, |_| {});
78 bounded_pool.with_scratch_bounded(1025, |_| {});
79 assert_eq!(bounded_pool.capacity(), 2048);
80 let retained = bounded_pool.release();
81 println!(
82 "bounded release: capacity={} -> retained={}",
83 2048, retained[0]
84 );
85 assert_eq!(retained[0], 1025);
86 bounded_pool.reset();
87 assert_eq!(bounded_pool.release()[0], 0);
88
89 println!("MAX_POOL_SLOTS = {MAX_POOL_SLOTS} (max concurrent nested borrows)");
90 println!("all scratch-pool assertions passed");
91}