Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/wasmtime/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,12 @@ impl<T> InstancePre<T> {
})
}

/// Returns a reference to the module that this [`InstancePre`] will be
/// instantiating.
pub fn module(&self) -> &Module {
&self.module
}

/// Instantiates this instance, creating a new instance within the provided
/// `store`.
///
Expand Down
40 changes: 34 additions & 6 deletions crates/wasmtime/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,38 @@ impl Module {
pub fn image_range(&self) -> Range<usize> {
self.compiled_module().image_range()
}

/// Force initialization of copy-on-write images to happen here-and-now
/// instead of when they're requested during first instantiation.
///
/// When [copy-on-write memory
/// initialization](crate::Config::memory_init_cow) is enabled then Wasmtime
/// will lazily create the initialization image for a module. This method
/// can be used to explicitly dictate when this initialization happens.
///
/// Note that this largely only matters on Linux when memfd is used.
/// Otherwise the copy-on-write image typically comes from disk and in that
/// situation the creation of the image is trivial as the image is always
/// sourced from disk. On Linux, though, when memfd is used a memfd is
/// created and the initialization image is written to it.
///
/// Also note that this method is not required to be called, it's available
/// as a performance optimization if required but is otherwise handled
/// automatically.
pub fn initialize_copy_on_write_image(&self) -> Result<()> {
self.inner.memory_images()?;
Ok(())
}
}

impl ModuleInner {
fn memory_images(&self) -> Result<Option<&ModuleMemoryImages>> {
let images = self
.memory_images
.get_or_try_init(|| memory_images(&self.engine, &self.module))?
.as_ref();
Ok(images)
}
}

fn _assert_send_sync() {
Expand Down Expand Up @@ -880,12 +912,8 @@ impl wasmtime_runtime::ModuleRuntimeInfo for ModuleInner {
}

fn memory_image(&self, memory: DefinedMemoryIndex) -> Result<Option<&Arc<MemoryImage>>> {
let images = self
.memory_images
.get_or_try_init(|| memory_images(&self.engine, &self.module))?;
Ok(images
.as_ref()
.and_then(|images| images.get_memory_image(memory)))
let images = self.memory_images()?;
Ok(images.and_then(|images| images.get_memory_image(memory)))
}

fn unique_id(&self) -> Option<CompiledModuleId> {
Expand Down