gaia-assembler 0.1.1

Universal assembler framework for Gaia project
Documentation
//! Performance monitoring and optimization utilities
//! 
//! This module provides tools for measuring and optimizing the performance of the compilation process.

use std::time::{Duration, Instant};
use std::collections::HashMap;

/// Performance metrics collector
#[derive(Debug, Default)]
pub struct PerformanceMonitor {
    start_times: HashMap<String, Instant>,
    durations: HashMap<String, Duration>,
}

impl PerformanceMonitor {
    /// Create a new performance monitor
    pub fn new() -> Self {
        Self::default()
    }

    /// Start measuring a stage
    pub fn start(&mut self, stage: &str) {
        self.start_times.insert(stage.to_string(), Instant::now());
    }

    /// Stop measuring a stage and record the duration
    pub fn stop(&mut self, stage: &str) {
        if let Some(start_time) = self.start_times.remove(stage) {
            let duration = start_time.elapsed();
            self.durations.insert(stage.to_string(), duration);
        }
    }

    /// Get the duration of a stage
    pub fn get_duration(&self, stage: &str) -> Option<&Duration> {
        self.durations.get(stage)
    }

    /// Get all recorded durations
    pub fn get_durations(&self) -> &HashMap<String, Duration> {
        &self.durations
    }

    /// Reset the performance monitor
    pub fn reset(&mut self) {
        self.start_times.clear();
        self.durations.clear();
    }

    /// Print the performance report
    pub fn print_report(&self) {
        println!("Performance Report:");
        println!("==================");
        for (stage, duration) in self.durations.iter() {
            println!("{}: {:?}", stage, duration);
        }
        println!("==================");
    }
}

/// A performance monitoring guard that automatically stops the measurement when dropped
pub struct PerformanceGuard<'a> {
    monitor: &'a mut PerformanceMonitor,
    stage: String,
}

impl<'a> PerformanceGuard<'a> {
    /// Create a new performance guard
    pub fn new(monitor: &'a mut PerformanceMonitor, stage: &str) -> Self {
        monitor.start(stage);
        Self {
            monitor,
            stage: stage.to_string(),
        }
    }
}

impl<'a> Drop for PerformanceGuard<'a> {
    fn drop(&mut self) {
        self.monitor.stop(&self.stage);
    }
}