Skip to content

Commit

Permalink
Merge pull request wasmerio#4171 from wasmerio/revive-4139-0-copy-mod…
Browse files Browse the repository at this point in the history
…ule-deserialization

Revive "0-copy module deserialization"
  • Loading branch information
Arshia001 authored Aug 23, 2023
2 parents 4030493 + 4d02b2b commit 5fc8974
Show file tree
Hide file tree
Showing 35 changed files with 1,382 additions and 456 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ glob = "0.3"
rustc_version = "0.4"

[dev-dependencies]
wasmer = { version = "=4.1.2", path = "lib/api", default-features = false, features = [
wasmer = { version = "=4.1.2", path = "lib/api", features = [
"compiler",
"singlepass",
"sys",
] }
anyhow = "1.0"
criterion = { version = "0.5", default-features = false }
Expand Down
41 changes: 16 additions & 25 deletions lib/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ bytes = "1"
wat = { version = "1.0", optional = true }
tracing = { version = "0.1", optional = true }
rustc-demangle = "0.1"
shared-buffer = "0.1"

# Dependencies and Development Dependencies for `sys`.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down Expand Up @@ -103,16 +104,10 @@ std = []
core = ["hashbrown"]

# Features for `sys`.
sys = [
"wasmer-compiler/translator",
"wasmer-compiler/compiler",
"std",
]
sys = ["wasmer-compiler/translator", "wasmer-compiler/compiler", "std"]
sys-default = ["sys", "wat", "cranelift"]
# - Compilers.
compiler = [
"sys",
]
compiler = ["sys"]
singlepass = ["compiler", "wasmer-compiler-singlepass"]
cranelift = ["compiler", "wasmer-compiler-cranelift"]
llvm = ["compiler", "wasmer-compiler-llvm"]
Expand All @@ -127,11 +122,7 @@ js-default = ["js", "std", "wasm-types-polyfill"]

wasm-types-polyfill = ["wasmparser"]

jsc = [
"rusty_jsc",
"wasm-types-polyfill",
"wasmparser",
]
jsc = ["rusty_jsc", "wasm-types-polyfill", "wasmparser"]

js-serializable-module = []

Expand All @@ -149,17 +140,17 @@ static-artifact-create = ["wasmer-compiler/static-artifact-create"]

[package.metadata.docs.rs]
features = [
"compiler",
"core",
"cranelift",
"engine",
"jit",
"singlepass",
"static-artifact-create",
"static-artifact-load",
"sys",
"sys-default",
"wasmer-artifact-create",
"wasmer-artifact-load",
"compiler",
"core",
"cranelift",
"engine",
"jit",
"singlepass",
"static-artifact-create",
"static-artifact-load",
"sys",
"sys-default",
"wasmer-artifact-create",
"wasmer-artifact-load",
]
rustc-args = ["--cfg", "docsrs"]
36 changes: 21 additions & 15 deletions lib/api/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::sys::engine as engine_imp;
#[cfg(feature = "sys")]
pub(crate) use crate::sys::engine::default_engine;
#[cfg(feature = "sys")]
use std::io::Read;
use crate::IntoBytes;
#[cfg(feature = "sys")]
use shared_buffer::OwnedBuffer;
#[cfg(feature = "sys")]
use std::path::Path;
#[cfg(feature = "sys")]
Expand Down Expand Up @@ -45,9 +47,12 @@ impl Engine {
/// See [`Artifact::deserialize_unchecked`].
pub unsafe fn deserialize_unchecked(
&self,
bytes: &[u8],
bytes: impl IntoBytes,
) -> Result<Arc<Artifact>, DeserializeError> {
Ok(Arc::new(Artifact::deserialize_unchecked(&self.0, bytes)?))
Ok(Arc::new(Artifact::deserialize_unchecked(
&self.0,
bytes.into_bytes().into(),
)?))
}

#[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
Expand All @@ -56,8 +61,14 @@ impl Engine {
///
/// # Safety
/// See [`Artifact::deserialize`].
pub unsafe fn deserialize(&self, bytes: &[u8]) -> Result<Arc<Artifact>, DeserializeError> {
Ok(Arc::new(Artifact::deserialize(&self.0, bytes)?))
pub unsafe fn deserialize(
&self,
bytes: impl IntoBytes,
) -> Result<Arc<Artifact>, DeserializeError> {
Ok(Arc::new(Artifact::deserialize(
&self.0,
bytes.into_bytes().into(),
)?))
}

#[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
Expand All @@ -71,13 +82,11 @@ impl Engine {
&self,
file_ref: &Path,
) -> Result<Arc<Artifact>, DeserializeError> {
let mut file = std::fs::File::open(file_ref)?;
let mut buffer = Vec::new();
// read the whole file
file.read_to_end(&mut buffer)?;
let file = std::fs::File::open(file_ref)?;
Ok(Arc::new(Artifact::deserialize_unchecked(
&self.0,
buffer.as_slice(),
OwnedBuffer::from_file(&file)
.map_err(|e| DeserializeError::Generic(format!("{e:?}")))?,
)?))
}

Expand All @@ -90,11 +99,8 @@ impl Engine {
&self,
file_ref: &Path,
) -> Result<Arc<Artifact>, DeserializeError> {
let mut file = std::fs::File::open(file_ref)?;
let mut buffer = Vec::new();
// read the whole file
file.read_to_end(&mut buffer)?;
Ok(Arc::new(Artifact::deserialize(&self.0, buffer.as_slice())?))
let bytes = std::fs::read(file_ref)?;
Ok(Arc::new(Artifact::deserialize(&self.0, bytes.into())?))
}
}

Expand Down
48 changes: 47 additions & 1 deletion lib/api/src/sys/engine.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::{path::Path, sync::Arc};

use shared_buffer::OwnedBuffer;
pub use wasmer_compiler::{
Artifact, BaseTunables, CompilerConfig, Engine, EngineBuilder, Tunables,
};
#[cfg(feature = "compiler")]
use wasmer_types::Features;
use wasmer_types::Target;
use wasmer_types::{DeserializeError, Target};

/// Returns the default engine for the Sys engine
pub(crate) fn default_engine() -> Engine {
Expand Down Expand Up @@ -83,6 +86,26 @@ pub trait NativeEngineExt {

/// Get a reference to attached Tunable of this engine
fn tunables(&self) -> &dyn Tunables;

/// Load a serialized WebAssembly module from a memory mapped file and deserialize it.
///
/// NOTE: you should almost always prefer [`Self::deserialize_from_mmapped_file`].
///
/// # Safety
/// See [`Artifact::deserialize_unchecked`].
unsafe fn deserialize_from_mmapped_file_unchecked(
&self,
file_ref: &Path,
) -> Result<Arc<Artifact>, DeserializeError>;

/// Load a serialized WebAssembly module from a memory mapped file and deserialize it.
///
/// # Safety
/// See [`Artifact::deserialize`].
unsafe fn deserialize_from_mmapped_file(
&self,
file_ref: &Path,
) -> Result<Arc<Artifact>, DeserializeError>;
}

impl NativeEngineExt for crate::engine::Engine {
Expand All @@ -106,4 +129,27 @@ impl NativeEngineExt for crate::engine::Engine {
fn tunables(&self) -> &dyn Tunables {
self.0.tunables()
}

unsafe fn deserialize_from_mmapped_file_unchecked(
&self,
file_ref: &Path,
) -> Result<Arc<Artifact>, DeserializeError> {
let bytes = std::fs::read(file_ref)?;
Ok(Arc::new(Artifact::deserialize_unchecked(
&self.0,
bytes.into(),
)?))
}

unsafe fn deserialize_from_mmapped_file(
&self,
file_ref: &Path,
) -> Result<Arc<Artifact>, DeserializeError> {
let file = std::fs::File::open(file_ref)?;
Ok(Arc::new(Artifact::deserialize(
&self.0,
OwnedBuffer::from_file(&file)
.map_err(|e| DeserializeError::Generic(format!("{e:?}")))?,
)?))
}
}
8 changes: 6 additions & 2 deletions lib/api/src/sys/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Module {
.as_engine_ref()
.engine()
.0
.deserialize_unchecked(&bytes)?;
.deserialize_unchecked(bytes.into())?;
Ok(Self::from_artifact(artifact))
}

Expand All @@ -88,7 +88,11 @@ impl Module {
bytes: impl IntoBytes,
) -> Result<Self, DeserializeError> {
let bytes = bytes.into_bytes();
let artifact = engine.as_engine_ref().engine().0.deserialize(&bytes)?;
let artifact = engine
.as_engine_ref()
.engine()
.0
.deserialize(bytes.into())?;
Ok(Self::from_artifact(artifact))
}

Expand Down
7 changes: 6 additions & 1 deletion lib/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ cfg-if = "1.0"
leb128 = "0.2"
enum-iterator = "0.7.0"

bytes = "1.0"
self_cell = "1.0"
rkyv = { version = "0.7.40", features = ["indexmap", "validation", "strict"] }
shared-buffer = "0.1"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
wasmer-vm = { path = "../vm", version = "=4.1.2" }
region = { version = "3.0" }
Expand All @@ -40,7 +45,7 @@ region = { version = "3.0" }
winapi = { version = "0.3", features = ["winnt", "impl-default"] }

[features]
default = ["std" ]
default = ["std"]
# This feature is for compiler implementors, it enables using `Compiler` and
# `CompilerConfig`, as well as the included wasmparser.
# Disable this feature if you just want a headless engine.
Expand Down
Loading

0 comments on commit 5fc8974

Please sign in to comment.