torsh-fx 0.1.2

Graph-based model representation and transformation for ToRSh
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
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
//! Performance optimization and analysis utilities for FX graphs
//!
//! This module provides advanced performance optimization features including:
//! - Parallel graph traversal for large graphs
//! - Graph caching and memoization for repeated operations
//! - Graph compression techniques for reduced memory usage
//! - Automatic performance profiling and bottleneck detection

use crate::{FxGraph, Node, TorshResult};
use petgraph::graph::NodeIndex;
use petgraph::visit::EdgeRef;
// SCIRS2 POLICY COMPLIANCE: Use scirs2_core::parallel_ops instead of direct rayon
use scirs2_core::parallel_ops::*;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Duration, Instant};

/// Parallel graph traversal utilities for large graphs
pub struct ParallelTraversal {
    graph: Arc<FxGraph>,
    thread_pool_size: Option<usize>,
}

impl ParallelTraversal {
    /// Create a new parallel traversal instance
    pub fn new(graph: Arc<FxGraph>) -> Self {
        Self {
            graph,
            thread_pool_size: None,
        }
    }

    /// Set custom thread pool size (default: number of CPU cores)
    pub fn with_thread_pool_size(mut self, size: usize) -> Self {
        self.thread_pool_size = Some(size);
        self
    }

    /// Perform parallel topological traversal of the graph
    pub fn parallel_topological_traverse<F>(&self, visitor: F) -> TorshResult<()>
    where
        F: Fn(NodeIndex, &Node) + Send + Sync,
    {
        // Build dependency graph for topological ordering
        let dependencies = self.build_dependency_map();
        let visited = HashSet::new();
        let mut ready_nodes = Vec::new();

        // Find nodes with no dependencies
        for (idx, _) in self.graph.nodes() {
            if dependencies.get(&idx).map_or(true, |deps| deps.is_empty()) {
                ready_nodes.push(idx);
            }
        }

        let visited = Arc::new(Mutex::new(visited));
        let dependencies = Arc::new(dependencies);

        while !ready_nodes.is_empty() {
            // Process ready nodes in parallel
            ready_nodes.par_iter().for_each(|&idx| {
                if let Some(node) = self.graph.get_node(idx) {
                    visitor(idx, node);
                    visited
                        .lock()
                        .expect("lock should not be poisoned")
                        .insert(idx);
                }
            });

            // Find newly ready nodes - optimized to avoid cloning the entire visited set
            ready_nodes.clear();

            for (idx, deps) in dependencies.iter() {
                let visited_guard = visited.lock().expect("lock should not be poisoned");
                if !visited_guard.contains(idx) {
                    if deps.iter().all(|dep| visited_guard.contains(dep)) {
                        ready_nodes.push(*idx);
                    }
                }
                // Release the lock early by dropping the guard
                drop(visited_guard);
            }
        }

        Ok(())
    }

    /// Perform parallel depth-first search with work stealing
    pub fn parallel_dfs<F>(&self, start_nodes: Vec<NodeIndex>, visitor: F) -> TorshResult<()>
    where
        F: Fn(NodeIndex, &Node) + Send + Sync,
    {
        let visited = Arc::new(Mutex::new(HashSet::new()));
        let visitor = Arc::new(visitor);

        start_nodes.into_par_iter().for_each(|start| {
            self.dfs_worker(start, visited.clone(), visitor.clone());
        });

        Ok(())
    }

    /// Build a dependency map for topological traversal
    fn build_dependency_map(&self) -> HashMap<NodeIndex, Vec<NodeIndex>> {
        let mut dependencies = HashMap::new();

        for (idx, _) in self.graph.nodes() {
            dependencies.insert(idx, Vec::new());
        }

        // Build reverse dependency map
        for edge_ref in self.graph.graph.edge_references() {
            use petgraph::visit::EdgeRef;
            let target = edge_ref.target();
            let source = edge_ref.source();
            dependencies
                .get_mut(&target)
                .expect("target node should exist in dependencies map")
                .push(source);
        }

        dependencies
    }

    /// DFS worker for parallel traversal
    fn dfs_worker<F>(
        &self,
        node: NodeIndex,
        visited: Arc<Mutex<HashSet<NodeIndex>>>,
        visitor: Arc<F>,
    ) where
        F: Fn(NodeIndex, &Node) + Send + Sync,
    {
        let mut stack = vec![node];

        while let Some(current) = stack.pop() {
            let already_visited = {
                let mut v = visited.lock().expect("lock should not be poisoned");
                if v.contains(&current) {
                    true
                } else {
                    v.insert(current);
                    false
                }
            };

            if already_visited {
                continue;
            }

            if let Some(node_data) = self.graph.get_node(current) {
                visitor(current, node_data);
            }

            // Add neighbors to stack
            for edge in self.graph.graph.edges(current) {
                stack.push(edge.target());
            }
        }
    }
}

/// Graph caching and memoization system
#[derive(Debug)]
pub struct GraphCache {
    operation_cache: RwLock<HashMap<String, CachedResult>>,
    subgraph_cache: RwLock<HashMap<String, Arc<FxGraph>>>,
    cache_stats: Arc<Mutex<CacheStatistics>>,
    max_cache_size: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachedResult {
    pub result: String,
    pub computation_time: Duration,
    pub access_count: u64,
    pub last_accessed: std::time::SystemTime,
}

#[derive(Debug, Default, Clone)]
pub struct CacheStatistics {
    pub hits: u64,
    pub misses: u64,
    pub evictions: u64,
    pub total_computation_time_saved: Duration,
}

impl GraphCache {
    /// Create a new graph cache with specified maximum size
    pub fn new(max_cache_size: usize) -> Self {
        Self {
            operation_cache: RwLock::new(HashMap::new()),
            subgraph_cache: RwLock::new(HashMap::new()),
            cache_stats: Arc::new(Mutex::new(CacheStatistics::default())),
            max_cache_size,
        }
    }

    /// Get cached operation result
    pub fn get_operation(&self, key: &str) -> Option<CachedResult> {
        let mut cache = self
            .operation_cache
            .write()
            .expect("lock should not be poisoned");
        if let Some(result) = cache.get_mut(key) {
            result.access_count += 1;
            result.last_accessed = std::time::SystemTime::now();
            self.cache_stats
                .lock()
                .expect("lock should not be poisoned")
                .hits += 1;
            Some(result.clone())
        } else {
            self.cache_stats
                .lock()
                .expect("lock should not be poisoned")
                .misses += 1;
            None
        }
    }

    /// Cache operation result
    pub fn cache_operation(&self, key: String, result: String, computation_time: Duration) {
        let mut cache = self
            .operation_cache
            .write()
            .expect("lock should not be poisoned");

        // Evict oldest entries if cache is full
        if cache.len() >= self.max_cache_size {
            self.evict_lru_operation(&mut cache);
        }

        let cached_result = CachedResult {
            result,
            computation_time,
            access_count: 1,
            last_accessed: std::time::SystemTime::now(),
        };

        cache.insert(key, cached_result);
    }

    /// Get cached subgraph
    pub fn get_subgraph(&self, key: &str) -> Option<Arc<FxGraph>> {
        let cache = self
            .subgraph_cache
            .read()
            .expect("lock should not be poisoned");
        if let Some(graph) = cache.get(key) {
            self.cache_stats
                .lock()
                .expect("lock should not be poisoned")
                .hits += 1;
            Some(graph.clone())
        } else {
            self.cache_stats
                .lock()
                .expect("lock should not be poisoned")
                .misses += 1;
            None
        }
    }

    /// Cache subgraph
    pub fn cache_subgraph(&self, key: String, graph: Arc<FxGraph>) {
        let mut cache = self
            .subgraph_cache
            .write()
            .expect("lock should not be poisoned");

        // Evict oldest entries if cache is full
        if cache.len() >= self.max_cache_size {
            self.evict_lru_subgraph(&mut cache);
        }

        cache.insert(key, graph);
    }

    /// Get cache statistics
    pub fn statistics(&self) -> CacheStatistics {
        self.cache_stats
            .lock()
            .expect("lock should not be poisoned")
            .clone()
    }

    /// Clear all caches
    pub fn clear(&self) {
        self.operation_cache
            .write()
            .expect("lock should not be poisoned")
            .clear();
        self.subgraph_cache
            .write()
            .expect("lock should not be poisoned")
            .clear();
        *self
            .cache_stats
            .lock()
            .expect("lock should not be poisoned") = CacheStatistics::default();
    }

    /// Evict least recently used operation
    fn evict_lru_operation(&self, cache: &mut HashMap<String, CachedResult>) {
        if let Some(lru_key) = cache
            .iter()
            .min_by_key(|(_, result)| result.last_accessed)
            .map(|(key, _)| key.clone())
        {
            cache.remove(&lru_key);
            self.cache_stats
                .lock()
                .expect("lock should not be poisoned")
                .evictions += 1;
        }
    }

    /// Evict least recently used subgraph (simplified LRU)
    fn evict_lru_subgraph(&self, cache: &mut HashMap<String, Arc<FxGraph>>) {
        if let Some(key) = cache.keys().next().cloned() {
            cache.remove(&key);
            self.cache_stats
                .lock()
                .expect("lock should not be poisoned")
                .evictions += 1;
        }
    }
}

/// Graph compression utilities
pub struct GraphCompression;

impl GraphCompression {
    /// Compress graph using operation deduplication
    pub fn deduplicate_operations(graph: &FxGraph) -> TorshResult<FxGraph> {
        let mut compressed_graph = FxGraph::new();
        let mut operation_map: HashMap<String, NodeIndex> = HashMap::new();
        let mut node_mapping: HashMap<NodeIndex, NodeIndex> = HashMap::new();

        // First pass: deduplicate operations
        for (old_idx, node) in graph.nodes() {
            let operation_key = Self::operation_key(node);

            if let Some(&existing_idx) = operation_map.get(&operation_key) {
                // Reuse existing operation
                node_mapping.insert(old_idx, existing_idx);
            } else {
                // Create new operation
                let new_idx = compressed_graph.graph.add_node(node.clone());
                operation_map.insert(operation_key, new_idx);
                node_mapping.insert(old_idx, new_idx);
            }
        }

        // Second pass: rebuild edges
        for edge_ref in graph.graph.edge_references() {
            use petgraph::visit::EdgeRef;
            let old_source = edge_ref.source();
            let old_target = edge_ref.target();

            if let (Some(&new_source), Some(&new_target)) =
                (node_mapping.get(&old_source), node_mapping.get(&old_target))
            {
                // Avoid duplicate edges
                if !compressed_graph
                    .graph
                    .edges_connecting(new_source, new_target)
                    .next()
                    .is_some()
                {
                    compressed_graph.graph.add_edge(
                        new_source,
                        new_target,
                        edge_ref.weight().clone(),
                    );
                }
            }
        }

        // Update inputs and outputs
        compressed_graph.inputs = graph
            .inputs()
            .iter()
            .filter_map(|&idx| node_mapping.get(&idx).copied())
            .collect();
        compressed_graph.outputs = graph
            .outputs()
            .iter()
            .filter_map(|&idx| node_mapping.get(&idx).copied())
            .collect();

        Ok(compressed_graph)
    }

    /// Create operation key for deduplication
    fn operation_key(node: &Node) -> String {
        match node {
            Node::Input(name) => format!("input:{name}"),
            Node::Call(op, args) => {
                let args_str = args.join(",");
                format!("call:{op}:{args_str}")
            }
            Node::Output => "output".into(),
            Node::Conditional {
                condition,
                then_branch,
                else_branch,
            } => {
                format!(
                    "conditional:{}:{}:{}",
                    condition,
                    then_branch.join(","),
                    else_branch.join(",")
                )
            }
            Node::Loop {
                condition,
                body,
                loop_vars,
            } => {
                format!(
                    "loop:{}:{}:{}",
                    condition,
                    body.join(","),
                    loop_vars.join(",")
                )
            }
            Node::Merge { inputs } => {
                let inputs_str = inputs.join(",");
                format!("merge:{inputs_str}")
            }
            Node::GetAttr { target, attr } => format!("getattr:{target}:{attr}"),
        }
    }

    /// Compress graph by removing redundant nodes
    pub fn remove_redundant_nodes(graph: &FxGraph) -> TorshResult<FxGraph> {
        let mut compressed_graph = FxGraph::new();
        let mut node_mapping: HashMap<NodeIndex, Option<NodeIndex>> = HashMap::new();

        // Identify redundant nodes (e.g., identity operations)
        for (old_idx, node) in graph.nodes() {
            if Self::is_redundant_node(node) {
                node_mapping.insert(old_idx, None); // Mark for removal
            } else {
                let new_idx = compressed_graph.graph.add_node(node.clone());
                node_mapping.insert(old_idx, Some(new_idx));
            }
        }

        // Rebuild edges, skipping redundant nodes
        for edge_ref in graph.graph.edge_references() {
            use petgraph::visit::EdgeRef;
            let old_source = edge_ref.source();
            let old_target = edge_ref.target();

            // Find actual source and target (skipping redundant nodes)
            let new_source = Self::find_actual_node(old_source, &node_mapping, graph);
            let new_target = Self::find_actual_node(old_target, &node_mapping, graph);

            if let (Some(source), Some(target)) = (new_source, new_target) {
                if source != target {
                    // Avoid self-loops
                    compressed_graph
                        .graph
                        .add_edge(source, target, edge_ref.weight().clone());
                }
            }
        }

        // Update inputs and outputs
        compressed_graph.inputs = graph
            .inputs()
            .iter()
            .filter_map(|&idx| Self::find_actual_node(idx, &node_mapping, graph))
            .collect();
        compressed_graph.outputs = graph
            .outputs()
            .iter()
            .filter_map(|&idx| Self::find_actual_node(idx, &node_mapping, graph))
            .collect();

        Ok(compressed_graph)
    }

    /// Check if a node is redundant (e.g., identity operation)
    fn is_redundant_node(node: &Node) -> bool {
        match node {
            Node::Call(op, _) => op == "identity" || op == "noop",
            _ => false,
        }
    }

    /// Find the actual node after skipping redundant nodes
    fn find_actual_node(
        start_idx: NodeIndex,
        node_mapping: &HashMap<NodeIndex, Option<NodeIndex>>,
        _graph: &FxGraph,
    ) -> Option<NodeIndex> {
        node_mapping.get(&start_idx).and_then(|&idx| idx)
    }
}

/// Automatic performance profiling and bottleneck detection
#[derive(Debug)]
pub struct PerformanceProfiler {
    operation_times: RwLock<HashMap<String, Vec<Duration>>>,
    bottlenecks: RwLock<Vec<PerformanceBottleneck>>,
    profiling_enabled: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceBottleneck {
    pub operation: String,
    pub average_time: Duration,
    pub frequency: u64,
    pub impact_score: f64,
    pub recommendations: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceReport {
    pub total_operations: u64,
    pub total_time: Duration,
    pub average_operation_time: Duration,
    pub bottlenecks: Vec<PerformanceBottleneck>,
    pub optimization_suggestions: Vec<String>,
}

impl PerformanceProfiler {
    /// Create a new performance profiler
    pub fn new() -> Self {
        Self {
            operation_times: RwLock::new(HashMap::new()),
            bottlenecks: RwLock::new(Vec::new()),
            profiling_enabled: true,
        }
    }

    /// Enable or disable profiling
    pub fn set_profiling_enabled(&mut self, enabled: bool) {
        self.profiling_enabled = enabled;
    }

    /// Record operation execution time
    pub fn record_operation(&self, operation: &str, duration: Duration) {
        if !self.profiling_enabled {
            return;
        }

        let mut times = self
            .operation_times
            .write()
            .expect("lock should not be poisoned");
        times
            .entry(operation.to_string())
            .or_insert_with(Vec::new)
            .push(duration);
    }

    /// Profile graph execution
    pub fn profile_graph_execution<F>(
        &self,
        graph: &FxGraph,
        executor: F,
    ) -> TorshResult<PerformanceReport>
    where
        F: FnOnce(&FxGraph) -> TorshResult<()>,
    {
        let start_time = Instant::now();

        // Execute the graph
        executor(graph)?;

        let total_time = start_time.elapsed();

        // Analyze performance data
        self.analyze_bottlenecks();

        // Generate report
        let report = self.generate_report(total_time);
        Ok(report)
    }

    /// Detect and analyze performance bottlenecks
    fn analyze_bottlenecks(&self) {
        let times = self
            .operation_times
            .read()
            .expect("lock should not be poisoned");
        let mut bottlenecks = Vec::new();

        for (operation, durations) in times.iter() {
            if durations.is_empty() {
                continue;
            }

            let total_time: Duration = durations.iter().sum();
            let average_time = total_time / durations.len() as u32;
            let frequency = durations.len() as u64;

            // Calculate impact score (average time * frequency)
            let impact_score = average_time.as_secs_f64() * frequency as f64;

            // Generate recommendations
            let recommendations = self.generate_recommendations(operation, average_time, frequency);

            if impact_score > 0.1 {
                // Threshold for considering as bottleneck
                bottlenecks.push(PerformanceBottleneck {
                    operation: operation.clone(),
                    average_time,
                    frequency,
                    impact_score,
                    recommendations,
                });
            }
        }

        // Sort by impact score
        bottlenecks.sort_by(|a, b| {
            b.impact_score
                .partial_cmp(&a.impact_score)
                .expect("impact_score should be comparable")
        });

        *self
            .bottlenecks
            .write()
            .expect("lock should not be poisoned") = bottlenecks;
    }

    /// Generate optimization recommendations
    fn generate_recommendations(
        &self,
        operation: &str,
        avg_time: Duration,
        frequency: u64,
    ) -> Vec<String> {
        let mut recommendations = Vec::new();

        if avg_time.as_millis() > 100 {
            recommendations.push(format!(
                "Consider optimizing '{}' operation - high execution time",
                operation
            ));
        }

        if frequency > 1000 {
            recommendations.push(format!(
                "Operation '{}' is called frequently - consider caching",
                operation
            ));
        }

        if operation.contains("conv") && avg_time.as_millis() > 50 {
            recommendations.push(
                "Consider using optimized convolution algorithms or GPU acceleration".to_string(),
            );
        }

        if operation.contains("matmul") && avg_time.as_millis() > 20 {
            recommendations.push(
                "Consider using BLAS libraries or tensor cores for matrix multiplication"
                    .to_string(),
            );
        }

        if recommendations.is_empty() {
            recommendations.push("Performance seems adequate for this operation".to_string());
        }

        recommendations
    }

    /// Generate comprehensive performance report
    fn generate_report(&self, total_time: Duration) -> PerformanceReport {
        let times = self
            .operation_times
            .read()
            .expect("lock should not be poisoned");
        let bottlenecks = self
            .bottlenecks
            .read()
            .expect("lock should not be poisoned")
            .clone();

        let total_operations: u64 = times.values().map(|v| v.len() as u64).sum();
        let average_operation_time = if total_operations > 0 {
            total_time / total_operations as u32
        } else {
            Duration::from_millis(0)
        };

        let optimization_suggestions = self.generate_global_optimizations(&bottlenecks);

        PerformanceReport {
            total_operations,
            total_time,
            average_operation_time,
            bottlenecks,
            optimization_suggestions,
        }
    }

    /// Generate global optimization suggestions
    fn generate_global_optimizations(&self, bottlenecks: &[PerformanceBottleneck]) -> Vec<String> {
        let mut suggestions = Vec::new();

        if bottlenecks.len() > 5 {
            suggestions.push(
                "Consider using graph optimization passes to reduce operation count".to_string(),
            );
        }

        if bottlenecks
            .iter()
            .any(|b| b.operation.contains("copy") || b.operation.contains("transpose"))
        {
            suggestions
                .push("Consider memory layout optimizations to reduce data movement".to_string());
        }

        if bottlenecks.iter().any(|b| b.frequency > 100) {
            suggestions
                .push("Enable operation caching for frequently used computations".to_string());
        }

        suggestions
            .push("Consider using parallel execution for independent operations".to_string());
        suggestions
            .push("Enable compiler optimizations and use release build for production".to_string());

        suggestions
    }

    /// Clear all profiling data
    pub fn clear(&self) {
        self.operation_times
            .write()
            .expect("lock should not be poisoned")
            .clear();
        self.bottlenecks
            .write()
            .expect("lock should not be poisoned")
            .clear();
    }

    /// Get current bottlenecks
    pub fn bottlenecks(&self) -> Vec<PerformanceBottleneck> {
        self.bottlenecks
            .read()
            .expect("lock should not be poisoned")
            .clone()
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Edge, FxGraph, Node};
    use std::sync::Arc;

    #[test]
    fn test_parallel_traversal() {
        let mut graph = FxGraph::new();
        let input = graph.graph.add_node(Node::Input("x".to_string()));
        let relu = graph
            .graph
            .add_node(Node::Call("relu".to_string(), vec!["x".to_string()]));
        let output = graph.graph.add_node(Node::Output);

        graph.graph.add_edge(
            input,
            relu,
            Edge {
                name: "x".to_string(),
            },
        );
        graph.graph.add_edge(
            relu,
            output,
            Edge {
                name: "relu_out".to_string(),
            },
        );
        graph.inputs.push(input);
        graph.outputs.push(output);

        let parallel_traversal = ParallelTraversal::new(Arc::new(graph));
        let visited_nodes = Vec::new();
        let visited_nodes = Arc::new(Mutex::new(visited_nodes));

        let result = parallel_traversal.parallel_topological_traverse(|idx, _node| {
            visited_nodes
                .lock()
                .expect("lock should not be poisoned")
                .push(idx);
        });

        assert!(result.is_ok());
        assert_eq!(
            visited_nodes
                .lock()
                .expect("lock should not be poisoned")
                .len(),
            3
        );
    }

    #[test]
    fn test_graph_cache() {
        let cache = GraphCache::new(100);

        // Test operation caching
        assert!(cache.get_operation("test_op").is_none());

        cache.cache_operation(
            "test_op".to_string(),
            "result".to_string(),
            Duration::from_millis(100),
        );

        let cached = cache.get_operation("test_op").unwrap();
        assert_eq!(cached.result, "result");
        assert_eq!(cached.access_count, 2);

        // Test cache hit
        let cached_again = cache.get_operation("test_op").unwrap();
        assert_eq!(cached_again.access_count, 3);

        let stats = cache.statistics();
        assert_eq!(stats.hits, 2);
        assert_eq!(stats.misses, 1);
    }

    #[test]
    fn test_graph_compression() {
        let mut graph = FxGraph::new();
        let input1 = graph.graph.add_node(Node::Input("x".to_string()));
        let input2 = graph.graph.add_node(Node::Input("x".to_string())); // Duplicate
        let relu = graph
            .graph
            .add_node(Node::Call("relu".to_string(), vec!["x".to_string()]));
        let output = graph.graph.add_node(Node::Output);

        graph.graph.add_edge(
            input1,
            relu,
            Edge {
                name: "x1".to_string(),
            },
        );
        graph.graph.add_edge(
            input2,
            relu,
            Edge {
                name: "x2".to_string(),
            },
        );
        graph.graph.add_edge(
            relu,
            output,
            Edge {
                name: "relu_out".to_string(),
            },
        );

        let compressed = GraphCompression::deduplicate_operations(&graph).unwrap();

        // Should have fewer nodes due to deduplication
        assert!(compressed.node_count() < graph.node_count());
    }

    #[test]
    fn test_performance_profiler() {
        let profiler = PerformanceProfiler::new();

        // Record some operations
        profiler.record_operation("conv2d", Duration::from_millis(100));
        profiler.record_operation("conv2d", Duration::from_millis(120));
        profiler.record_operation("relu", Duration::from_millis(10));

        // Create a simple graph for testing
        let graph = FxGraph::new();

        let report = profiler
            .profile_graph_execution(&graph, |_| Ok(()))
            .unwrap();

        assert_eq!(report.total_operations, 3);
        assert!(!report.bottlenecks.is_empty());
        assert!(!report.optimization_suggestions.is_empty());
    }

    #[test]
    fn test_cache_statistics() {
        let cache = GraphCache::new(2); // Small cache for testing eviction

        cache.cache_operation(
            "op1".to_string(),
            "result1".to_string(),
            Duration::from_millis(50),
        );
        // Small delay to ensure different timestamps
        std::thread::sleep(Duration::from_millis(1));

        cache.cache_operation(
            "op2".to_string(),
            "result2".to_string(),
            Duration::from_millis(75),
        );
        std::thread::sleep(Duration::from_millis(1));

        cache.cache_operation(
            "op3".to_string(),
            "result3".to_string(),
            Duration::from_millis(100),
        ); // Should trigger eviction

        let stats = cache.statistics();
        assert_eq!(stats.evictions, 1);

        // Verify cache contents - op1 should be evicted as it was oldest
        assert!(cache.get_operation("op1").is_none());
        assert!(cache.get_operation("op2").is_some() || cache.get_operation("op3").is_some());

        // Verify we have exactly 2 items cached
        let op2_exists = cache.get_operation("op2").is_some();
        let op3_exists = cache.get_operation("op3").is_some();
        assert_eq!((op2_exists as usize) + (op3_exists as usize), 2);
    }
}