Skip to content

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

feat: allow Wizer context reuse (#13954)

Roman Volosatovsf6664f67f8fa
diff --git a/crates/wizer/src/component.rs b/crates/wizer/src/component.rs
index 235223e101..61558cf3f7 100644
--- a/crates/wizer/src/component.rs
+++ b/crates/wizer/src/component.rs
@@ -34,15 +34,15 @@ impl Wizer {
/// Same as [`Wizer::snapshot`], except for components.
pub async fn snapshot_component(
&self,
- mut cx: ComponentContext<'_>,
+ cx: &ComponentContext<'_>,
instance: &mut impl ComponentInstanceState,
) -> Result<Vec<u8>> {
if !self.func_renames.is_empty() {
bail!("components do not support renaming functions");
}
- let snapshot = snapshot::snapshot(&cx, instance).await;
- let rewritten_wasm = self.rewrite_component(&mut cx, &snapshot);
+ let snapshot = snapshot::snapshot(cx, instance).await;
+ let rewritten_wasm = self.rewrite_component(cx, &snapshot);
self.debug_assert_valid_wasm(&rewritten_wasm, "rewritten component");
Ok(rewritten_wasm)
diff --git a/crates/wizer/src/component/info.rs b/crates/wizer/src/component/info.rs
index 153d5419bd..7db7540b5c 100644
--- a/crates/wizer/src/component/info.rs
+++ b/crates/wizer/src/component/info.rs
@@ -5,7 +5,7 @@ use std::collections::HashMap;
/// [`Wizer::instrument_component`].
///
/// [`Wizer::instrument_component`]: crate::Wizer::instrument_component
-#[derive(Default)]
+#[derive(Clone, Default)]
pub struct ComponentContext<'a> {
/// Sections of the component, which are either raw bytes or a parsed module
/// using `ModuleContext`.
@@ -31,6 +31,7 @@ pub struct ComponentContext<'a> {
}
/// Generated accessors during instrumentation and the metadata about them.
+#[derive(Clone)]
pub(crate) enum Accessor {
/// This accessor retrieves the value of a wasm global.
Global {
@@ -64,6 +65,7 @@ pub(crate) enum Accessor {
}
/// A section of a component, learned during parsing.
+#[derive(Clone)]
pub(crate) enum RawSection<'a> {
/// A non-module section, whose raw contents are stored here.
Raw(wasm_encoder::RawSection<'a>),
diff --git a/crates/wizer/src/component/rewrite.rs b/crates/wizer/src/component/rewrite.rs
index 7a2de6658f..252b225bb2 100644
--- a/crates/wizer/src/component/rewrite.rs
+++ b/crates/wizer/src/component/rewrite.rs
@@ -12,7 +12,7 @@ impl Wizer {
/// updates module sections with whatever [`Wizer::rewrite`] returns.
pub(crate) fn rewrite_component(
&self,
- component: &mut ComponentContext<'_>,
+ component: &ComponentContext<'_>,
snapshot: &ComponentSnapshot,
) -> Vec<u8> {
let mut encoder = wasm_encoder::Component::new();
@@ -23,7 +23,7 @@ impl Wizer {
};
let mut module_index = 0;
- for section in component.sections.iter_mut() {
+ for section in component.sections.iter() {
match section {
RawSection::Module(module) => {
let snapshot = snapshot
diff --git a/crates/wizer/src/component/wasmtime.rs b/crates/wizer/src/component/wasmtime.rs
index 570f5ed466..93164dc407 100644
--- a/crates/wizer/src/component/wasmtime.rs
+++ b/crates/wizer/src/component/wasmtime.rs
@@ -33,7 +33,7 @@ impl Wizer {
self.initialize_component(store, &instance, index, args, &mut rets)
.await?;
let snap = self
- .snapshot_component(cx, &mut WasmtimeWizerComponent { store, instance })
+ .snapshot_component(&cx, &mut WasmtimeWizerComponent { store, instance })
.await?;
Ok((snap, rets))
}
diff --git a/crates/wizer/src/info.rs b/crates/wizer/src/info.rs
index 32b2a1e529..76e1a9ea6c 100644
--- a/crates/wizer/src/info.rs
+++ b/crates/wizer/src/info.rs
@@ -6,7 +6,7 @@ use std::ops::Range;
///
/// These are created during our `parse` pass and then used throughout
/// our later passes.
-#[derive(Default)]
+#[derive(Clone, Default)]
pub struct ModuleContext<'a> {
/// The raw sections from the original Wasm input.
raw_sections: Vec<wasm_encoder::RawSection<'a>>,
diff --git a/crates/wizer/src/lib.rs b/crates/wizer/src/lib.rs
index 76496cd104..91eeb216aa 100644
--- a/crates/wizer/src/lib.rs
+++ b/crates/wizer/src/lib.rs
@@ -232,14 +232,14 @@ impl Wizer {
/// pre-initialized.
pub async fn snapshot(
&self,
- mut cx: ModuleContext<'_>,
+ cx: &ModuleContext<'_>,
instance: &mut impl InstanceState,
) -> Result<Vec<u8>> {
// Parse rename spec.
let renames = FuncRenames::parse(&self.func_renames)?;
- let snapshot = snapshot::snapshot(&cx, instance).await;
- let rewritten_wasm = self.rewrite(&mut cx, &snapshot, &renames, true);
+ let snapshot = snapshot::snapshot(cx, instance).await;
+ let rewritten_wasm = self.rewrite(cx, &snapshot, &renames, true);
self.debug_assert_valid_wasm(&rewritten_wasm, "rewritten module");
diff --git a/crates/wizer/src/rewrite.rs b/crates/wizer/src/rewrite.rs
index 33083b5467..cb2f4e1f1e 100644
--- a/crates/wizer/src/rewrite.rs
+++ b/crates/wizer/src/rewrite.rs
@@ -12,7 +12,7 @@ impl Wizer {
///
pub(crate) fn rewrite(
&self,
- module: &mut ModuleContext<'_>,
+ module: &ModuleContext<'_>,
snapshot: &Snapshot,
renames: &FuncRenames,
remove_wasi_initialize: bool,
diff --git a/crates/wizer/src/wasmtime.rs b/crates/wizer/src/wasmtime.rs
index 8a85d0dbcd..e1289ea69f 100644
--- a/crates/wizer/src/wasmtime.rs
+++ b/crates/wizer/src/wasmtime.rs
@@ -22,7 +22,7 @@ impl Wizer {
let instance = instantiate(store, &module).await?;
self.initialize(store, &instance).await?;
- self.snapshot(cx, &mut WasmtimeWizer { store, instance })
+ self.snapshot(&cx, &mut WasmtimeWizer { store, instance })
.await
}
diff --git a/src/commands/wizer.rs b/src/commands/wizer.rs
index f37e44e6f5..aa72397b32 100644
--- a/src/commands/wizer.rs
+++ b/src/commands/wizer.rs
@@ -127,7 +127,7 @@ impl WizerCommand {
(WizerInfo::Core(cx), CliInstance::Core(instance)) => {
self.wizer
.snapshot(
- cx,
+ &cx,
&mut wasmtime_wizer::WasmtimeWizer {
store: &mut store,
instance,
@@ -140,7 +140,7 @@ impl WizerCommand {
(WizerInfo::Component(cx), CliInstance::Component(instance)) => {
self.wizer
.snapshot_component(
- cx,
+ &cx,
&mut wasmtime_wizer::WasmtimeWizerComponent {
store: &mut store,
instance,