Skip to content

Commit

Permalink
Fix compilation cache for components
Browse files Browse the repository at this point in the history
This commit fixes a mistake in bytecodealliance#8181 which meant that the caching for
components was no longer working. The mistake is fixed in this commit as
well as a new test being added too.
  • Loading branch information
alexcrichton committed Mar 28, 2024
1 parent a461382 commit 6f68593
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
12 changes: 7 additions & 5 deletions crates/wasmtime/src/compile/code_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,13 @@ impl<'a> CodeBuilder<'a> {
// 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::Module)
.ok()?;
|(engine, wasm, _), serialized_bytes| {
let kind = if wasmparser::Parser::is_component(&wasm) {
ObjectKind::Component
} else {
ObjectKind::Module
};
let code = engine.0.load_code_bytes(&serialized_bytes, kind).ok()?;
Some((code, None))
},
)?;
Expand Down
32 changes: 32 additions & 0 deletions crates/wasmtime/src/engine/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,4 +812,36 @@ Caused by:

Ok(())
}

#[test]
#[cfg_attr(miri, ignore)]
#[cfg(feature = "component-model")]
fn components_are_cached() -> Result<()> {
use crate::component::Component;

let td = TempDir::new()?;
let config_path = td.path().join("config.toml");
std::fs::write(
&config_path,
&format!(
"
[cache]
enabled = true
directory = '{}'
",
td.path().join("cache").display()
),
)?;
let mut cfg = Config::new();
cfg.cache_config_load(&config_path)?;
let engine = Engine::new(&cfg)?;
Component::new(&engine, "(component (core module (func)))")?;
assert_eq!(engine.config().cache_config.cache_hits(), 0);
assert_eq!(engine.config().cache_config.cache_misses(), 1);
Component::new(&engine, "(component (core module (func)))")?;
assert_eq!(engine.config().cache_config.cache_hits(), 1);
assert_eq!(engine.config().cache_config.cache_misses(), 1);

Ok(())
}
}

0 comments on commit 6f68593

Please sign in to comment.