numrs2 0.3.3

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
//! Parallel memory allocation strategies
//!
//! This module provides thread-safe memory allocators optimized for
//! parallel numerical computations with minimal contention.

use crate::error::{NumRs2Error, Result};
use crate::traits::{AllocationStats, MemoryAllocator, SpecializedAllocator};
use std::alloc::Layout;
use std::cell::RefCell;
use std::collections::HashMap;
use std::ptr::NonNull;
use std::sync::{Arc, Mutex, RwLock};
use std::thread::{self, ThreadId};
use std::time::{Duration, Instant};

thread_local! {
    /// Thread-local allocator for reduced contention
    static LOCAL_ALLOCATOR: RefCell<Option<ThreadLocalState>> = const { RefCell::new(None) };
}

/// Thread-local allocation state
#[derive(Debug)]
struct ThreadLocalState {
    allocator: Box<dyn MemoryAllocator<Error = NumRs2Error> + Send>,
    stats: AllocationStats,
    last_gc: Instant,
    cache_size_limit: usize,
    cached_blocks: Vec<CachedBlock>,
}

#[derive(Debug)]
struct CachedBlock {
    ptr: NonNull<u8>,
    layout: Layout,
    allocated_at: Instant,
}

// SAFETY: CachedBlock is safe to send between threads because:
// 1. NonNull<u8> is just a pointer wrapper and the actual memory management is handled by allocators
// 2. Layout and Instant are both Send + Sync
// 3. The allocator ensures memory safety across threads
unsafe impl Send for CachedBlock {}
unsafe impl Sync for CachedBlock {}

impl ThreadLocalState {
    fn new<A>(allocator: A, cache_size_limit: usize) -> Self
    where
        A: MemoryAllocator<Error = NumRs2Error> + Send + 'static,
    {
        Self {
            allocator: Box::new(allocator),
            stats: AllocationStats::default(),
            last_gc: Instant::now(),
            cache_size_limit,
            cached_blocks: Vec::new(),
        }
    }

    fn try_allocate_from_cache(&mut self, layout: Layout) -> Option<NonNull<u8>> {
        // Find a cached block that fits
        for (i, block) in self.cached_blocks.iter().enumerate() {
            if block.layout.size() >= layout.size() && block.layout.align() >= layout.align() {
                let ptr = block.ptr;
                self.cached_blocks.remove(i);
                return Some(ptr);
            }
        }
        None
    }

    fn cache_block(&mut self, ptr: NonNull<u8>, layout: Layout) {
        if self.cached_blocks.len() < self.cache_size_limit {
            self.cached_blocks.push(CachedBlock {
                ptr,
                layout,
                allocated_at: Instant::now(),
            });
        } else {
            // Cache is full, actually deallocate
            unsafe {
                let _ = self.allocator.deallocate(ptr, layout);
            }
        }
    }

    fn garbage_collect(&mut self, max_age: Duration) {
        let now = Instant::now();
        self.cached_blocks.retain(|block| {
            if now.duration_since(block.allocated_at) > max_age {
                // Block is too old, deallocate it
                unsafe {
                    let _ = self.allocator.deallocate(block.ptr, block.layout);
                }
                false
            } else {
                true
            }
        });
        self.last_gc = now;
    }

    fn should_gc(&self, gc_interval: Duration) -> bool {
        self.last_gc.elapsed() > gc_interval
    }
}

/// Configuration for parallel allocator
#[derive(Debug, Clone)]
pub struct ParallelAllocatorConfig {
    /// Enable thread-local caching
    pub enable_thread_local_cache: bool,
    /// Maximum cached blocks per thread
    pub max_cached_blocks_per_thread: usize,
    /// Garbage collection interval
    pub gc_interval: Duration,
    /// Maximum age for cached blocks
    pub max_block_age: Duration,
    /// Enable NUMA-aware allocation
    pub numa_aware: bool,
    /// Global pool size for shared allocations
    pub global_pool_size: usize,
    /// Enable allocation tracking
    pub enable_tracking: bool,
}

impl Default for ParallelAllocatorConfig {
    fn default() -> Self {
        Self {
            enable_thread_local_cache: true,
            max_cached_blocks_per_thread: 100,
            gc_interval: Duration::from_secs(30),
            max_block_age: Duration::from_secs(300),
            numa_aware: false,
            global_pool_size: 1024 * 1024, // 1MB
            enable_tracking: true,
        }
    }
}

/// Parallel memory allocator with thread-local optimization
pub struct ParallelAllocator<A>
where
    A: MemoryAllocator<Error = NumRs2Error> + Send + Sync + Clone,
{
    base_allocator: A,
    config: ParallelAllocatorConfig,
    global_stats: Arc<RwLock<AllocationStats>>,
    thread_allocators: Arc<Mutex<HashMap<ThreadId, Arc<Mutex<ThreadLocalState>>>>>,
    global_pool: Arc<Mutex<Vec<CachedBlock>>>,
}

impl<A> std::fmt::Debug for ParallelAllocator<A>
where
    A: MemoryAllocator<Error = NumRs2Error> + Send + Sync + Clone,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ParallelAllocator")
            .field("base_allocator", &"<allocator>")
            .field("config", &self.config)
            .field("global_stats", &"<mutex>")
            .field("thread_allocators", &"<mutex>")
            .field("global_pool", &"<mutex>")
            .finish()
    }
}

impl<A> ParallelAllocator<A>
where
    A: MemoryAllocator<Error = NumRs2Error> + Send + Sync + Clone + 'static,
{
    /// Create a new parallel allocator
    pub fn new(base_allocator: A, config: ParallelAllocatorConfig) -> Self {
        Self {
            base_allocator,
            config,
            global_stats: Arc::new(RwLock::new(AllocationStats::default())),
            thread_allocators: Arc::new(Mutex::new(HashMap::new())),
            global_pool: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Get or create thread-local state
    fn get_thread_local_state(&self) -> Result<Arc<Mutex<ThreadLocalState>>> {
        let thread_id = thread::current().id();
        let mut allocators = self
            .thread_allocators
            .lock()
            .expect("lock should not be poisoned");

        if let Some(state) = allocators.get(&thread_id) {
            Ok(Arc::clone(state))
        } else {
            let local_state = ThreadLocalState::new(
                self.base_allocator.clone(),
                self.config.max_cached_blocks_per_thread,
            );
            let state = Arc::new(Mutex::new(local_state));
            allocators.insert(thread_id, Arc::clone(&state));
            Ok(state)
        }
    }

    /// Try to allocate from global pool
    fn try_allocate_from_global_pool(&self, layout: Layout) -> Option<NonNull<u8>> {
        if !self.config.enable_thread_local_cache {
            return None;
        }

        let mut pool = self
            .global_pool
            .lock()
            .expect("lock should not be poisoned");

        for (i, block) in pool.iter().enumerate() {
            if block.layout.size() >= layout.size() && block.layout.align() >= layout.align() {
                let ptr = block.ptr;
                pool.remove(i);
                return Some(ptr);
            }
        }
        None
    }

    /// Return block to global pool
    fn return_to_global_pool(&self, ptr: NonNull<u8>, layout: Layout) {
        if !self.config.enable_thread_local_cache {
            unsafe {
                let _ = self.base_allocator.deallocate(ptr, layout);
            }
            return;
        }

        let mut pool = self
            .global_pool
            .lock()
            .expect("lock should not be poisoned");

        if pool.len() < self.config.global_pool_size / std::mem::size_of::<CachedBlock>() {
            pool.push(CachedBlock {
                ptr,
                layout,
                allocated_at: Instant::now(),
            });
        } else {
            // Pool is full, actually deallocate
            unsafe {
                let _ = self.base_allocator.deallocate(ptr, layout);
            }
        }
    }

    /// Trigger garbage collection for all thread-local caches
    pub fn garbage_collect_all(&self) -> Result<()> {
        let allocators = self
            .thread_allocators
            .lock()
            .expect("lock should not be poisoned");

        for state in allocators.values() {
            if let Ok(mut local_state) = state.try_lock() {
                local_state.garbage_collect(self.config.max_block_age);
            }
        }

        // Also clean global pool
        {
            let mut pool = self
                .global_pool
                .lock()
                .expect("lock should not be poisoned");
            let now = Instant::now();
            pool.retain(|block| {
                if now.duration_since(block.allocated_at) > self.config.max_block_age {
                    unsafe {
                        let _ = self.base_allocator.deallocate(block.ptr, block.layout);
                    }
                    false
                } else {
                    true
                }
            });
        }

        Ok(())
    }

    /// Get aggregate statistics from all threads
    pub fn aggregate_statistics(&self) -> AllocationStats {
        let global_stats = self
            .global_stats
            .read()
            .expect("lock should not be poisoned")
            .clone();
        let allocators = self
            .thread_allocators
            .lock()
            .expect("lock should not be poisoned");

        let mut aggregate = global_stats;

        for state in allocators.values() {
            if let Ok(local_state) = state.try_lock() {
                aggregate.bytes_allocated += local_state.stats.bytes_allocated;
                aggregate.bytes_deallocated += local_state.stats.bytes_deallocated;
                aggregate.allocation_count += local_state.stats.allocation_count;
                aggregate.deallocation_count += local_state.stats.deallocation_count;
                aggregate.active_allocations += local_state.stats.active_allocations;
                aggregate.peak_usage = aggregate.peak_usage.max(local_state.stats.peak_usage);
            }
        }

        aggregate
    }

    /// Get number of cached blocks across all threads
    pub fn total_cached_blocks(&self) -> usize {
        let allocators = self
            .thread_allocators
            .lock()
            .expect("lock should not be poisoned");
        let mut total = 0;

        for state in allocators.values() {
            if let Ok(local_state) = state.try_lock() {
                total += local_state.cached_blocks.len();
            }
        }

        total += self
            .global_pool
            .lock()
            .expect("lock should not be poisoned")
            .len();
        total
    }

    /// Force cleanup of all cached memory
    pub fn force_cleanup(&self) -> Result<()> {
        // Clean thread-local caches
        let allocators = self
            .thread_allocators
            .lock()
            .expect("lock should not be poisoned");

        for state in allocators.values() {
            if let Ok(mut local_state) = state.try_lock() {
                for block in local_state.cached_blocks.drain(..) {
                    unsafe {
                        self.base_allocator.deallocate(block.ptr, block.layout)?;
                    }
                }
            }
        }

        // Clean global pool
        {
            let mut pool = self
                .global_pool
                .lock()
                .expect("lock should not be poisoned");
            for block in pool.drain(..) {
                unsafe {
                    self.base_allocator.deallocate(block.ptr, block.layout)?;
                }
            }
        }

        Ok(())
    }
}

impl<A> MemoryAllocator for ParallelAllocator<A>
where
    A: MemoryAllocator<Error = NumRs2Error> + Send + Sync + Clone + 'static,
{
    type Error = NumRs2Error;

    fn allocate(&self, layout: Layout) -> Result<NonNull<u8>> {
        // Try thread-local cache first
        if self.config.enable_thread_local_cache {
            let state = self.get_thread_local_state()?;
            let mut local_state = state.lock().expect("lock should not be poisoned");

            // Check if we should do garbage collection
            if local_state.should_gc(self.config.gc_interval) {
                local_state.garbage_collect(self.config.max_block_age);
            }

            // Try to allocate from thread-local cache
            if let Some(ptr) = local_state.try_allocate_from_cache(layout) {
                local_state.stats.allocation_count += 1;
                local_state.stats.active_allocations += 1;
                return Ok(ptr);
            }
        }

        // Try global pool
        if let Some(ptr) = self.try_allocate_from_global_pool(layout) {
            if self.config.enable_tracking {
                let mut stats = self
                    .global_stats
                    .write()
                    .expect("lock should not be poisoned");
                stats.allocation_count += 1;
                stats.active_allocations += 1;
            }
            return Ok(ptr);
        }

        // Allocate new memory from base allocator
        let ptr = self.base_allocator.allocate(layout)?;

        if self.config.enable_tracking {
            let mut stats = self
                .global_stats
                .write()
                .expect("lock should not be poisoned");
            stats.bytes_allocated += layout.size();
            stats.allocation_count += 1;
            stats.active_allocations += 1;
            stats.peak_usage = stats
                .peak_usage
                .max(stats.bytes_allocated - stats.bytes_deallocated);
        }

        Ok(ptr)
    }

    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) -> Result<()> {
        // Try to cache the block for reuse
        if self.config.enable_thread_local_cache {
            if let Ok(state) = self.get_thread_local_state() {
                let mut local_state = state.lock().expect("lock should not be poisoned");
                if local_state.cached_blocks.len() < self.config.max_cached_blocks_per_thread {
                    local_state.cache_block(ptr, layout);
                    local_state.stats.deallocation_count += 1;
                    local_state.stats.active_allocations =
                        local_state.stats.active_allocations.saturating_sub(1);
                    return Ok(());
                }
            }
        }

        // Try global pool
        self.return_to_global_pool(ptr, layout);

        if self.config.enable_tracking {
            let mut stats = self
                .global_stats
                .write()
                .expect("lock should not be poisoned");
            stats.bytes_deallocated += layout.size();
            stats.deallocation_count += 1;
            stats.active_allocations = stats.active_allocations.saturating_sub(1);
        }

        Ok(())
    }

    unsafe fn reallocate(
        &self,
        ptr: NonNull<u8>,
        old_layout: Layout,
        new_layout: Layout,
    ) -> Result<NonNull<u8>> {
        // For simplicity, always allocate new and copy
        let new_ptr = self.allocate(new_layout)?;

        let copy_size = old_layout.size().min(new_layout.size());
        std::ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_ptr(), copy_size);

        self.deallocate(ptr, old_layout)?;

        Ok(new_ptr)
    }

    fn supports_layout(&self, layout: Layout) -> bool {
        self.base_allocator.supports_layout(layout)
    }

    fn preferred_alignment(&self) -> usize {
        self.base_allocator.preferred_alignment()
    }

    fn statistics(&self) -> Option<AllocationStats> {
        if self.config.enable_tracking {
            Some(self.aggregate_statistics())
        } else {
            None
        }
    }
}

impl<A> SpecializedAllocator for ParallelAllocator<A>
where
    A: MemoryAllocator<Error = NumRs2Error> + Send + Sync + Clone + 'static,
{
    fn allocation_error(&self, msg: &str) -> Self::Error {
        NumRs2Error::AllocationFailed(msg.to_string())
    }
}

/// Thread-local allocator that provides zero-contention allocation
pub struct ThreadLocalAllocator {
    config: ParallelAllocatorConfig,
}

impl ThreadLocalAllocator {
    /// Create a new thread-local allocator
    pub fn new(config: ParallelAllocatorConfig) -> Self {
        Self { config }
    }

    /// Initialize thread-local storage for current thread
    pub fn initialize_current_thread<A>(&self, allocator: A) -> Result<()>
    where
        A: MemoryAllocator<Error = NumRs2Error> + Send + 'static,
    {
        LOCAL_ALLOCATOR.with(|local| {
            let mut local_ref = local.borrow_mut();
            if local_ref.is_none() {
                *local_ref = Some(ThreadLocalState::new(
                    allocator,
                    self.config.max_cached_blocks_per_thread,
                ));
            }
        });
        Ok(())
    }

    /// Allocate using thread-local allocator
    pub fn allocate(&self, layout: Layout) -> Result<NonNull<u8>> {
        LOCAL_ALLOCATOR.with(|local| {
            let mut local_ref = local.borrow_mut();

            if let Some(ref mut state) = *local_ref {
                // Check cache first
                if let Some(ptr) = state.try_allocate_from_cache(layout) {
                    state.stats.allocation_count += 1;
                    state.stats.active_allocations += 1;
                    return Ok(ptr);
                }

                // Allocate new
                let ptr = state.allocator.allocate(layout)?;
                state.stats.bytes_allocated += layout.size();
                state.stats.allocation_count += 1;
                state.stats.active_allocations += 1;

                Ok(ptr)
            } else {
                Err(NumRs2Error::RuntimeError(
                    "Thread-local allocator not initialized".to_string(),
                ))
            }
        })
    }

    /// Deallocate using thread-local allocator
    ///
    /// # Safety
    ///
    /// The caller must ensure that:
    /// - `ptr` was allocated with this allocator using the same `layout`
    /// - `ptr` is not used after this call
    /// - The memory region pointed to by `ptr` is not accessed concurrently
    /// - The layout matches exactly the layout used during allocation
    pub unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) -> Result<()> {
        LOCAL_ALLOCATOR.with(|local| {
            let mut local_ref = local.borrow_mut();

            if let Some(ref mut state) = *local_ref {
                // Try to cache the block
                if state.cached_blocks.len() < self.config.max_cached_blocks_per_thread {
                    state.cache_block(ptr, layout);
                } else {
                    // Cache full, actually deallocate
                    state.allocator.deallocate(ptr, layout)?;
                }

                state.stats.bytes_deallocated += layout.size();
                state.stats.deallocation_count += 1;
                state.stats.active_allocations = state.stats.active_allocations.saturating_sub(1);

                Ok(())
            } else {
                Err(NumRs2Error::RuntimeError(
                    "Thread-local allocator not initialized".to_string(),
                ))
            }
        })
    }

    /// Get statistics for current thread
    pub fn current_thread_statistics(&self) -> Option<AllocationStats> {
        LOCAL_ALLOCATOR.with(|local| local.borrow().as_ref().map(|state| state.stats.clone()))
    }

    /// Trigger garbage collection for current thread
    pub fn garbage_collect_current_thread(&self) -> Result<()> {
        LOCAL_ALLOCATOR.with(|local| {
            let mut local_ref = local.borrow_mut();

            if let Some(ref mut state) = *local_ref {
                state.garbage_collect(self.config.max_block_age);
            }
        });
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::memory_alloc::NumericalArrayAllocator;
    use std::time::Duration;

    #[test]
    fn test_parallel_allocator_creation() {
        let base = NumericalArrayAllocator::new();
        let config = ParallelAllocatorConfig::default();
        let allocator = ParallelAllocator::new(base, config);

        assert!(allocator.config.enable_thread_local_cache);
        assert_eq!(allocator.total_cached_blocks(), 0);
    }

    #[test]
    fn test_basic_allocation() {
        let base = NumericalArrayAllocator::new();
        let config = ParallelAllocatorConfig::default();
        let allocator = ParallelAllocator::new(base, config);

        let layout =
            Layout::from_size_align(1024, 8).expect("layout with size 1024 and align 8 is valid");
        let ptr = allocator
            .allocate(layout)
            .expect("allocation should succeed");

        unsafe {
            allocator
                .deallocate(ptr, layout)
                .expect("deallocation should succeed");
        }
    }

    #[test]
    fn test_thread_local_caching() {
        let base = NumericalArrayAllocator::new();
        let config = ParallelAllocatorConfig {
            max_cached_blocks_per_thread: 5,
            ..Default::default()
        };
        let allocator = ParallelAllocator::new(base, config);

        let layout =
            Layout::from_size_align(64, 8).expect("layout with size 64 and align 8 is valid");

        // Allocate and deallocate several blocks
        for _ in 0..3 {
            let ptr = allocator
                .allocate(layout)
                .expect("allocation should succeed");
            unsafe {
                allocator
                    .deallocate(ptr, layout)
                    .expect("deallocation should succeed");
            }
        }

        // Should have cached blocks
        assert!(allocator.total_cached_blocks() > 0);
    }

    #[test]
    fn test_statistics_aggregation() {
        let base = NumericalArrayAllocator::new();
        let config = ParallelAllocatorConfig::default();
        let allocator = ParallelAllocator::new(base, config);

        let layout =
            Layout::from_size_align(128, 8).expect("layout with size 128 and align 8 is valid");

        // Make some allocations
        let mut ptrs = Vec::new();
        for _ in 0..5 {
            ptrs.push(
                allocator
                    .allocate(layout)
                    .expect("allocation should succeed"),
            );
        }

        let stats = allocator.aggregate_statistics();
        assert!(stats.allocation_count >= 5);
        assert!(stats.bytes_allocated >= 128 * 5);

        // Clean up
        for ptr in ptrs {
            unsafe {
                allocator
                    .deallocate(ptr, layout)
                    .expect("deallocation should succeed");
            }
        }
    }

    #[test]
    fn test_garbage_collection() {
        let base = NumericalArrayAllocator::new();
        let config = ParallelAllocatorConfig {
            max_block_age: Duration::from_millis(1), // Very short for testing
            ..Default::default()
        };
        let allocator = ParallelAllocator::new(base, config);

        let layout =
            Layout::from_size_align(64, 8).expect("layout with size 64 and align 8 is valid");

        // Allocate and deallocate to create cached blocks
        for _ in 0..3 {
            let ptr = allocator
                .allocate(layout)
                .expect("allocation should succeed");
            unsafe {
                allocator
                    .deallocate(ptr, layout)
                    .expect("deallocation should succeed");
            }
        }

        let initial_cached = allocator.total_cached_blocks();
        assert!(initial_cached > 0);

        // Wait for blocks to age
        std::thread::sleep(Duration::from_millis(10));

        // Trigger garbage collection
        allocator
            .garbage_collect_all()
            .expect("garbage collection should succeed");

        // Should have fewer cached blocks
        let final_cached = allocator.total_cached_blocks();
        assert!(final_cached <= initial_cached);
    }

    #[test]
    fn test_thread_local_allocator() {
        let config = ParallelAllocatorConfig::default();
        let tl_allocator = ThreadLocalAllocator::new(config);

        // Initialize for current thread
        let base = NumericalArrayAllocator::new();
        tl_allocator
            .initialize_current_thread(base)
            .expect("thread-local initialization should succeed");

        let layout =
            Layout::from_size_align(256, 8).expect("layout with size 256 and align 8 is valid");
        let ptr = tl_allocator
            .allocate(layout)
            .expect("allocation should succeed");

        unsafe {
            tl_allocator
                .deallocate(ptr, layout)
                .expect("deallocation should succeed");
        }

        let stats = tl_allocator
            .current_thread_statistics()
            .expect("thread-local stats should be available");
        assert_eq!(stats.allocation_count, 1);
        assert_eq!(stats.deallocation_count, 1);
    }

    #[test]
    fn test_force_cleanup() {
        let base = NumericalArrayAllocator::new();
        let config = ParallelAllocatorConfig::default();
        let allocator = ParallelAllocator::new(base, config);

        let layout =
            Layout::from_size_align(64, 8).expect("layout with size 64 and align 8 is valid");

        // Create some cached blocks
        for _ in 0..3 {
            let ptr = allocator
                .allocate(layout)
                .expect("allocation should succeed");
            unsafe {
                allocator
                    .deallocate(ptr, layout)
                    .expect("deallocation should succeed");
            }
        }

        assert!(allocator.total_cached_blocks() > 0);

        // Force cleanup
        allocator
            .force_cleanup()
            .expect("force cleanup should succeed");

        // Should have no cached blocks
        assert_eq!(allocator.total_cached_blocks(), 0);
    }

    #[test]
    fn test_reallocation() {
        let base = NumericalArrayAllocator::new();
        let config = ParallelAllocatorConfig::default();
        let allocator = ParallelAllocator::new(base, config);

        let old_layout =
            Layout::from_size_align(64, 8).expect("layout with size 64 and align 8 is valid");
        let new_layout =
            Layout::from_size_align(128, 8).expect("layout with size 128 and align 8 is valid");

        let ptr = allocator
            .allocate(old_layout)
            .expect("allocation should succeed");

        unsafe {
            let new_ptr = allocator
                .reallocate(ptr, old_layout, new_layout)
                .expect("reallocation should succeed");
            allocator
                .deallocate(new_ptr, new_layout)
                .expect("deallocation should succeed");
        }
    }

    #[test]
    fn test_multithreaded_allocation() {
        let base = NumericalArrayAllocator::new();
        let config = ParallelAllocatorConfig::default();
        let allocator = Arc::new(ParallelAllocator::new(base, config));

        let mut handles = Vec::new();

        for _ in 0..4 {
            let allocator_clone = Arc::clone(&allocator);
            let handle = std::thread::spawn(move || {
                let layout = Layout::from_size_align(128, 8)
                    .expect("layout with size 128 and align 8 is valid");

                for _ in 0..10 {
                    let ptr = allocator_clone
                        .allocate(layout)
                        .expect("allocation should succeed");
                    unsafe {
                        allocator_clone
                            .deallocate(ptr, layout)
                            .expect("deallocation should succeed");
                    }
                }
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().expect("thread should join successfully");
        }

        let stats = allocator.aggregate_statistics();
        assert!(stats.allocation_count >= 40);
    }
}