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
//! GPU Memory Management
//!
//! This module provides GPU memory pooling, buffer reuse, and optimized memory transfers.
//! It includes garbage collection for unused buffers and strategies to minimize CPU-GPU
//! data transfer overhead.
//!
//! ## Features
//!
//! - **Memory Pooling**: Reuse GPU buffers to reduce allocation overhead
//! - **Garbage Collection**: Automatic cleanup of unused buffers
//! - **Transfer Optimization**: Batch transfers and asynchronous operations
//! - **Memory Tracking**: Monitor GPU memory usage and availability
//!
//! ## Example
//!
//! ```rust,ignore
//! use numrs2::gpu::memory::{GpuMemoryPool, TransferStrategy};
//!
//! # #[cfg(feature = "gpu")]
//! # fn example() -> numrs2::error::Result<()> {
//! let context = numrs2::gpu::new_context()?;
//! let mut pool = GpuMemoryPool::new(context.clone());
//!
//! // Allocate a buffer from the pool
//! let buffer = pool.allocate(1024, wgpu::BufferUsages::STORAGE)?;
//!
//! // Buffer is automatically returned to pool when dropped
//! drop(buffer);
//!
//! // Run garbage collection to free unused buffers
//! pool.collect_garbage(0.8); // Keep 80% of recent buffers
//! # Ok(())
//! # }
//! ```

use crate::error::{MemoryError, NumRs2Error, OperationContext, Result};
use crate::gpu::context::GpuContextRef;
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

/// Default size threshold for large transfers (16MB)
const LARGE_TRANSFER_THRESHOLD: u64 = 16 * 1024 * 1024;

/// Default maximum pool size per buffer size (100 buffers)
const DEFAULT_MAX_POOL_SIZE: usize = 100;

/// Default buffer expiration time (5 minutes)
const DEFAULT_BUFFER_EXPIRATION: Duration = Duration::from_secs(300);

/// GPU memory pool for buffer reuse
///
/// The memory pool maintains separate pools for different buffer sizes and usage types,
/// allowing efficient reuse without fragmentation. Buffers are automatically returned
/// to the pool when dropped.
///
/// CACHE ALIGNMENT: Aligned to 64-byte cache lines to prevent false sharing when
/// multiple threads access the pool concurrently. The `Arc<Mutex<BufferPools>>` is a
/// hot synchronization point, and proper alignment ensures the mutex and its data
/// occupy separate cache lines from other thread-local data, reducing cache coherency
/// traffic and improving parallel GPU memory allocation performance.
#[repr(align(64))]
pub struct GpuMemoryPool {
    /// Reference to the GPU context
    context: GpuContextRef,
    /// Pooled buffers organized by size and usage
    pools: Arc<Mutex<BufferPools>>,
    /// Configuration for the memory pool
    config: PoolConfig,
}

/// Configuration for the memory pool
#[derive(Debug, Clone)]
pub struct PoolConfig {
    /// Maximum number of buffers to keep per size/usage combination
    pub max_pool_size: usize,
    /// Time after which unused buffers expire
    pub buffer_expiration: Duration,
    /// Whether to enable automatic garbage collection
    pub auto_gc: bool,
    /// Minimum retention rate during garbage collection (0.0 to 1.0)
    pub gc_retention_rate: f32,
}

impl Default for PoolConfig {
    fn default() -> Self {
        Self {
            max_pool_size: DEFAULT_MAX_POOL_SIZE,
            buffer_expiration: DEFAULT_BUFFER_EXPIRATION,
            auto_gc: true,
            gc_retention_rate: 0.8,
        }
    }
}

/// Storage for pooled buffers
struct BufferPools {
    /// Map from (size, usage_bits) to queue of available buffers
    pools: HashMap<(u64, u32), VecDeque<PooledBuffer>>,
    /// Total number of buffers in all pools
    total_buffers: usize,
    /// Total bytes allocated across all pools
    total_bytes: u64,
}

/// A buffer stored in the pool with metadata
struct PooledBuffer {
    /// The actual GPU buffer
    buffer: wgpu::Buffer,
    /// Size of the buffer in bytes
    size: u64,
    /// When this buffer was last used
    last_used: Instant,
}

impl GpuMemoryPool {
    /// Creates a new GPU memory pool with default configuration
    pub fn new(context: GpuContextRef) -> Self {
        Self::with_config(context, PoolConfig::default())
    }

    /// Creates a new GPU memory pool with custom configuration
    pub fn with_config(context: GpuContextRef, config: PoolConfig) -> Self {
        Self {
            context,
            pools: Arc::new(Mutex::new(BufferPools {
                pools: HashMap::new(),
                total_buffers: 0,
                total_bytes: 0,
            })),
            config,
        }
    }

    /// Allocates a buffer from the pool or creates a new one
    ///
    /// # Arguments
    ///
    /// * `size` - Size of the buffer in bytes
    /// * `usage` - Buffer usage flags
    ///
    /// # Returns
    ///
    /// A managed buffer that will be returned to the pool when dropped
    pub fn allocate(&mut self, size: u64, usage: wgpu::BufferUsages) -> Result<ManagedBuffer> {
        let usage_bits = usage.bits();
        let key = (size, usage_bits);

        // Try to get a buffer from the pool
        let buffer = {
            let mut pools = self.pools.lock().map_err(|e| {
                NumRs2Error::from(MemoryError::gpu_memory_error(
                    &format!("Failed to lock buffer pool: {}", e),
                    None,
                ))
            })?;

            if let Some(pool) = pools.pools.get_mut(&key) {
                if let Some(mut pooled) = pool.pop_front() {
                    pooled.last_used = Instant::now();
                    pools.total_buffers = pools.total_buffers.saturating_sub(1);
                    pools.total_bytes = pools.total_bytes.saturating_sub(size);
                    Some(pooled.buffer)
                } else {
                    None
                }
            } else {
                None
            }
        };

        // If no buffer available in pool, create a new one
        let buffer = if let Some(buf) = buffer {
            buf
        } else {
            self.context.create_empty_buffer(size, usage)
        };

        Ok(ManagedBuffer {
            buffer: Some(buffer),
            size,
            usage_bits,
            pool: Arc::clone(&self.pools),
            returned: false,
        })
    }

    /// Runs garbage collection to free unused buffers
    ///
    /// # Arguments
    ///
    /// * `retention_rate` - Fraction of recent buffers to keep (0.0 to 1.0)
    ///
    /// # Returns
    ///
    /// The number of buffers freed and total bytes freed
    pub fn collect_garbage(&mut self, retention_rate: f32) -> Result<(usize, u64)> {
        let retention_rate = retention_rate.clamp(0.0, 1.0);
        let cutoff_time = Instant::now() - self.config.buffer_expiration;

        let mut pools = self.pools.lock().map_err(|e| {
            NumRs2Error::from(MemoryError::gpu_memory_error(
                &format!("Failed to lock buffer pool during GC: {}", e),
                None,
            ))
        })?;

        let mut freed_buffers = 0;
        let mut freed_bytes = 0u64;

        // Process each pool
        for pool in pools.pools.values_mut() {
            let original_len = pool.len();

            // Calculate how many buffers to keep
            let keep_count = ((original_len as f32) * retention_rate).ceil() as usize;

            // Collect indices to remove
            let mut kept = 0;
            pool.retain(|pooled| {
                let should_keep = pooled.last_used > cutoff_time && kept < keep_count;
                if should_keep {
                    kept += 1;
                    true
                } else {
                    freed_buffers += 1;
                    freed_bytes += pooled.size;
                    false
                }
            });
        }

        // Update totals
        pools.total_buffers = pools.total_buffers.saturating_sub(freed_buffers);
        pools.total_bytes = pools.total_bytes.saturating_sub(freed_bytes);

        // Remove empty pools
        pools.pools.retain(|_, pool| !pool.is_empty());

        Ok((freed_buffers, freed_bytes))
    }

    /// Returns the current pool statistics
    pub fn statistics(&self) -> Result<PoolStatistics> {
        let pools = self.pools.lock().map_err(|e| {
            NumRs2Error::from(MemoryError::gpu_memory_error(
                &format!("Failed to lock buffer pool: {}", e),
                None,
            ))
        })?;

        Ok(PoolStatistics {
            total_buffers: pools.total_buffers,
            total_bytes: pools.total_bytes,
            pool_count: pools.pools.len(),
        })
    }

    /// Clears all buffers from the pool
    pub fn clear(&mut self) -> Result<()> {
        let mut pools = self.pools.lock().map_err(|e| {
            NumRs2Error::from(MemoryError::gpu_memory_error(
                &format!("Failed to lock buffer pool during clear: {}", e),
                None,
            ))
        })?;

        pools.pools.clear();
        pools.total_buffers = 0;
        pools.total_bytes = 0;

        Ok(())
    }
}

/// Statistics about the memory pool
#[derive(Debug, Clone, Copy)]
pub struct PoolStatistics {
    /// Total number of buffers in the pool
    pub total_buffers: usize,
    /// Total bytes allocated in the pool
    pub total_bytes: u64,
    /// Number of different pool types
    pub pool_count: usize,
}

/// A managed GPU buffer that returns to the pool when dropped
pub struct ManagedBuffer {
    buffer: Option<wgpu::Buffer>,
    size: u64,
    usage_bits: u32,
    pool: Arc<Mutex<BufferPools>>,
    returned: bool,
}

impl ManagedBuffer {
    /// Gets a reference to the underlying buffer
    ///
    /// # Panics
    ///
    /// Panics if the buffer has already been returned to the pool
    pub fn buffer(&self) -> &wgpu::Buffer {
        self.buffer
            .as_ref()
            .expect("Buffer has been returned to pool")
    }

    /// Gets the size of the buffer in bytes
    pub fn size(&self) -> u64 {
        self.size
    }

    /// Manually returns the buffer to the pool
    pub fn return_to_pool(&mut self) {
        if !self.returned {
            self.returned = true;

            // Take the buffer out of the Option
            if let Some(buffer) = self.buffer.take() {
                // Try to return buffer to pool (ignore errors on drop)
                if let Ok(mut pools) = self.pool.lock() {
                    let key = (self.size, self.usage_bits);
                    let pool = pools.pools.entry(key).or_insert_with(VecDeque::new);

                    pool.push_back(PooledBuffer {
                        buffer,
                        size: self.size,
                        last_used: Instant::now(),
                    });

                    pools.total_buffers += 1;
                    pools.total_bytes += self.size;
                }
            }
        }
    }
}

impl Drop for ManagedBuffer {
    fn drop(&mut self) {
        self.return_to_pool();
    }
}

/// Strategy for transferring data between CPU and GPU
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransferStrategy {
    /// Transfer immediately (synchronous)
    Immediate,
    /// Batch multiple transfers together
    Batched,
    /// Use asynchronous transfer
    Async,
}

/// Memory transfer optimizer for CPU-GPU data movement
///
/// CACHE ALIGNMENT: Aligned to 64-byte cache lines for optimal transfer performance.
/// The batch_queue and async_queue are frequently accessed during GPU operations,
/// and cache alignment ensures efficient batching and reduces memory latency when
/// coordinating multiple concurrent transfers.
#[repr(align(64))]
pub struct TransferOptimizer {
    context: GpuContextRef,
    strategy: TransferStrategy,
    batch_queue: Vec<PendingTransfer>,
    async_queue: Arc<Mutex<Vec<AsyncTransfer>>>,
}

struct PendingTransfer {
    source: Vec<u8>,
    destination: wgpu::Buffer,
    offset: u64,
}

/// Asynchronous transfer operation
struct AsyncTransfer {
    id: u64,
    size: u64,
    submitted: std::time::Instant,
    completed: bool,
}

impl TransferOptimizer {
    /// Creates a new transfer optimizer
    pub fn new(context: GpuContextRef, strategy: TransferStrategy) -> Self {
        Self {
            context,
            strategy,
            batch_queue: Vec::new(),
            async_queue: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Determines if a transfer is considered large
    pub fn is_large_transfer(size: u64) -> bool {
        size >= LARGE_TRANSFER_THRESHOLD
    }

    /// Queues a data transfer to GPU
    pub fn queue_transfer<T: bytemuck::Pod>(
        &mut self,
        data: &[T],
        buffer: &wgpu::Buffer,
    ) -> Result<()> {
        self.queue_transfer_with_offset(data, buffer, 0)
    }

    /// Queues a data transfer to GPU with offset
    pub fn queue_transfer_with_offset<T: bytemuck::Pod>(
        &mut self,
        data: &[T],
        buffer: &wgpu::Buffer,
        offset: u64,
    ) -> Result<()> {
        let byte_data = bytemuck::cast_slice(data);

        match self.strategy {
            TransferStrategy::Immediate => {
                self.context.queue().write_buffer(buffer, offset, byte_data);
                Ok(())
            }
            TransferStrategy::Batched => {
                // For batched transfers, store the transfer for later
                self.batch_queue.push(PendingTransfer {
                    source: byte_data.to_vec(),
                    destination: buffer.clone(),
                    offset,
                });
                Ok(())
            }
            TransferStrategy::Async => {
                // For async, queue the transfer and track it
                let transfer_id = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .map_err(|e| {
                        NumRs2Error::from(MemoryError::gpu_memory_error(
                            &format!("Failed to generate transfer ID: {}", e),
                            None,
                        ))
                    })?
                    .as_nanos() as u64;

                self.context.queue().write_buffer(buffer, offset, byte_data);

                let mut async_queue = self.async_queue.lock().map_err(|e| {
                    NumRs2Error::from(MemoryError::gpu_memory_error(
                        &format!("Failed to lock async queue: {}", e),
                        None,
                    ))
                })?;

                async_queue.push(AsyncTransfer {
                    id: transfer_id,
                    size: byte_data.len() as u64,
                    submitted: Instant::now(),
                    completed: false,
                });

                Ok(())
            }
        }
    }

    /// Flushes all pending batched transfers
    pub fn flush(&mut self) -> Result<()> {
        if !self.batch_queue.is_empty() {
            // Process all batched transfers
            for transfer in self.batch_queue.drain(..) {
                self.context.queue().write_buffer(
                    &transfer.destination,
                    transfer.offset,
                    &transfer.source,
                );
            }
        }
        Ok(())
    }

    /// Gets the number of pending async transfers
    pub fn pending_transfers(&self) -> Result<usize> {
        let async_queue = self.async_queue.lock().map_err(|e| {
            NumRs2Error::from(MemoryError::gpu_memory_error(
                &format!("Failed to lock async queue: {}", e),
                None,
            ))
        })?;

        Ok(async_queue.iter().filter(|t| !t.completed).count())
    }

    /// Waits for all async transfers to complete
    pub fn wait_for_completion(&mut self) -> Result<()> {
        // Poll the device to ensure all transfers are complete
        self.context
            .device()
            .poll(wgpu::PollType::wait_indefinitely())
            .map_err(|e| {
                NumRs2Error::from(MemoryError::gpu_memory_error(
                    &format!("Failed to poll device: {:?}", e),
                    None,
                ))
            })?;

        let mut async_queue = self.async_queue.lock().map_err(|e| {
            NumRs2Error::from(MemoryError::gpu_memory_error(
                &format!("Failed to lock async queue: {}", e),
                None,
            ))
        })?;

        // Mark all transfers as completed
        for transfer in async_queue.iter_mut() {
            transfer.completed = true;
        }

        Ok(())
    }

    /// Clears completed async transfers from the queue
    pub fn clear_completed(&mut self) -> Result<usize> {
        let mut async_queue = self.async_queue.lock().map_err(|e| {
            NumRs2Error::from(MemoryError::gpu_memory_error(
                &format!("Failed to lock async queue: {}", e),
                None,
            ))
        })?;

        let before_count = async_queue.len();
        async_queue.retain(|t| !t.completed);

        Ok(before_count - async_queue.len())
    }

    /// Gets the current transfer strategy
    pub fn strategy(&self) -> TransferStrategy {
        self.strategy
    }

    /// Sets a new transfer strategy
    pub fn set_strategy(&mut self, strategy: TransferStrategy) {
        self.strategy = strategy;
    }
}

/// Double buffer for streaming GPU operations
///
/// Maintains two buffers and alternates between them to allow overlapping
/// compute and data transfer operations.
pub struct DoubleBuffer {
    context: GpuContextRef,
    buffers: [wgpu::Buffer; 2],
    current_index: usize,
    size: u64,
    usage: wgpu::BufferUsages,
}

impl DoubleBuffer {
    /// Creates a new double buffer
    pub fn new(context: GpuContextRef, size: u64, usage: wgpu::BufferUsages) -> Self {
        let buffer_a = context.create_empty_buffer(size, usage);
        let buffer_b = context.create_empty_buffer(size, usage);

        Self {
            context,
            buffers: [buffer_a, buffer_b],
            current_index: 0,
            size,
            usage,
        }
    }

    /// Gets the current buffer
    pub fn current(&self) -> &wgpu::Buffer {
        &self.buffers[self.current_index]
    }

    /// Gets the next buffer (for prefetching)
    pub fn next(&self) -> &wgpu::Buffer {
        &self.buffers[1 - self.current_index]
    }

    /// Swaps the buffers
    pub fn swap(&mut self) {
        self.current_index = 1 - self.current_index;
    }

    /// Gets the buffer size
    pub fn size(&self) -> u64 {
        self.size
    }

    /// Writes data to the current buffer
    pub fn write_current<T: bytemuck::Pod>(&self, data: &[T]) {
        self.context
            .queue()
            .write_buffer(self.current(), 0, bytemuck::cast_slice(data));
    }

    /// Writes data to the next buffer (for prefetching)
    pub fn write_next<T: bytemuck::Pod>(&self, data: &[T]) {
        self.context
            .queue()
            .write_buffer(self.next(), 0, bytemuck::cast_slice(data));
    }
}

/// Buffer aliasing manager for memory-efficient operations
///
/// Allows multiple GPU arrays to share the same underlying buffer when appropriate,
/// reducing memory usage and improving cache locality.
pub struct BufferAliasManager {
    context: GpuContextRef,
    aliases: Arc<Mutex<HashMap<u64, Vec<BufferAlias>>>>,
}

struct BufferAlias {
    buffer: wgpu::Buffer,
    offset: u64,
    size: u64,
    usage: wgpu::BufferUsages,
    ref_count: usize,
}

impl BufferAliasManager {
    /// Creates a new buffer alias manager
    pub fn new(context: GpuContextRef) -> Self {
        Self {
            context,
            aliases: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Creates or reuses a buffer with aliasing
    pub fn get_or_create_buffer(
        &mut self,
        size: u64,
        usage: wgpu::BufferUsages,
    ) -> Result<wgpu::Buffer> {
        let key = size;

        let mut aliases = self.aliases.lock().map_err(|e| {
            NumRs2Error::from(MemoryError::gpu_memory_error(
                &format!("Failed to lock alias manager: {}", e),
                None,
            ))
        })?;

        // Try to find an existing alias
        if let Some(alias_list) = aliases.get_mut(&key) {
            for alias in alias_list.iter_mut() {
                if alias.usage == usage {
                    alias.ref_count += 1;
                    return Ok(alias.buffer.clone());
                }
            }
        }

        // Create new buffer
        let buffer = self.context.create_empty_buffer(size, usage);

        // Add to aliases
        let alias = BufferAlias {
            buffer: buffer.clone(),
            offset: 0,
            size,
            usage,
            ref_count: 1,
        };

        aliases.entry(key).or_insert_with(Vec::new).push(alias);

        Ok(buffer)
    }

    /// Releases a buffer alias
    pub fn release_buffer(&mut self, size: u64, usage: wgpu::BufferUsages) -> Result<()> {
        let key = size;

        let mut aliases = self.aliases.lock().map_err(|e| {
            NumRs2Error::from(MemoryError::gpu_memory_error(
                &format!("Failed to lock alias manager: {}", e),
                None,
            ))
        })?;

        if let Some(alias_list) = aliases.get_mut(&key) {
            for alias in alias_list.iter_mut() {
                if alias.usage == usage && alias.ref_count > 0 {
                    alias.ref_count -= 1;
                    break;
                }
            }

            // Remove aliases with zero ref count
            alias_list.retain(|a| a.ref_count > 0);

            if alias_list.is_empty() {
                aliases.remove(&key);
            }
        }

        Ok(())
    }

    /// Gets statistics about buffer aliasing
    pub fn statistics(&self) -> Result<AliasStatistics> {
        let aliases = self.aliases.lock().map_err(|e| {
            NumRs2Error::from(MemoryError::gpu_memory_error(
                &format!("Failed to lock alias manager: {}", e),
                None,
            ))
        })?;

        let total_aliases: usize = aliases.values().map(|v| v.len()).sum();
        let total_refs: usize = aliases
            .values()
            .flat_map(|v| v.iter().map(|a| a.ref_count))
            .sum();

        Ok(AliasStatistics {
            total_aliases,
            total_references: total_refs,
            buffer_sizes: aliases.len(),
        })
    }

    /// Clears all buffer aliases
    pub fn clear(&mut self) -> Result<()> {
        let mut aliases = self.aliases.lock().map_err(|e| {
            NumRs2Error::from(MemoryError::gpu_memory_error(
                &format!("Failed to lock alias manager: {}", e),
                None,
            ))
        })?;

        aliases.clear();
        Ok(())
    }
}

/// Statistics about buffer aliasing
#[derive(Debug, Clone, Copy)]
pub struct AliasStatistics {
    /// Total number of buffer aliases
    pub total_aliases: usize,
    /// Total number of references to aliased buffers
    pub total_references: usize,
    /// Number of different buffer sizes
    pub buffer_sizes: usize,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_pool_config_default() {
        let config = PoolConfig::default();
        assert_eq!(config.max_pool_size, DEFAULT_MAX_POOL_SIZE);
        assert!(config.auto_gc);
        assert_eq!(config.gc_retention_rate, 0.8);
    }

    #[test]
    fn test_is_large_transfer() {
        assert!(!TransferOptimizer::is_large_transfer(1024));
        assert!(!TransferOptimizer::is_large_transfer(
            LARGE_TRANSFER_THRESHOLD - 1
        ));
        assert!(TransferOptimizer::is_large_transfer(
            LARGE_TRANSFER_THRESHOLD
        ));
        assert!(TransferOptimizer::is_large_transfer(
            LARGE_TRANSFER_THRESHOLD + 1
        ));
    }

    #[test]
    fn test_transfer_strategy() {
        assert_ne!(TransferStrategy::Immediate, TransferStrategy::Batched);
        assert_ne!(TransferStrategy::Immediate, TransferStrategy::Async);
        assert_ne!(TransferStrategy::Batched, TransferStrategy::Async);
    }
}