Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Drop cached compiler contexts between modules/components (#14314)

Alex Crichton2be5e3a62b95
diff --git a/crates/cranelift/src/compiler.rs b/crates/cranelift/src/compiler.rs
index ed95a74f24..980a498b7d 100644
--- a/crates/cranelift/src/compiler.rs
+++ b/crates/cranelift/src/compiler.rs
@@ -959,6 +959,10 @@ impl wasmtime_environ::Compiler for Compiler {
let func = func.downcast_ref::<CompiledFunction>().unwrap();
Box::new(func.relocations().map(|r| r.reloc_target))
}
+
+ fn release_caches(&self) {
+ *self.contexts.lock().unwrap() = Vec::new();
+ }
}
impl InliningCompiler for Compiler {
diff --git a/crates/environ/src/compile/mod.rs b/crates/environ/src/compile/mod.rs
index 620f627392..dbfd044977 100644
--- a/crates/environ/src/compile/mod.rs
+++ b/crates/environ/src/compile/mod.rs
@@ -433,6 +433,10 @@ pub trait Compiler: Send + Sync {
// By default, an ISA cannot create a System V CIE.
None
}
+
+ /// Invoked at the end of a module or component compilation and signals
+ /// that any transient caches across functions can now be dropped.
+ fn release_caches(&self);
}
/// An inlining compiler.
diff --git a/crates/wasmtime/src/compile.rs b/crates/wasmtime/src/compile.rs
index d0b3f1d121..deb540deee 100644
--- a/crates/wasmtime/src/compile.rs
+++ b/crates/wasmtime/src/compile.rs
@@ -129,6 +129,8 @@ pub(crate) fn build_module_artifacts<T: FinishedObject>(
object.serialize_info(&(&info, &index, &types));
let result = T::finish_object(object, obj_state)?;
+ compiler.release_caches();
+
Ok((result, Some((info, index, types))))
}
@@ -236,6 +238,8 @@ pub(crate) fn build_component_artifacts<T: FinishedObject>(
};
object.serialize_info(&artifacts);
+ compiler.release_caches();
+
let result = T::finish_object(object, obj_state)?;
Ok((result, Some(artifacts)))
}
diff --git a/crates/winch/src/compiler.rs b/crates/winch/src/compiler.rs
index 5cf3a4a100..4568341ff4 100644
--- a/crates/winch/src/compiler.rs
+++ b/crates/winch/src/compiler.rs
@@ -229,6 +229,10 @@ impl wasmtime_environ::Compiler for Compiler {
) -> Box<dyn Iterator<Item = FuncKey> + 'a> {
self.trampolines.compiled_function_relocation_targets(func)
}
+
+ fn release_caches(&self) {
+ self.trampolines.release_caches();
+ }
}
/// A wrapper around another `Compiler` implementation that may or may not be an
@@ -324,6 +328,10 @@ impl wasmtime_environ::Compiler for NoInlineCompiler {
self.0
.append_dwarf(obj, translations, get_func, dwarf_package_bytes, tunables)
}
+
+ fn release_caches(&self) {
+ self.0.release_caches();
+ }
}
impl wasmtime_environ::component::ComponentCompiler for NoInlineCompiler {