amberjs 1.17.0

JavaScript/TypeScript runtime built with Rust and V8
Documentation
// V8 Engine Configuration Flags
//
// This module provides comprehensive V8 engine configuration options
// for performance optimization, allowing fine-tuned control over
// JIT compilation, memory management, and garbage collection.
//
// Stage 69 Phase 2: V8 Engine Deep Optimization

use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap};

/// V8 Engine Flags Configuration
/// Provides high-performance V8 engine configuration options
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct V8EngineFlags {
    /// Enable TurboFan optimization (highest level)
    pub turbo_optimization_level: u8,
    /// Enable TurboFan profiling for performance analysis
    pub turbo_profiling: bool,
    /// Maximum old space size in MB
    pub max_old_space_mb: usize,
    /// Maximum new space size in MB
    pub max_new_space_mb: usize,
    /// GC interval in milliseconds
    pub gc_interval_ms: u64,
    /// Enable Sparkplug baseline compiler for faster startup
    pub enable_sparkplug: bool,
    /// Enable Maglev mid-tier optimizer
    pub enable_maglev: bool,
    /// Maximum inline depth for function inlining
    pub max_inline_depth: u32,
    /// Enable JIT compilation
    pub jit_enabled: bool,
    /// Optimize for size vs speed (0 = speed, 1 = balance, 2 = size)
    pub optimize_for_size: u8,
    /// Code range size in MB (0 = auto)
    pub code_range_size_mb: usize,
    /// Enable concurrent garbage collection
    pub concurrent_gc: bool,
    /// Enable incremental marking
    pub incremental_marking: bool,
    /// Maximum executable size in MB (0 = auto)
    pub max_executable_size_mb: usize,
}
impl Default for V8EngineFlags {
    fn default() -> Self {
        Self {
            turbo_optimization_level: 3,
            turbo_profiling: false,
            max_old_space_mb: 256,
            max_new_space_mb: 16,
            gc_interval_ms: 500,
            enable_sparkplug: true,
            enable_maglev: true,
            max_inline_depth: 10,
            jit_enabled: true,
            optimize_for_size: 0,
            code_range_size_mb: 0,
            concurrent_gc: true,
            incremental_marking: true,
            max_executable_size_mb: 128,
        }
    }
}
impl V8EngineFlags {
    /// High-performance configuration for maximum speed
    /// Optimized for production workloads with sustained high performance
    pub fn high_performance() -> Self {
        Self {
            turbo_optimization_level: 4,        // Highest optimization level
            turbo_profiling: true,              // Enable profiling for optimization
            max_old_space_mb: 512,              // Larger heap for sustained workloads
            max_new_space_mb: 64,               // Larger new space for faster allocation
            gc_interval_ms: 100,                // More frequent GC to prevent long pauses
            enable_sparkplug: true,             // Fast baseline compilation
            enable_maglev: true,                // Mid-tier optimization
            max_inline_depth: 15,               // Deeper inlining for better performance
            jit_enabled: true,                  // Enable JIT
            optimize_for_size: 0,               // Optimize for speed, not size
            code_range_size_mb: 256,            // Larger code range for JIT
            concurrent_gc: true,                // Non-blocking GC
            incremental_marking: true,          // Incremental marking for smoother GC
            max_executable_size_mb: 256,        // Larger executable size limit
        }
    }
    /// Balanced configuration for development and moderate workloads
    pub fn balanced() -> Self {
        Self {
            turbo_optimization_level: 3,
            turbo_profiling: false,
            max_old_space_mb: 256,
            max_new_space_mb: 32,
            gc_interval_ms: 200,
            enable_sparkplug: true,
            enable_maglev: true,
            max_inline_depth: 10,
            jit_enabled: true,
            optimize_for_size: 1,
            code_range_size_mb: 128,
            concurrent_gc: true,
            incremental_marking: true,
            max_executable_size_mb: 128,
        }
    }
    /// Low-memory configuration for memory-constrained environments
    pub fn low_memory() -> Self {
        Self {
            turbo_optimization_level: 2,
            turbo_profiling: false,
            max_old_space_mb: 128,
            max_new_space_mb: 16,
            gc_interval_ms: 100,
            enable_sparkplug: true,
            enable_maglev: false,
            max_inline_depth: 5,
            jit_enabled: true,
            optimize_for_size: 2,
            code_range_size_mb: 64,
            concurrent_gc: false,
            incremental_marking: false,
            max_executable_size_mb: 64,
        }
    }
    /// Convert flags to V8 command-line arguments
    pub fn to_v8_flags(&self) -> Vec<String> {
        let mut flags = Vec::new();
        // JIT and optimization flags
        if self.jit_enabled {
            flags.push("--turbofan".to_string());
        }
        flags.push(format!("--turbo_optimization_level={}", self.turbo_optimization_level));
        flags.push(format!("--max_old_space_size={}", self.max_old_space_mb));
        flags.push(format!("--max_new_space_size={}", self.max_new_space_mb));
        // Compiler flags
        if self.enable_sparkplug {
            flags.push("--sparkplug".to_string());
        }
        if self.enable_maglev {
            flags.push("--maglev".to_string());
        }
        flags.push(format!("--max_inline_depth={}", self.max_inline_depth));
        // Memory flags
        if self.code_range_size_mb > 0 {
            flags.push(format!("--code_range_size={}", self.code_range_size_mb));
        }
        if self.max_executable_size_mb > 0 {
            flags.push(format!("--max_executable_size={}", self.max_executable_size_mb));
        }
        // GC flags
        if self.concurrent_gc {
            flags.push("--concurrent_gc".to_string());
        }
        if self.incremental_marking {
            flags.push("--incremental_marking".to_string());
        }
        flags.push(format!("--gc_interval={}", self.gc_interval_ms));
        // Profiling flags
        if self.turbo_profiling {
            flags.push("--turbo_profiling".to_string());
        }
        // Optimization flags
        if self.optimize_for_size > 0 {
            flags.push(format!("--optimize_for_size={}", self.optimize_for_size));
        }
        // Additional high-performance flags
        flags.push("--inline-js".to_string());      // Inline JavaScript
        flags.push("--inline-wasm".to_string());    // Inline WebAssembly
        flags.push("--turbo_fast_math".to_string()); // Fast math operations
        flags.push("--turbo_loop_peeling".to_string()); // Loop peeling optimization
        flags.push("--turbo_loop_unrolling".to_string()); // Loop unrolling
        flags.push("--turbo_loop_variable_scheduling".to_string()); // Better loop variable scheduling
        flags
    }
    /// Get a performance profile string for logging
    pub fn profile_name(&self) -> &str {
        match (
            self.turbo_optimization_level,
            self.max_old_space_mb,
            self.max_inline_depth,
        ) {
            (4, 512..=usize::MAX, 15) => "high_performance",
            (3, 256..=511, 10) => "balanced",
            (_, 128..=255, _) => "low_memory",
            _ => "custom",
        }
    }
    /// Get estimated memory usage in MB
    pub fn estimated_memory_mb(&self) -> usize {
        self.max_old_space_mb + self.max_new_space_mb + self.code_range_size_mb
    }
}
/// V8 Engine Configuration Manager
/// Manages multiple V8 configurations for different workloads
#[derive(Debug)]
pub struct V8ConfigManager {
    /// Map of configuration name to flags
    configs: HashMap<String, V8EngineFlags>,
}
impl V8ConfigManager {
    /// Create a new configuration manager with default configurations
    pub fn new() -> Self {
        let mut configs = HashMap::new();
        configs.insert("default".to_string(), V8EngineFlags::default());
        configs.insert("high_performance".to_string(), V8EngineFlags::high_performance());
        configs.insert("balanced".to_string(), V8EngineFlags::balanced());
        configs.insert("low_memory".to_string(), V8EngineFlags::low_memory());
        Self { configs }
    }
    /// Get a configuration by name
    pub fn get(&self, name: &str) -> Option<&V8EngineFlags> {
        self.configs.get(name)
    }
    /// Add or update a configuration
    pub fn add(&mut self, name: String, flags: V8EngineFlags) {
        self.configs.insert(name, flags);
    }
    /// Get all available configuration names
    pub fn config_names(&self) -> Vec<&str> {
        self.configs.keys().map(|k| k.as_str()).collect()
    }
    /// Get the best configuration for the current system
    pub fn best_for_system(&self) -> &V8EngineFlags {
        // For now, use high_performance as default
        // In the future, we could auto-detect system capabilities
        self.configs.get("high_performance").unwrap()
    }
}
impl Default for V8ConfigManager {
    fn default() -> Self {
        Self::new()
    }
}
#[cfg(test)]
mod tests {
    #[test]
    fn test_high_performance_config() {
        let flags: _ = V8EngineFlags::high_performance();
        assert_eq!(flags.turbo_optimization_level, 4);
        assert!(flags.turbo_profiling);
        assert_eq!(flags.max_old_space_mb, 512);
        assert!(flags.jit_enabled);
    }
    #[test]
    fn test_v8_flags_conversion() {
        let flags: _ = V8EngineFlags::high_performance();
        let v8_flags: _ = flags.to_v8_flags();
        assert!(v8_flags.contains(&"--turbofan".to_string()));
        assert!(v8_flags.iter().any(|f| f.contains("turbo_optimization_level=4")));
        assert!(v8_flags.contains(&"--sparkplug".to_string()));
    }
    #[test]
    fn test_config_manager() {
        let manager: _ = V8ConfigManager::new();
        assert!(manager.config_names().contains(&"high_performance"));
        assert!(manager.get("high_performance").is_some());
    }
    #[test]
    fn test_profile_name() {
        let flags: _ = V8EngineFlags::high_performance();
        assert_eq!(flags.profile_name(), "high_performance");
    }
    #[test]
    fn test_estimated_memory() {
        let flags: _ = V8EngineFlags::high_performance();
        let mem: _ = flags.estimated_memory_mb();
        assert_eq!(mem, 512 + 64 + 256); // old_space + new_space + code_range
    }
}