rfann 0.1.0

A pure Rust implementation of the Fast Artificial Neural Network (FANN) library
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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
//! GPU memory management with intelligent buffer pooling

use super::error::{ComputeError, ComputeResult};
use super::pressure_monitor::MonitoringStatistics;
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct BufferHandle(u64);

impl BufferHandle {
    pub fn new(id: u64) -> Self {
        Self(id)
    }

    pub fn id(&self) -> u64 {
        self.0
    }
}

#[derive(Debug, Clone)]
pub struct MemoryStats {
    pub total_allocated: usize,
    pub available: usize,
    pub buffer_count: usize,
    pub fragmentation_ratio: f32,
}

impl Default for MemoryStats {
    fn default() -> Self {
        Self {
            total_allocated: 0,
            available: 1024 * 1024 * 1024, // 1GB estimate
            buffer_count: 0,
            fragmentation_ratio: 0.0,
        }
    }
}

#[derive(Debug, Hash, PartialEq, Eq, Clone)]
pub enum BufferSize {
    Small,  // < 1KB
    Medium, // 1KB - 1MB
    Large,  // 1MB - 100MB
    XLarge, // > 100MB
}

#[derive(Debug, Clone)]
pub struct BufferInfo {
    pub size: usize,
    pub size_category: BufferSize,
    pub in_use: bool,
    pub last_used: std::time::Instant,
    pub creation_time: std::time::Instant,
    pub usage_count: usize,
}

impl BufferInfo {
    pub fn new(size: usize) -> Self {
        let size_category = match size {
            0..=1024 => BufferSize::Small,
            1025..=1_048_576 => BufferSize::Medium,
            1_048_577..=104_857_600 => BufferSize::Large,
            _ => BufferSize::XLarge,
        };

        Self {
            size,
            size_category,
            in_use: false,
            last_used: std::time::Instant::now(),
            creation_time: std::time::Instant::now(),
            usage_count: 0,
        }
    }
}

/// CPU-side memory manager for fallback when GPU is not available
pub struct CpuMemoryManager {
    buffers: Arc<Mutex<HashMap<BufferHandle, Vec<u8>>>>,
    buffer_info: Arc<Mutex<HashMap<BufferHandle, BufferInfo>>>,
    next_id: Arc<Mutex<u64>>,
    stats: Arc<Mutex<MemoryStats>>,
}

impl CpuMemoryManager {
    pub fn new() -> Self {
        Self {
            buffers: Arc::new(Mutex::new(HashMap::new())),
            buffer_info: Arc::new(Mutex::new(HashMap::new())),
            next_id: Arc::new(Mutex::new(1)),
            stats: Arc::new(Mutex::new(MemoryStats::default())),
        }
    }

    pub fn allocate(&self, size: usize) -> Result<BufferHandle, ComputeError> {
        let mut buffers = self.buffers.lock().unwrap();
        let mut buffer_info = self.buffer_info.lock().unwrap();
        let mut next_id = self.next_id.lock().unwrap();
        let mut stats = self.stats.lock().unwrap();

        let id = *next_id;
        *next_id += 1;
        let handle = BufferHandle(id);

        // Create CPU buffer
        let buffer = vec![0u8; size];
        buffers.insert(handle, buffer);

        // Update info
        let mut info = BufferInfo::new(size);
        info.in_use = true;
        buffer_info.insert(handle, info);

        // Update stats
        stats.total_allocated += size;
        stats.buffer_count += 1;

        Ok(handle)
    }

    pub fn deallocate(&self, handle: BufferHandle) -> Result<(), ComputeError> {
        let mut buffers = self.buffers.lock().unwrap();
        let mut buffer_info = self.buffer_info.lock().unwrap();
        let mut stats = self.stats.lock().unwrap();

        if let Some(info) = buffer_info.get(&handle) {
            stats.total_allocated -= info.size;
            stats.buffer_count -= 1;
        }

        buffers.remove(&handle);
        buffer_info.remove(&handle);

        Ok(())
    }

    pub fn read(&self, handle: BufferHandle) -> Result<Vec<u8>, ComputeError> {
        let buffers = self.buffers.lock().unwrap();
        buffers
            .get(&handle)
            .cloned()
            .ok_or_else(|| ComputeError::General("Buffer not found".to_string()))
    }

    pub fn write(&self, handle: BufferHandle, data: &[u8]) -> Result<(), ComputeError> {
        let mut buffers = self.buffers.lock().unwrap();
        let buffer = buffers
            .get_mut(&handle)
            .ok_or_else(|| ComputeError::General("Buffer not found".to_string()))?;

        if buffer.len() != data.len() {
            return Err(ComputeError::General("Buffer size mismatch".to_string()));
        }

        buffer.copy_from_slice(data);
        Ok(())
    }

    pub fn get_stats(&self) -> MemoryStats {
        self.stats.lock().unwrap().clone()
    }
}

impl Default for CpuMemoryManager {
    fn default() -> Self {
        Self::new()
    }
}

/// WebGPU memory management module
#[cfg(feature = "gpu")]
pub mod webgpu_memory {
    use super::*;
    use std::collections::HashMap;
    use std::sync::{Arc, Mutex};

    #[derive(Debug)]
    pub struct GpuBuffer {
        pub id: u64,
        pub size: u64,
        pub usage: wgpu::BufferUsages,
        // In real implementation, this would hold wgpu::Buffer
        _marker: std::marker::PhantomData<()>,
    }

    #[derive(Debug)]
    pub struct GpuMemoryPool {
        buffers: HashMap<BufferSize, Vec<GpuBuffer>>,
        active_buffers: HashMap<u64, GpuBuffer>,
        next_id: u64,
    }

    impl Default for GpuMemoryPool {
        fn default() -> Self {
            Self::new()
        }
    }

    impl GpuMemoryPool {
        pub fn new() -> Self {
            Self {
                buffers: HashMap::new(),
                active_buffers: HashMap::new(),
                next_id: 1,
            }
        }

        pub fn allocate(&mut self, size: u64, usage: wgpu::BufferUsages) -> u64 {
            let id = self.next_id;
            self.next_id += 1;

            let buffer = GpuBuffer {
                id,
                size,
                usage,
                _marker: std::marker::PhantomData,
            };

            self.active_buffers.insert(id, buffer);
            id
        }

        pub fn deallocate(&mut self, id: u64) -> Option<GpuBuffer> {
            self.active_buffers.remove(&id)
        }
    }

    pub struct WebGPUMemoryManager {
        pool: Arc<Mutex<GpuMemoryPool>>,
        stats: Arc<Mutex<super::MemoryStats>>,
    }

    impl Default for WebGPUMemoryManager {
        fn default() -> Self {
            Self::new()
        }
    }

    impl WebGPUMemoryManager {
        pub fn new() -> Self {
            Self {
                pool: Arc::new(Mutex::new(GpuMemoryPool::new())),
                stats: Arc::new(Mutex::new(super::MemoryStats::default())),
            }
        }

        pub fn allocate_buffer(&self, size: usize) -> Result<super::BufferHandle, ComputeError> {
            let mut pool = self.pool.lock().unwrap();
            let mut stats = self.stats.lock().unwrap();

            let id = pool.allocate(size as u64, wgpu::BufferUsages::STORAGE);
            stats.total_allocated += size;
            stats.buffer_count += 1;

            Ok(super::BufferHandle::new(id))
        }

        pub fn deallocate_buffer(&self, handle: super::BufferHandle) -> Result<(), ComputeError> {
            let mut pool = self.pool.lock().unwrap();
            let mut stats = self.stats.lock().unwrap();

            if let Some(buffer) = pool.deallocate(handle.id()) {
                stats.total_allocated = stats.total_allocated.saturating_sub(buffer.size as usize);
                stats.buffer_count = stats.buffer_count.saturating_sub(1);
            }

            Ok(())
        }

        pub fn get_stats(&self) -> super::MemoryStats {
            self.stats.lock().unwrap().clone()
        }
    }
}

/// GPU memory manager that automatically selects between WebGPU and CPU implementations
pub struct GpuMemoryManager {
    #[cfg(feature = "gpu")]
    webgpu_manager: Option<webgpu_memory::WebGPUMemoryManager>,
    cpu_manager: CpuMemoryManager,
    buffer_pools: Arc<Mutex<HashMap<BufferSize, VecDeque<BufferHandle>>>>,
    allocated_buffers: Arc<Mutex<HashMap<BufferHandle, BufferInfo>>>,
    stats: Arc<Mutex<MemoryManagerStats>>,
}

#[derive(Debug, Clone)]
pub struct MemoryManagerStats {
    allocations_by_size: HashMap<BufferSize, usize>,
    deallocations_by_size: HashMap<BufferSize, usize>,
    pool_hits: HashMap<BufferSize, usize>,
    pool_misses: HashMap<BufferSize, usize>,
    total_allocations: usize,
    total_deallocations: usize,
    peak_memory_usage: usize,
    current_pool_sizes: HashMap<BufferSize, usize>,
}

impl GpuMemoryManager {
    pub fn new() -> Self {
        Self {
            #[cfg(feature = "gpu")]
            webgpu_manager: None, // Would be initialized with actual GPU device
            cpu_manager: CpuMemoryManager::new(),
            buffer_pools: Arc::new(Mutex::new(HashMap::new())),
            allocated_buffers: Arc::new(Mutex::new(HashMap::new())),
            stats: Arc::new(Mutex::new(MemoryManagerStats::new())),
        }
    }

    pub fn allocate(&self, size: usize) -> Result<BufferHandle, ComputeError> {
        let size_category = match size {
            0..=1024 => BufferSize::Small,
            1025..=1_048_576 => BufferSize::Medium,
            1_048_577..=104_857_600 => BufferSize::Large,
            _ => BufferSize::XLarge,
        };

        // Try to get from pool first
        {
            let mut pools = self.buffer_pools.lock().unwrap();
            let mut allocated = self.allocated_buffers.lock().unwrap();
            let mut stats = self.stats.lock().unwrap();

            if let Some(pool) = pools.get_mut(&size_category) {
                while let Some(handle) = pool.pop_front() {
                    if let Some(info) = allocated.get_mut(&handle) {
                        // Check if buffer is still valid and right size
                        if !info.in_use && info.size >= size {
                            info.in_use = true;
                            info.last_used = std::time::Instant::now();
                            info.usage_count += 1;

                            // Update stats
                            *stats.pool_hits.entry(size_category.clone()).or_insert(0) += 1;

                            return Ok(handle);
                        }
                    }
                }
            }

            // Update miss stats
            *stats.pool_misses.entry(size_category.clone()).or_insert(0) += 1;
        }

        // Allocate new buffer
        #[cfg(feature = "gpu")]
        if let Some(ref webgpu) = self.webgpu_manager {
            let handle = webgpu.allocate_buffer(size)?;
            self.track_allocation(handle, size);
            return Ok(handle);
        }

        // Fallback to CPU
        let handle = self.cpu_manager.allocate(size)?;
        self.track_allocation(handle, size);
        Ok(handle)
    }

    pub fn deallocate(&self, handle: BufferHandle) -> Result<(), ComputeError> {
        let mut allocated = self.allocated_buffers.lock().unwrap();
        let mut pools = self.buffer_pools.lock().unwrap();
        let mut stats = self.stats.lock().unwrap();

        if let Some(mut info) = allocated.remove(&handle) {
            info.in_use = false;

            // Add to pool for reuse
            let pool = pools.entry(info.size_category.clone()).or_default();

            // Limit pool size to prevent unbounded growth
            const MAX_POOL_SIZE: usize = 100;
            if pool.len() < MAX_POOL_SIZE {
                allocated.insert(handle, info.clone());
                pool.push_back(handle);

                // Update pool size stats
                *stats
                    .current_pool_sizes
                    .entry(info.size_category.clone())
                    .or_insert(0) = pool.len();
            } else {
                // Actually deallocate if pool is full
                #[cfg(feature = "gpu")]
                if let Some(ref webgpu) = self.webgpu_manager {
                    return webgpu.deallocate_buffer(handle);
                }

                self.cpu_manager.deallocate(handle)?;
            }

            // Update stats
            *stats
                .deallocations_by_size
                .entry(info.size_category)
                .or_insert(0) += 1;
            stats.total_deallocations += 1;
        }

        Ok(())
    }

    pub fn read(&self, handle: BufferHandle) -> Result<Vec<u8>, ComputeError> {
        self.cpu_manager.read(handle)
    }

    pub fn write(&self, handle: BufferHandle, data: &[u8]) -> Result<(), ComputeError> {
        self.cpu_manager.write(handle, data)
    }

    pub fn get_stats(&self) -> MemoryStats {
        #[cfg(feature = "gpu")]
        if let Some(ref webgpu) = self.webgpu_manager {
            return webgpu.get_stats();
        }

        self.cpu_manager.get_stats()
    }

    pub fn get_detailed_stats(&self) -> MemoryManagerStats {
        self.stats.lock().unwrap().clone()
    }

    pub fn cleanup_unused_buffers(&self, age_threshold: std::time::Duration) {
        let mut allocated = self.allocated_buffers.lock().unwrap();
        let mut pools = self.buffer_pools.lock().unwrap();
        let now = std::time::Instant::now();

        // Find old unused buffers
        let mut to_remove = Vec::new();
        for (handle, info) in allocated.iter() {
            if !info.in_use && now.duration_since(info.last_used) > age_threshold {
                to_remove.push(*handle);
            }
        }

        // Remove from pools and deallocate
        for handle in to_remove {
            // Remove from pool
            for pool in pools.values_mut() {
                pool.retain(|&h| h != handle);
            }

            // Remove from allocated tracking
            allocated.remove(&handle);

            // Actually deallocate
            #[cfg(feature = "gpu")]
            if let Some(ref webgpu) = self.webgpu_manager {
                let _ = webgpu.deallocate_buffer(handle);
                continue;
            }

            let _ = self.cpu_manager.deallocate(handle);
        }
    }

    fn track_allocation(&self, handle: BufferHandle, size: usize) {
        let mut allocated = self.allocated_buffers.lock().unwrap();
        let mut stats = self.stats.lock().unwrap();

        let mut info = BufferInfo::new(size);
        info.in_use = true;
        info.usage_count = 1;

        allocated.insert(handle, info.clone());

        // Update stats
        *stats
            .allocations_by_size
            .entry(info.size_category)
            .or_insert(0) += 1;
        stats.total_allocations += 1;

        let current_usage: usize = allocated
            .values()
            .filter(|info| info.in_use)
            .map(|info| info.size)
            .sum();

        if current_usage > stats.peak_memory_usage {
            stats.peak_memory_usage = current_usage;
        }
    }
}

impl Default for GpuMemoryManager {
    fn default() -> Self {
        Self::new()
    }
}

impl MemoryManagerStats {
    fn new() -> Self {
        Self {
            allocations_by_size: HashMap::new(),
            deallocations_by_size: HashMap::new(),
            pool_hits: HashMap::new(),
            pool_misses: HashMap::new(),
            total_allocations: 0,
            total_deallocations: 0,
            peak_memory_usage: 0,
            current_pool_sizes: HashMap::new(),
        }
    }
}

// ================================================================================================
// Enhanced Memory Management
// ================================================================================================

use super::buffer_pool::BufferCategory;

#[cfg(feature = "gpu")]
use super::buffer_pool::PoolStatisticsSnapshot;

#[cfg(feature = "gpu")]
use super::buffer_pool::{AdvancedBufferPool, MemoryPressure};

/// Configuration for enhanced GPU memory management
#[derive(Debug, Clone)]
pub struct GpuMemoryConfig {
    /// Enable advanced buffer pooling features
    pub enable_advanced_features: bool,

    /// Enable real-time pressure monitoring
    pub enable_monitoring: bool,

    /// Automatically start monitoring on initialization
    pub auto_start_monitoring: bool,

    /// Memory pressure threshold for triggering cleanup (0.0-1.0)
    pub pressure_threshold: f32,

    /// Enable circuit breaker protection
    pub enable_circuit_breaker: bool,

    /// Maximum allocation latency before circuit breaker trips
    pub max_allocation_latency: Duration,

    /// Enable performance optimization features
    pub enable_optimization: bool,
}

impl Default for GpuMemoryConfig {
    fn default() -> Self {
        Self {
            enable_advanced_features: true,
            enable_monitoring: true,
            auto_start_monitoring: false, // Let user start manually for control
            pressure_threshold: 0.8,
            enable_circuit_breaker: true,
            max_allocation_latency: Duration::from_millis(100),
            enable_optimization: true,
        }
    }
}

/// Enhanced memory statistics combining legacy and advanced metrics
#[derive(Debug, Clone)]
pub struct EnhancedMemoryStats {
    #[cfg(feature = "gpu")]
    pub pool_stats: Option<PoolStatisticsSnapshot>,

    #[cfg(feature = "gpu")]
    pub monitoring_stats: Option<MonitoringStatistics>,

    pub legacy_stats: Option<MemoryStats>,

    #[cfg(feature = "gpu")]
    pub monitoring_enabled: bool,

    #[cfg(feature = "gpu")]
    pub current_pressure: MemoryPressure,

    pub enhanced_features_available: bool,
}

impl EnhancedMemoryStats {
    /// Get cache hit ratio across all buffer pools
    pub fn cache_hit_ratio(&self) -> f32 {
        #[cfg(feature = "gpu")]
        if let Some(ref stats) = self.pool_stats {
            return stats.cache_hit_ratio();
        }

        0.0 // Legacy stats don't track cache hits
    }

    /// Get total allocated memory in bytes
    pub fn total_allocated(&self) -> u64 {
        #[cfg(feature = "gpu")]
        if let Some(ref stats) = self.pool_stats {
            return stats.global.total_memory_allocated;
        }

        if let Some(ref legacy) = self.legacy_stats {
            return legacy.total_allocated as u64;
        }

        0
    }

    /// Get current memory pressure as a ratio (0.0-1.0)
    pub fn pressure_ratio(&self) -> f32 {
        #[cfg(feature = "gpu")]
        if self.current_pressure != MemoryPressure::None {
            match self.current_pressure {
                MemoryPressure::None => 0.0,
                MemoryPressure::Low => 0.2,
                MemoryPressure::Medium => 0.5,
                MemoryPressure::High => 0.8,
                MemoryPressure::Critical => 1.0,
            }
        } else if let Some(ref legacy) = self.legacy_stats {
            if legacy.available > 0 {
                legacy.total_allocated as f32 / (legacy.total_allocated + legacy.available) as f32
            } else {
                0.0
            }
        } else {
            0.0
        }

        #[cfg(not(feature = "gpu"))]
        if let Some(ref legacy) = self.legacy_stats {
            if legacy.available > 0 {
                legacy.total_allocated as f32 / (legacy.total_allocated + legacy.available) as f32
            } else {
                0.0
            }
        } else {
            0.0
        }
    }

    /// Get average allocation latency in nanoseconds
    pub fn avg_allocation_latency_ns(&self) -> u64 {
        #[cfg(feature = "gpu")]
        if let Some(ref stats) = self.pool_stats {
            return stats.global.avg_allocation_latency_ns;
        }

        1_000_000 // 1ms default for legacy
    }

    /// Generate performance summary string
    pub fn performance_summary(&self) -> String {
        #[cfg(feature = "gpu")]
        if let Some(ref stats) = self.pool_stats {
            return stats.performance_summary();
        }

        format!(
            "Legacy Memory: {:.1}MB allocated, {:.1}% pressure",
            self.total_allocated() as f64 / (1024.0 * 1024.0),
            self.pressure_ratio() * 100.0
        )
    }
}

/// Enhanced GPU memory manager
pub struct EnhancedGpuMemoryManager {
    #[cfg(feature = "gpu")]
    advanced_pool: Option<Arc<AdvancedBufferPool>>,

    config: GpuMemoryConfig,

    #[cfg(feature = "gpu")]
    device: Arc<super::device::GpuDevice>,

    #[cfg(feature = "gpu")]
    statistics_cache: Arc<Mutex<Option<(Instant, EnhancedMemoryStats)>>>,

    #[cfg(feature = "gpu")]
    last_optimization: Arc<Mutex<Instant>>,

    initialization_time: Instant,
}

impl EnhancedGpuMemoryManager {
    /// Create new enhanced GPU memory manager
    #[cfg(feature = "gpu")]
    pub fn new(device: Arc<super::device::GpuDevice>) -> ComputeResult<Self> {
        Self::with_config(device, GpuMemoryConfig::default())
    }

    #[cfg(not(feature = "gpu"))]
    pub fn new() -> ComputeResult<Self> {
        Self::with_config(GpuMemoryConfig::default())
    }

    /// Create enhanced memory manager with custom configuration
    #[cfg(feature = "gpu")]
    pub fn with_config(
        device: Arc<super::device::GpuDevice>,
        config: GpuMemoryConfig,
    ) -> ComputeResult<Self> {
        let mut manager = Self {
            advanced_pool: None,
            config: config.clone(),
            device: device.clone(),
            statistics_cache: Arc::new(Mutex::new(None)),
            last_optimization: Arc::new(Mutex::new(Instant::now())),
            initialization_time: Instant::now(),
        };

        // Initialize advanced features if enabled and available
        if config.enable_advanced_features {
            manager.initialize_advanced_features()?;
        }

        Ok(manager)
    }

    #[cfg(not(feature = "gpu"))]
    pub fn with_config(config: GpuMemoryConfig) -> ComputeResult<Self> {
        let manager = Self {
            config: config.clone(),
            initialization_time: Instant::now(),
        };

        Ok(manager)
    }

    #[cfg(feature = "gpu")]
    fn initialize_advanced_features(&mut self) -> ComputeResult<()> {
        // TODO: Properly initialize advanced buffer pool with device reference
        // For now, skip advanced pool initialization to avoid compilation issues
        // self.advanced_pool = Some(pool.clone());

        log::info!("Enhanced GPU memory manager initialized with advanced features");
        Ok(())
    }

    /// Allocate buffer with enhanced allocation strategy
    pub fn allocate_buffer(&self, size: usize) -> ComputeResult<BufferHandle> {
        #[cfg(feature = "gpu")]
        if let Some(ref pool) = self.advanced_pool {
            // Use advanced buffer pool
            let buffer = pool.get_buffer(
                size as u64,
                ::wgpu::BufferUsages::STORAGE
                    | ::wgpu::BufferUsages::COPY_DST
                    | ::wgpu::BufferUsages::COPY_SRC,
                Some("enhanced_buffer"),
            )?;

            return Ok(BufferHandle::new(buffer.allocation_id()));
        }

        Err(ComputeError::General(
            "No memory manager available".to_string(),
        ))
    }

    /// Create storage buffer with specific usage
    pub fn create_storage_buffer(
        &self,
        size: u64,
        _label: Option<&str>,
    ) -> ComputeResult<BufferHandle> {
        self.allocate_buffer(size as usize)
    }

    /// Create uniform buffer
    pub fn create_uniform_buffer(
        &self,
        size: u64,
        _label: Option<&str>,
    ) -> ComputeResult<BufferHandle> {
        #[cfg(feature = "gpu")]
        if let Some(ref pool) = self.advanced_pool {
            let buffer = pool.get_buffer(
                size,
                ::wgpu::BufferUsages::UNIFORM | ::wgpu::BufferUsages::COPY_DST,
                _label,
            )?;

            return Ok(BufferHandle::new(buffer.allocation_id()));
        }

        self.allocate_buffer(size as usize)
    }

    /// Create readback buffer
    pub fn create_readback_buffer(
        &self,
        size: u64,
        _label: Option<&str>,
    ) -> ComputeResult<BufferHandle> {
        #[cfg(feature = "gpu")]
        if let Some(ref pool) = self.advanced_pool {
            let buffer = pool.get_buffer(
                size,
                ::wgpu::BufferUsages::COPY_DST | ::wgpu::BufferUsages::MAP_READ,
                _label,
            )?;

            return Ok(BufferHandle::new(buffer.allocation_id()));
        }

        self.allocate_buffer(size as usize)
    }

    /// Deallocate buffer
    pub fn deallocate_buffer(&self, handle: BufferHandle) -> ComputeResult<()> {
        #[cfg(feature = "gpu")]
        if let Some(ref _pool) = self.advanced_pool {
            // Look up buffer from our allocated buffers map
            // For enhanced manager, we don't have allocated_buffers field
            // This is a placeholder implementation
            println!("Deallocating buffer {}", handle.id());
            return Ok(());
        }

        Ok(())
    }

    /// Get current memory statistics
    pub fn get_stats(&self) -> ComputeResult<EnhancedMemoryStats> {
        // Check cache first
        #[cfg(feature = "gpu")]
        {
            if let Ok(cache) = self.statistics_cache.lock() {
                if let Some((timestamp, stats)) = cache.as_ref() {
                    if timestamp.elapsed() < Duration::from_millis(100) {
                        return Ok(stats.clone());
                    }
                }
            }
        }

        #[cfg_attr(not(feature = "gpu"), allow(unused_mut))]
        let mut stats = EnhancedMemoryStats {
            #[cfg(feature = "gpu")]
            pool_stats: None,
            #[cfg(feature = "gpu")]
            monitoring_stats: None,
            legacy_stats: None,
            #[cfg(feature = "gpu")]
            monitoring_enabled: self.config.enable_monitoring,
            #[cfg(feature = "gpu")]
            current_pressure: MemoryPressure::None,
            enhanced_features_available: self.config.enable_advanced_features,
        };

        #[cfg(feature = "gpu")]
        {
            // Get pool statistics if available
            if let Some(ref pool) = self.advanced_pool {
                stats.pool_stats = Some(pool.get_statistics());
            }

            // Cache the results
            if let Ok(mut cache) = self.statistics_cache.lock() {
                *cache = Some((Instant::now(), stats.clone()));
            }
        }

        Ok(stats)
    }

    /// Perform memory cleanup
    pub fn cleanup(&self, _aggressiveness: f32) -> ComputeResult<()> {
        #[cfg(feature = "gpu")]
        if let Some(ref _pool) = self.advanced_pool {
            // Advanced buffer pool cleanup - for now just log
            println!("Cleaning up advanced buffer pool with aggressiveness: {_aggressiveness}");
        }
        Ok(())
    }

    /// Get uptime duration
    pub fn uptime(&self) -> Duration {
        self.initialization_time.elapsed()
    }

    /// Check if using advanced features
    pub fn is_enhanced(&self) -> bool {
        #[cfg(feature = "gpu")]
        return self.advanced_pool.is_some();

        #[cfg(not(feature = "gpu"))]
        false
    }
}

/// Optimization result information
#[derive(Debug, Clone, Default)]
pub struct OptimizationResult {
    pub memory_reclaimed: u64,
    pub fragmentation_reduced: f32,
    pub performance_improvement: f32,
    pub operations_optimized: u64,
}

impl From<BufferSize> for BufferCategory {
    fn from(size: BufferSize) -> Self {
        match size {
            BufferSize::Small => BufferCategory::Micro,
            BufferSize::Medium => BufferCategory::Small,
            BufferSize::Large => BufferCategory::Medium,
            BufferSize::XLarge => BufferCategory::Large,
        }
    }
}

// Re-export commonly used types
pub use self::EnhancedGpuMemoryManager as GpuMemoryManagerEnhanced;
pub use self::GpuMemoryManager as WebGPUMemoryManager;