Skip to content

Commit 5b9e220

Browse files
committed
Split the spawn functions so they can be invoked separately in a more performant way
1 parent fe3c6d0 commit 5b9e220

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

lib/wasix/src/bin_factory/exec.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ pub async fn spawn_exec(
3030
env: WasiEnv,
3131
runtime: &Arc<dyn Runtime + Send + Sync + 'static>,
3232
) -> Result<TaskJoinHandle, SpawnError> {
33+
// Load the WASM
34+
let wasm = spawn_load_wasm(&env, &binary, name).await?;
35+
36+
// Load the module
37+
let module = spawn_load_module(&env, name, wasm, runtime).await?;
38+
39+
// Spawn union the file system
40+
spawn_union_fs(&env, &binary).await?;
41+
42+
// Now run the module
43+
spawn_exec_module(module, env, runtime)
44+
}
45+
46+
pub async fn spawn_load_wasm<'a>(
47+
env: &WasiEnv,
48+
binary: &'a BinaryPackage,
49+
name: &str,
50+
) -> Result<&'a [u8], SpawnError> {
3351
let wasm = if let Some(cmd) = binary.get_command(name) {
3452
cmd.atom.as_ref()
3553
} else if let Some(wasm) = binary.entrypoint_bytes() {
@@ -44,7 +62,15 @@ pub async fn spawn_exec(
4462
env.on_exit(Some(Errno::Noexec.into())).await;
4563
return Err(SpawnError::CompileError);
4664
};
65+
Ok(wasm)
66+
}
4767

68+
pub async fn spawn_load_module(
69+
env: &WasiEnv,
70+
name: &str,
71+
wasm: &[u8],
72+
runtime: &Arc<dyn Runtime + Send + Sync + 'static>,
73+
) -> Result<Module, SpawnError> {
4874
let module = match runtime.load_module(wasm).await {
4975
Ok(module) => module,
5076
Err(err) => {
@@ -57,7 +83,10 @@ pub async fn spawn_exec(
5783
return Err(SpawnError::CompileError);
5884
}
5985
};
86+
Ok(module)
87+
}
6088

89+
pub async fn spawn_union_fs(env: &WasiEnv, binary: &BinaryPackage) -> Result<(), SpawnError> {
6190
// If the file system has not already been union'ed then do so
6291
env.state
6392
.fs
@@ -68,9 +97,7 @@ pub async fn spawn_exec(
6897
SpawnError::FileSystemError
6998
})?;
7099
tracing::debug!("{:?}", env.state.fs);
71-
72-
// Now run the module
73-
spawn_exec_module(module, env, runtime)
100+
Ok(())
74101
}
75102

76103
pub fn spawn_exec_module(

lib/wasix/src/bin_factory/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ mod exec;
1414

1515
pub use self::{
1616
binary_package::*,
17-
exec::{run_exec, spawn_exec, spawn_exec_module},
17+
exec::{
18+
run_exec, spawn_exec, spawn_exec_module, spawn_load_module, spawn_load_wasm, spawn_union_fs,
19+
},
1820
};
1921
use crate::{os::command::Commands, Runtime};
2022

lib/wasix/src/runtime/mod.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ where
104104
fn load_module<'a>(&'a self, wasm: &'a [u8]) -> BoxFuture<'a, anyhow::Result<Module>> {
105105
let engine = self.engine();
106106
let module_cache = self.module_cache();
107+
let hash = ModuleHash::hash(wasm);
107108

108-
let task = async move { load_module(&engine, &module_cache, wasm).await };
109+
let task = async move { load_module(&engine, &module_cache, wasm, hash).await };
109110

110111
Box::pin(task)
111112
}
@@ -150,16 +151,16 @@ pub async fn load_module(
150151
engine: &wasmer::Engine,
151152
module_cache: &(dyn ModuleCache + Send + Sync),
152153
wasm: &[u8],
154+
wasm_hash: ModuleHash,
153155
) -> Result<Module, anyhow::Error> {
154-
let hash = ModuleHash::hash(wasm);
155-
let result = module_cache.load(hash, engine).await;
156+
let result = module_cache.load(wasm_hash, engine).await;
156157

157158
match result {
158159
Ok(module) => return Ok(module),
159160
Err(CacheError::NotFound) => {}
160161
Err(other) => {
161162
tracing::warn!(
162-
%hash,
163+
%wasm_hash,
163164
error=&other as &dyn std::error::Error,
164165
"Unable to load the cached module",
165166
);
@@ -168,9 +169,9 @@ pub async fn load_module(
168169

169170
let module = Module::new(&engine, wasm)?;
170171

171-
if let Err(e) = module_cache.save(hash, engine, &module).await {
172+
if let Err(e) = module_cache.save(wasm_hash, engine, &module).await {
172173
tracing::warn!(
173-
%hash,
174+
%wasm_hash,
174175
error=&e as &dyn std::error::Error,
175176
"Unable to cache the compiled module",
176177
);
@@ -549,7 +550,9 @@ impl Runtime for OverriddenRuntime {
549550
if self.engine.is_some() || self.module_cache.is_some() {
550551
let engine = self.engine();
551552
let module_cache = self.module_cache();
552-
let task = async move { load_module(&engine, &module_cache, wasm).await };
553+
let hash = ModuleHash::hash(wasm);
554+
555+
let task = async move { load_module(&engine, &module_cache, wasm, hash).await };
553556
Box::pin(task)
554557
} else {
555558
self.inner.load_module(wasm)

0 commit comments

Comments
 (0)