1 //! Tuning Wasmtime for fast Wasm execution. 2 3 use wasmtime::{Config, Engine, Result, Strategy}; 4 main() -> Result<()>5fn main() -> Result<()> { 6 let mut config = Config::new(); 7 8 // Enable the Cranelift optimizing compiler. 9 config.strategy(Strategy::Cranelift); 10 11 // Enable signals-based traps. This is required to elide explicit 12 // bounds-checking. 13 config.signals_based_traps(true); 14 15 // Configure linear memories such that explicit bounds-checking can be 16 // elided. 17 config.memory_reservation(1 << 32); 18 config.memory_guard_size(1 << 32); 19 20 if CROSS_COMPILING { 21 // When cross-compiling, force-enable various ISA extensions. 22 // 23 // Warning: it is wildly unsafe to run a pre-compiled Wasm program that 24 // enabled a particular ISA extension on a machine that does not 25 // actually support that ISA extension!!! 26 unsafe { 27 if let Err(error) = config.target("x86_64") { 28 eprintln!( 29 "Wasmtime was not compiled with the x86_64 backend for \ 30 Cranelift enabled: {error:?}", 31 ); 32 return Ok(()); 33 } 34 config.cranelift_flag_enable("has_sse41"); 35 config.cranelift_flag_enable("has_avx"); 36 config.cranelift_flag_enable("has_avx2"); 37 config.cranelift_flag_enable("has_lzcnt"); 38 } 39 } 40 41 // Build an `Engine` with our `Config` that is tuned for fast Wasm 42 // execution. 43 let engine = match Engine::new(&config) { 44 Ok(engine) => engine, 45 Err(error) => { 46 eprintln!("Configuration is incompatible with this host platform: {error:?}"); 47 return Ok(()); 48 } 49 }; 50 51 // Now we can use `engine` to compile and/or run some Wasm programs... 52 53 let _ = engine; 54 Ok(()) 55 } 56 57 const CROSS_COMPILING: bool = false; 58