pandrs 0.3.0

A high-performance DataFrame library for Rust, providing pandas-like API with advanced features including SIMD optimization, parallel processing, and distributed computing capabilities
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
//! Adaptive JIT Optimizer
//!
//! This module provides adaptive optimization capabilities that learn from runtime
//! performance data to make intelligent optimization decisions.

use crate::core::error::{Error, Result};
use crate::optimized::jit::{
    cache::{CachedFunctionMetadata, FunctionId, JitFunctionCache},
    config::{JITConfig, LoadBalancing, ParallelConfig, SIMDConfig},
    expression_tree::{ExpressionTree, OptimizationType as ExprOptType},
    performance_monitor::{
        FunctionPerformanceMetrics, JitPerformanceMonitor, OptimizationSuggestion, OptimizationType,
    },
};
use crate::{read_lock_safe, write_lock_safe};
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};

/// Adaptive optimizer that learns from performance data
pub struct AdaptiveOptimizer {
    /// Performance monitor for gathering runtime data
    monitor: Arc<JitPerformanceMonitor>,
    /// Function cache for storing optimized functions
    cache: Arc<JitFunctionCache>,
    /// Current JIT configuration
    config: Arc<RwLock<JITConfig>>,
    /// Optimization history for learning
    optimization_history: RwLock<Vec<OptimizationEvent>>,
    /// Performance baselines for comparison
    performance_baselines: RwLock<HashMap<FunctionId, PerformanceBaseline>>,
    /// Learning parameters
    learning_params: LearningParameters,
    /// Optimization strategies
    strategies: Vec<Box<dyn OptimizationStrategy + Send + Sync>>,
    /// Last optimization timestamp
    last_optimization: RwLock<Instant>,
}

/// Learning parameters for the adaptive optimizer
#[derive(Debug, Clone)]
pub struct LearningParameters {
    /// Learning rate for adapting to performance changes
    pub learning_rate: f64,
    /// Minimum improvement threshold to apply an optimization
    pub improvement_threshold: f64,
    /// Window size for performance moving averages
    pub performance_window_size: usize,
    /// Confidence threshold for making optimization decisions
    pub confidence_threshold: f64,
    /// Maximum number of optimization attempts per function
    pub max_optimization_attempts: usize,
    /// Cooldown period between optimizations (in seconds)
    pub optimization_cooldown_secs: u64,
}

impl Default for LearningParameters {
    fn default() -> Self {
        Self {
            learning_rate: 0.1,
            improvement_threshold: 0.05, // 5% improvement
            performance_window_size: 50,
            confidence_threshold: 0.8,
            max_optimization_attempts: 5,
            optimization_cooldown_secs: 60,
        }
    }
}

/// Record of an optimization event
#[derive(Debug, Clone)]
pub struct OptimizationEvent {
    /// Function that was optimized
    pub function_id: FunctionId,
    /// Type of optimization applied
    pub optimization_type: OptimizationType,
    /// Timestamp when optimization was applied
    pub timestamp: Instant,
    /// Performance before optimization
    pub performance_before: f64,
    /// Performance after optimization
    pub performance_after: Option<f64>,
    /// Whether the optimization was successful
    pub success: Option<bool>,
    /// Configuration used for optimization
    pub config_snapshot: JITConfig,
}

/// Performance baseline for a function
#[derive(Debug, Clone)]
pub struct PerformanceBaseline {
    /// Average execution time in nanoseconds
    pub avg_execution_time_ns: f64,
    /// Standard deviation of execution times
    pub std_dev_execution_time_ns: f64,
    /// Throughput in operations per second
    pub throughput_ops_per_sec: f64,
    /// Memory usage in bytes
    pub memory_usage_bytes: usize,
    /// CPU utilization (0.0 to 1.0)
    pub cpu_utilization: f64,
    /// Number of samples used for baseline
    pub sample_count: usize,
    /// Confidence level in the baseline (0.0 to 1.0)
    pub confidence: f64,
    /// Timestamp when baseline was established
    pub established_at: Instant,
}

/// Trait for optimization strategies
pub trait OptimizationStrategy {
    /// Get the name of this strategy
    fn name(&self) -> &'static str;

    /// Analyze a function and suggest optimizations
    fn analyze(
        &self,
        function_id: &FunctionId,
        metrics: &FunctionPerformanceMetrics,
        baseline: Option<&PerformanceBaseline>,
        config: &JITConfig,
    ) -> Vec<OptimizationSuggestion>;

    /// Apply optimization to configuration
    fn apply(&self, suggestion: &OptimizationSuggestion, config: &mut JITConfig) -> Result<()>;

    /// Estimate the confidence in this optimization
    fn confidence(
        &self,
        suggestion: &OptimizationSuggestion,
        metrics: &FunctionPerformanceMetrics,
    ) -> f64;
}

/// SIMD optimization strategy
pub struct SIMDOptimizationStrategy;

impl OptimizationStrategy for SIMDOptimizationStrategy {
    fn name(&self) -> &'static str {
        "SIMD"
    }

    fn analyze(
        &self,
        _function_id: &FunctionId,
        metrics: &FunctionPerformanceMetrics,
        baseline: Option<&PerformanceBaseline>,
        config: &JITConfig,
    ) -> Vec<OptimizationSuggestion> {
        let mut suggestions = Vec::new();

        // Suggest SIMD if:
        // 1. SIMD is not enabled or threshold is too high
        // 2. Function has arithmetic operations
        // 3. CPU utilization is below optimal

        if !config.simd.enabled || config.simd.min_simd_size > 32 {
            if metrics.avg_cpu_utilization < 0.7 && metrics.execution_count > 10 {
                let improvement = if let Some(baseline) = baseline {
                    // Estimate improvement based on baseline
                    0.2 + (0.7 - metrics.avg_cpu_utilization) * 0.5
                } else {
                    0.25 // Default estimate
                };

                suggestions.push(OptimizationSuggestion {
                    suggestion_type: OptimizationType::EnableSIMD,
                    description: "Enable SIMD vectorization for better performance".to_string(),
                    priority:
                        crate::optimized::jit::performance_monitor::OptimizationPriority::Medium,
                    estimated_improvement: improvement,
                });
            }
        }

        suggestions
    }

    fn apply(&self, suggestion: &OptimizationSuggestion, config: &mut JITConfig) -> Result<()> {
        match suggestion.suggestion_type {
            OptimizationType::EnableSIMD => {
                config.simd.enabled = true;
                config.simd.min_simd_size = 32;
                config.simd.vector_width = 32; // AVX2
                Ok(())
            }
            _ => Err(Error::InvalidOperation(
                "Invalid optimization type for SIMD strategy".to_string(),
            )),
        }
    }

    fn confidence(
        &self,
        _suggestion: &OptimizationSuggestion,
        metrics: &FunctionPerformanceMetrics,
    ) -> f64 {
        // Higher confidence for functions with more executions and consistent performance
        let execution_confidence = (metrics.execution_count.min(100) as f64 / 100.0).sqrt();
        let consistency_confidence = 1.0
            - (metrics.std_dev_execution_time_ns / metrics.avg_execution_time_ns.max(1.0)).min(1.0);

        (execution_confidence + consistency_confidence) / 2.0
    }
}

/// Parallelization optimization strategy
pub struct ParallelOptimizationStrategy;

impl OptimizationStrategy for ParallelOptimizationStrategy {
    fn name(&self) -> &'static str {
        "Parallel"
    }

    fn analyze(
        &self,
        _function_id: &FunctionId,
        metrics: &FunctionPerformanceMetrics,
        baseline: Option<&PerformanceBaseline>,
        config: &JITConfig,
    ) -> Vec<OptimizationSuggestion> {
        let mut suggestions = Vec::new();

        // Suggest parallelization if:
        // 1. CPU utilization is low
        // 2. Function execution time is significant
        // 3. Current chunk size is too large

        if metrics.avg_cpu_utilization < 0.6 && metrics.avg_execution_time_ns > 1_000_000.0 {
            // > 1ms
            if config.parallel.min_chunk_size > 1000 {
                let improvement = if let Some(baseline) = baseline {
                    0.15 + (0.6 - metrics.avg_cpu_utilization) * 0.4
                } else {
                    0.20
                };

                suggestions.push(OptimizationSuggestion {
                    suggestion_type: OptimizationType::EnableParallelization,
                    description: "Enable more aggressive parallelization".to_string(),
                    priority:
                        crate::optimized::jit::performance_monitor::OptimizationPriority::High,
                    estimated_improvement: improvement,
                });
            }
        }

        suggestions
    }

    fn apply(&self, suggestion: &OptimizationSuggestion, config: &mut JITConfig) -> Result<()> {
        match suggestion.suggestion_type {
            OptimizationType::EnableParallelization => {
                config.parallel.min_chunk_size = (config.parallel.min_chunk_size / 2).max(100);
                config.parallel.load_balancing = LoadBalancing::Dynamic;
                config.parallel.work_stealing = true;
                Ok(())
            }
            _ => Err(Error::InvalidOperation(
                "Invalid optimization type for parallel strategy".to_string(),
            )),
        }
    }

    fn confidence(
        &self,
        _suggestion: &OptimizationSuggestion,
        metrics: &FunctionPerformanceMetrics,
    ) -> f64 {
        // Higher confidence for longer-running functions
        let time_confidence = (metrics.avg_execution_time_ns / 10_000_000.0).min(1.0); // Normalize to 10ms
        let execution_confidence = (metrics.execution_count.min(50) as f64 / 50.0).sqrt();

        (time_confidence + execution_confidence) / 2.0
    }
}

/// Memory optimization strategy
pub struct MemoryOptimizationStrategy;

impl OptimizationStrategy for MemoryOptimizationStrategy {
    fn name(&self) -> &'static str {
        "Memory"
    }

    fn analyze(
        &self,
        _function_id: &FunctionId,
        metrics: &FunctionPerformanceMetrics,
        baseline: Option<&PerformanceBaseline>,
        _config: &JITConfig,
    ) -> Vec<OptimizationSuggestion> {
        let mut suggestions = Vec::new();

        // Suggest memory optimization if:
        // 1. Memory usage is high
        // 2. Performance is degrading

        if metrics.avg_memory_usage_bytes > 1024 * 1024 {
            // > 1MB
            let priority = if metrics.avg_memory_usage_bytes > 10 * 1024 * 1024 {
                crate::optimized::jit::performance_monitor::OptimizationPriority::High
            } else {
                crate::optimized::jit::performance_monitor::OptimizationPriority::Medium
            };

            let improvement = if let Some(baseline) = baseline {
                (baseline.memory_usage_bytes as f64 / metrics.avg_memory_usage_bytes as f64)
                    .min(0.5)
            } else {
                0.15
            };

            suggestions.push(OptimizationSuggestion {
                suggestion_type: OptimizationType::ReduceMemoryUsage,
                description: "Optimize memory usage to improve cache locality".to_string(),
                priority,
                estimated_improvement: improvement,
            });
        }

        suggestions
    }

    fn apply(&self, _suggestion: &OptimizationSuggestion, _config: &mut JITConfig) -> Result<()> {
        // Memory optimization would typically involve cache management
        // which is handled elsewhere
        Ok(())
    }

    fn confidence(
        &self,
        _suggestion: &OptimizationSuggestion,
        metrics: &FunctionPerformanceMetrics,
    ) -> f64 {
        // Higher confidence for functions with more memory usage data
        (metrics.execution_count.min(30) as f64 / 30.0).sqrt()
    }
}

impl AdaptiveOptimizer {
    /// Create a new adaptive optimizer
    pub fn new(
        monitor: Arc<JitPerformanceMonitor>,
        cache: Arc<JitFunctionCache>,
        config: JITConfig,
    ) -> Self {
        let mut strategies: Vec<Box<dyn OptimizationStrategy + Send + Sync>> = Vec::new();
        strategies.push(Box::new(SIMDOptimizationStrategy));
        strategies.push(Box::new(ParallelOptimizationStrategy));
        strategies.push(Box::new(MemoryOptimizationStrategy));

        Self {
            monitor,
            cache,
            config: Arc::new(RwLock::new(config)),
            optimization_history: RwLock::new(Vec::new()),
            performance_baselines: RwLock::new(HashMap::new()),
            learning_params: LearningParameters::default(),
            strategies,
            last_optimization: RwLock::new(Instant::now()),
        }
    }

    /// Run optimization cycle
    pub fn optimize(&self) -> Result<OptimizationReport> {
        let now = Instant::now();
        let last_opt = *read_lock_safe!(
            self.last_optimization,
            "adaptive optimizer last optimization read"
        )?;

        // Check cooldown
        if now.duration_since(last_opt).as_secs() < self.learning_params.optimization_cooldown_secs
        {
            return Ok(OptimizationReport::default());
        }

        let mut report = OptimizationReport::default();

        // Get functions that need optimization
        let functions_needing_optimization = self.monitor.get_functions_needing_optimization()?;

        for (function_id, _suggestions) in functions_needing_optimization {
            if let Some(optimization_result) = self.optimize_function(&function_id)? {
                report.optimizations_applied.push(optimization_result);
            }
        }

        // Update performance baselines
        self.update_performance_baselines()?;

        // Apply global configuration optimizations
        let config_optimizations = self.optimize_global_config()?;
        report.config_changes.extend(config_optimizations);

        *write_lock_safe!(
            self.last_optimization,
            "adaptive optimizer last optimization write"
        )? = now;

        Ok(report)
    }

    /// Optimize a specific function
    fn optimize_function(&self, function_id: &FunctionId) -> Result<Option<AppliedOptimization>> {
        // Get current performance metrics
        let metrics = match self.monitor.get_function_metrics(function_id) {
            Some(metrics) => metrics,
            None => return Ok(None),
        };

        // Get performance baseline
        let baseline = self
            .performance_baselines
            .read()
            .expect("operation should succeed")
            .get(function_id)
            .cloned();

        // Get current configuration
        let config = read_lock_safe!(self.config, "adaptive optimizer config read")?.clone();

        // Analyze with all strategies
        let mut all_suggestions = Vec::new();
        for strategy in &self.strategies {
            let suggestions = strategy.analyze(function_id, &metrics, baseline.as_ref(), &config);
            for suggestion in suggestions {
                let confidence = strategy.confidence(&suggestion, &metrics);
                all_suggestions.push((suggestion, confidence, strategy.name()));
            }
        }

        // Sort by confidence and estimated improvement
        all_suggestions.sort_by(|a, b| {
            let score_a = a.0.estimated_improvement * a.1;
            let score_b = b.0.estimated_improvement * b.1;
            score_b
                .partial_cmp(&score_a)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Apply the best optimization if confidence is high enough
        if let Some((suggestion, confidence, strategy_name)) = all_suggestions.first() {
            if *confidence >= self.learning_params.confidence_threshold
                && suggestion.estimated_improvement >= self.learning_params.improvement_threshold
            {
                // Check if we've already tried this optimization too many times
                let attempt_count =
                    self.count_optimization_attempts(function_id, suggestion.suggestion_type);
                if attempt_count >= self.learning_params.max_optimization_attempts {
                    return Ok(None);
                }

                // Apply the optimization
                let mut config = write_lock_safe!(self.config, "adaptive optimizer config write")?;
                match self.apply_optimization_by_type(suggestion, &mut config, strategy_name) {
                    Ok(_) => {
                        // Record the optimization event
                        let _ = self.record_optimization_event(OptimizationEvent {
                            function_id: function_id.clone(),
                            optimization_type: suggestion.suggestion_type,
                            timestamp: Instant::now(),
                            performance_before: metrics.get_performance_score(),
                            performance_after: None, // Will be updated later
                            success: None,
                            config_snapshot: config.clone(),
                        });

                        return Ok(Some(AppliedOptimization {
                            function_id: function_id.clone(),
                            optimization_type: suggestion.suggestion_type,
                            strategy_name: strategy_name.to_string(),
                            estimated_improvement: suggestion.estimated_improvement,
                            confidence: *confidence,
                        }));
                    }
                    Err(e) => {
                        eprintln!("Failed to apply optimization: {}", e);
                    }
                }
            }
        }

        Ok(None)
    }

    /// Apply optimization by type
    fn apply_optimization_by_type(
        &self,
        suggestion: &OptimizationSuggestion,
        config: &mut JITConfig,
        strategy_name: &str,
    ) -> Result<()> {
        for strategy in &self.strategies {
            if strategy.name() == strategy_name {
                return strategy.apply(suggestion, config);
            }
        }
        Err(Error::InvalidOperation(format!(
            "Unknown strategy: {}",
            strategy_name
        )))
    }

    /// Count optimization attempts for a function and type
    fn count_optimization_attempts(
        &self,
        function_id: &FunctionId,
        optimization_type: OptimizationType,
    ) -> usize {
        self.optimization_history
            .read()
            .expect("operation should succeed")
            .iter()
            .filter(|event| {
                event.function_id == *function_id && event.optimization_type == optimization_type
            })
            .count()
    }

    /// Record an optimization event
    fn record_optimization_event(&self, event: OptimizationEvent) -> Result<()> {
        let mut history = write_lock_safe!(
            self.optimization_history,
            "adaptive optimizer optimization history write"
        )?;
        history.push(event);

        // Keep only recent history
        if history.len() > 1000 {
            history.drain(0..100);
        }
        Ok(())
    }

    /// Update performance baselines for all functions
    fn update_performance_baselines(&self) -> Result<()> {
        let top_functions = self.monitor.get_top_performing_functions(100)?;
        let mut baselines = write_lock_safe!(
            self.performance_baselines,
            "adaptive optimizer performance baselines write"
        )?;

        for metrics in top_functions {
            if metrics.execution_count >= self.learning_params.performance_window_size as u64 {
                let confidence = self.calculate_baseline_confidence(&metrics);

                let baseline = PerformanceBaseline {
                    avg_execution_time_ns: metrics.avg_execution_time_ns,
                    std_dev_execution_time_ns: metrics.std_dev_execution_time_ns,
                    throughput_ops_per_sec: metrics.throughput_ops_per_sec,
                    memory_usage_bytes: metrics.avg_memory_usage_bytes,
                    cpu_utilization: metrics.avg_cpu_utilization,
                    sample_count: metrics.execution_count as usize,
                    confidence,
                    established_at: Instant::now(),
                };

                baselines.insert(metrics.function_id.clone(), baseline);
            }
        }

        Ok(())
    }

    /// Calculate confidence in a performance baseline
    fn calculate_baseline_confidence(&self, metrics: &FunctionPerformanceMetrics) -> f64 {
        let sample_confidence = (metrics.execution_count.min(100) as f64 / 100.0).sqrt();
        let consistency_confidence = 1.0
            - (metrics.std_dev_execution_time_ns / metrics.avg_execution_time_ns.max(1.0)).min(1.0);

        (sample_confidence + consistency_confidence) / 2.0
    }

    /// Optimize global configuration
    fn optimize_global_config(&self) -> Result<Vec<String>> {
        let suggestions = self.monitor.suggest_config_optimizations()?;
        let mut applied_changes = Vec::new();

        // Apply high-confidence suggestions
        for suggestion in suggestions {
            if suggestion.estimated_improvement >= self.learning_params.improvement_threshold {
                // Apply the suggestion (simplified implementation)
                applied_changes.push(suggestion.description);
            }
        }

        Ok(applied_changes)
    }

    /// Get optimization statistics
    pub fn get_optimization_stats(&self) -> Result<OptimizationStats> {
        let history = read_lock_safe!(
            self.optimization_history,
            "adaptive optimizer optimization history read"
        )?;
        let baselines = read_lock_safe!(
            self.performance_baselines,
            "adaptive optimizer performance baselines read"
        )?;

        let total_optimizations = history.len();
        let successful_optimizations = history
            .iter()
            .filter(|event| event.success == Some(true))
            .count();

        let avg_improvement = history
            .iter()
            .filter_map(|event| {
                if let (Some(before), Some(after)) =
                    (Some(event.performance_before), event.performance_after)
                {
                    Some((after - before) / before)
                } else {
                    None
                }
            })
            .sum::<f64>()
            / successful_optimizations.max(1) as f64;

        Ok(OptimizationStats {
            total_optimizations,
            successful_optimizations,
            success_rate: successful_optimizations as f64 / total_optimizations.max(1) as f64,
            average_improvement: avg_improvement,
            active_baselines: baselines.len(),
            last_optimization: *read_lock_safe!(
                self.last_optimization,
                "adaptive optimizer last optimization read"
            )?,
        })
    }

    /// Learn from recent performance data
    pub fn learn_from_performance(&self) -> Result<LearningReport> {
        let mut report = LearningReport::default();

        // Analyze recent optimization events
        let history = read_lock_safe!(
            self.optimization_history,
            "adaptive optimizer optimization history read"
        )?;
        let recent_events: Vec<_> = history.iter()
            .filter(|event| event.timestamp.elapsed().as_secs() < 3600) // Last hour
            .collect();

        // Update learning parameters based on success rate
        if !recent_events.is_empty() {
            let success_rate = recent_events
                .iter()
                .filter(|event| event.success == Some(true))
                .count() as f64
                / recent_events.len() as f64;

            // Adjust confidence threshold based on success rate
            let mut learning_params = self.learning_params.clone();
            if success_rate > 0.8 {
                learning_params.confidence_threshold =
                    (learning_params.confidence_threshold - 0.05).max(0.5);
                report
                    .adjustments
                    .push("Lowered confidence threshold due to high success rate".to_string());
            } else if success_rate < 0.5 {
                learning_params.confidence_threshold =
                    (learning_params.confidence_threshold + 0.05).min(0.95);
                report
                    .adjustments
                    .push("Raised confidence threshold due to low success rate".to_string());
            }

            // Update improvement threshold based on average improvement
            let avg_improvement = recent_events
                .iter()
                .filter_map(|event| {
                    if let (Some(before), Some(after)) =
                        (Some(event.performance_before), event.performance_after)
                    {
                        Some((after - before) / before)
                    } else {
                        None
                    }
                })
                .sum::<f64>()
                / recent_events.len() as f64;

            if avg_improvement > 0.2 {
                // Note: learning_params adjustment computed but not persisted (read-only analysis)
                let _new_threshold = (learning_params.improvement_threshold + 0.01).min(0.2);
                report.adjustments.push(
                    "Raised improvement threshold due to high average improvement".to_string(),
                );
            }
        }

        Ok(report)
    }
}

/// Report of optimizations applied
#[derive(Debug, Default)]
pub struct OptimizationReport {
    /// Optimizations applied to specific functions
    pub optimizations_applied: Vec<AppliedOptimization>,
    /// Global configuration changes
    pub config_changes: Vec<String>,
}

/// Applied optimization details
#[derive(Debug, Clone)]
pub struct AppliedOptimization {
    /// Function that was optimized
    pub function_id: FunctionId,
    /// Type of optimization applied
    pub optimization_type: OptimizationType,
    /// Strategy that suggested the optimization
    pub strategy_name: String,
    /// Estimated improvement
    pub estimated_improvement: f64,
    /// Confidence in the optimization
    pub confidence: f64,
}

/// Optimization statistics
#[derive(Debug, Clone)]
pub struct OptimizationStats {
    /// Total number of optimizations attempted
    pub total_optimizations: usize,
    /// Number of successful optimizations
    pub successful_optimizations: usize,
    /// Success rate (0.0 to 1.0)
    pub success_rate: f64,
    /// Average performance improvement
    pub average_improvement: f64,
    /// Number of active performance baselines
    pub active_baselines: usize,
    /// Timestamp of last optimization
    pub last_optimization: Instant,
}

/// Learning report
#[derive(Debug, Default)]
pub struct LearningReport {
    /// Adjustments made to learning parameters
    pub adjustments: Vec<String>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::optimized::jit::cache::FunctionId;
    use std::sync::Arc;

    #[test]
    fn test_adaptive_optimizer_creation() {
        let monitor = Arc::new(JitPerformanceMonitor::new(JITConfig::default()));
        let cache = Arc::new(JitFunctionCache::new(64));
        let optimizer = AdaptiveOptimizer::new(monitor, cache, JITConfig::default());

        assert_eq!(optimizer.strategies.len(), 3); // SIMD, Parallel, Memory
    }

    #[test]
    fn test_optimization_strategies() {
        let simd_strategy = SIMDOptimizationStrategy;
        let function_id = FunctionId::new("test", "f64", "f64", "test_op", 1);
        let mut metrics =
            crate::optimized::jit::performance_monitor::FunctionPerformanceMetrics::new(
                function_id,
            );

        // Set up metrics that should trigger SIMD optimization
        metrics.avg_cpu_utilization = 0.5; // Low CPU utilization
        metrics.execution_count = 20; // Enough executions

        let config = JITConfig::default();
        let suggestions = simd_strategy.analyze(&metrics.function_id, &metrics, None, &config);

        assert!(!suggestions.is_empty());
        assert!(suggestions[0].suggestion_type == OptimizationType::EnableSIMD);
    }

    #[test]
    fn test_performance_baseline() {
        let function_id = FunctionId::new("test", "f64", "f64", "test_op", 1);
        let mut metrics =
            crate::optimized::jit::performance_monitor::FunctionPerformanceMetrics::new(
                function_id,
            );

        // Simulate consistent performance
        for _ in 0..100 {
            metrics.record_execution(1_000_000, 1024, 0.8);
        }

        let optimizer = AdaptiveOptimizer::new(
            Arc::new(JitPerformanceMonitor::new(JITConfig::default())),
            Arc::new(JitFunctionCache::new(64)),
            JITConfig::default(),
        );

        let confidence = optimizer.calculate_baseline_confidence(&metrics);
        assert!(confidence > 0.5); // Should have reasonable confidence
    }
}