From 1c53d7cc4e4605efe7e251de2b0117939d7d6eec Mon Sep 17 00:00:00 2001 From: Trevor Elliott Date: Wed, 6 Dec 2023 10:54:58 -0800 Subject: [PATCH 1/2] Use the compilation cache for components as well Co-authored-by: Pat Hickey --- crates/wasmtime/src/component/component.rs | 47 ++++++++++++++++++++-- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/crates/wasmtime/src/component/component.rs b/crates/wasmtime/src/component/component.rs index 280b3fdef60e..ce912dbe62e0 100644 --- a/crates/wasmtime/src/component/component.rs +++ b/crates/wasmtime/src/component/component.rs @@ -127,14 +127,53 @@ impl Component { #[cfg(any(feature = "cranelift", feature = "winch"))] #[cfg_attr(nightlydoc, doc(cfg(any(feature = "cranelift", feature = "winch"))))] pub fn from_binary(engine: &Engine, binary: &[u8]) -> Result { + use crate::module::HashedEngineCompileEnv; + engine .check_compatible_with_native_host() .context("compilation settings are not compatible with the native host")?; - let (mmap, artifacts) = Component::build_artifacts(engine, binary)?; - let mut code_memory = CodeMemory::new(mmap)?; - code_memory.publish()?; - Component::from_parts(engine, Arc::new(code_memory), Some(artifacts)) + cfg_if::cfg_if! { + if #[cfg(feature = "cache")] { + let state = (HashedEngineCompileEnv(engine), binary); + let (code, artifacts) = wasmtime_cache::ModuleCacheEntry::new( + "wasmtime", + engine.cache_config(), + ) + .get_data_raw( + &state, + + // Cache miss, compute the actual artifacts + |(engine, wasm)| -> Result<_> { + let (mmap, artifacts) = Component::build_artifacts(engine.0, wasm)?; + let code = publish_mmap(mmap)?; + Ok((code, Some(artifacts))) + }, + + // Implementation of how to serialize artifacts + |(_engine, _wasm), (code, _info_and_types)| { + Some(code.mmap().to_vec()) + }, + + // Cache hit, deserialize the provided artifacts + |(engine, _wasm), serialized_bytes| { + let code = engine.0.load_code_bytes(&serialized_bytes, ObjectKind::Component).ok()?; + Some((code, None)) + }, + )?; + } else { + let (mmap, artifacts) = Component::build_artifacts(engine, binary)?; + let code = publish_mmap(mmap)?; + } + }; + + return Component::from_parts(engine, code, artifacts); + + fn publish_mmap(mmap: MmapVec) -> Result> { + let mut code = CodeMemory::new(mmap)?; + code.publish()?; + Ok(Arc::new(code)) + } } /// Same as [`Module::deserialize`], but for components. From b1b0574592a3ee517aca498e2cc5cc680d662b2f Mon Sep 17 00:00:00 2001 From: Trevor Elliott Date: Tue, 12 Dec 2023 14:40:54 -0800 Subject: [PATCH 2/2] Wrap artifacts in `Some` --- crates/wasmtime/src/component/component.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/wasmtime/src/component/component.rs b/crates/wasmtime/src/component/component.rs index ce912dbe62e0..6b6a90c42ea9 100644 --- a/crates/wasmtime/src/component/component.rs +++ b/crates/wasmtime/src/component/component.rs @@ -163,6 +163,7 @@ impl Component { )?; } else { let (mmap, artifacts) = Component::build_artifacts(engine, binary)?; + let artifacts = Some(artifacts); let code = publish_mmap(mmap)?; } };