Add a Module::deserialize_file method (#3266)
* Add a `Module::deserialize_file` method This commit adds a new method to the `wasmtime::Module` type, `deserialize_file`. This is intended to be the same as the `deserialize` method except for the serialized module is present as an on-disk file. This enables Wasmtime to internally use `mmap` to avoid copying bytes around and generally makes loading a module much faster. A C API is added in this commit as well for various bindings to use this accelerated path now as well. Another option perhaps for a Rust-based API is to have an API taking a `File` itself to allow for a custom file descriptor in one way or another, but for now that's left for a possible future refactoring if we find a use case. * Fix compat with main - handle readdonly mmap * wip * Try to fix Windows support
This commit is contained in:
@@ -474,6 +474,25 @@ impl Module {
|
||||
module.into_module(engine)
|
||||
}
|
||||
|
||||
/// Same as [`deserialize`], except that the contents of `path` are read to
|
||||
/// deserialize into a [`Module`].
|
||||
///
|
||||
/// For more information see the documentation of the [`deserialize`]
|
||||
/// method for why this function is `unsafe`.
|
||||
///
|
||||
/// This method is provided because it can be faster than [`deserialize`]
|
||||
/// since the data doesn't need to be copied around, but rather the module
|
||||
/// can be used directly from an mmap'd view of the file provided.
|
||||
///
|
||||
/// [`deserialize`]: Module::deserialize
|
||||
pub unsafe fn deserialize_file(engine: &Engine, path: impl AsRef<Path>) -> Result<Module> {
|
||||
let module = SerializedModule::from_file(
|
||||
path.as_ref(),
|
||||
engine.config().deserialize_check_wasmtime_version,
|
||||
)?;
|
||||
module.into_module(engine)
|
||||
}
|
||||
|
||||
fn from_parts(
|
||||
engine: &Engine,
|
||||
mut modules: Vec<Arc<CompiledModule>>,
|
||||
|
||||
@@ -55,6 +55,7 @@ use object::{Bytes, File, Object, ObjectSection};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::BTreeMap;
|
||||
use std::convert::TryFrom;
|
||||
use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use wasmtime_environ::{Compiler, FlagValue, Tunables};
|
||||
@@ -367,6 +368,15 @@ impl<'a> SerializedModule<'a> {
|
||||
Self::from_mmap(MmapVec::from_slice(bytes)?, check_version)
|
||||
}
|
||||
|
||||
pub fn from_file(path: &Path, check_version: bool) -> Result<Self> {
|
||||
Self::from_mmap(
|
||||
MmapVec::from_file(path).with_context(|| {
|
||||
format!("failed to create file mapping for: {}", path.display())
|
||||
})?,
|
||||
check_version,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn from_mmap(mut mmap: MmapVec, check_version: bool) -> Result<Self> {
|
||||
// Artifacts always start with an ELF file, so read that first.
|
||||
// Afterwards we continually read ELF files until we see the `u64::MAX`
|
||||
|
||||
Reference in New Issue
Block a user