From 764a33d410ff5c4af0f02a958ebbf29284ca0295 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Wed, 11 Sep 2024 17:19:18 -0700 Subject: [PATCH] refactor: bypass serialized cache in wasmer_wamr flag There is no utility to caching serialized wasms as they are not pre-compiled. Attempting to use a previously pre-compiled wasm in the interpreter engine will fail. --- crates/host/src/module/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/host/src/module/mod.rs b/crates/host/src/module/mod.rs index ee83528c..781dedca 100644 --- a/crates/host/src/module/mod.rs +++ b/crates/host/src/module/mod.rs @@ -196,6 +196,7 @@ impl SerializedModuleCache { /// Given a wasm, compiles with compiler engine, serializes the result, adds it to /// the cache and returns that. + #[cfg(feature = "wasmer_sys")] fn get_with_build_cache( &mut self, key: CacheKey, @@ -293,6 +294,7 @@ impl SerializedModuleCache { /// Given a wasm, attempts to get the serialized module for it from the cache. /// If the cache misses, a new serialized module will be built from the wasm. + #[cfg(feature = "wasmer_sys")] pub fn get(&mut self, key: CacheKey, wasm: &[u8]) -> Result, wasmer::RuntimeError> { match self.cache.get(&key) { Some(serialized_module) => { @@ -306,6 +308,18 @@ impl SerializedModuleCache { None => self.get_with_build_cache(key, wasm), } } + + /// Bypass the cache entirely, returning a wasm module. + /// Wasm are intepretered (not compiled) when using the feature `wasmer_wamr`, + /// so there is no utility in caching them in a pre-serialized format. + #[cfg(feature = "wasmer_wamr")] + pub fn get(&mut self, key: CacheKey, wasm: &[u8]) -> Result, wasmer::RuntimeError> { + let engine = (self.make_engine)(); + let module = Module::from_binary(&engine, wasm) + .map_err(|e| wasm_error!(WasmErrorInner::Compile(e.to_string())))?; + + Ok(Arc::new(module)) + } } /// Caches deserialized wasm modules. Deserialization of cached modules from