Skip to content

Commit

Permalink
Merge pull request #4024 from wasmerio/wasmer-run-cache
Browse files Browse the repository at this point in the history
Save compiled `*.wasm` files to the module cache when they are loaded directly
  • Loading branch information
syrusakbary authored Jun 22, 2023
2 parents 51e3365 + ebe590e commit 7201dec
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
9 changes: 7 additions & 2 deletions lib/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,13 @@ impl ExecutableTarget {
);
}

Module::new(&engine, &wasm)
.with_context(|| format!("Unable to compile \"{}\"", path.display()))?
let module = tracing::debug_span!("compiling_wasm")
.in_scope(|| Module::new(&engine, &wasm))
.with_context(|| format!("Unable to compile \"{}\"", path.display()))?;

tasks.block_on(module_cache.save(module_hash, &engine, &module))?;

module
}
};

Expand Down
7 changes: 6 additions & 1 deletion lib/wasix/src/runtime/module_cache/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl FileSystemCache {

#[async_trait::async_trait]
impl ModuleCache for FileSystemCache {
#[tracing::instrument(level = "debug", skip_all, fields(%key))]
async fn load(&self, key: ModuleHash, engine: &Engine) -> Result<Module, CacheError> {
let path = self.path(key, engine.deterministic_id());

Expand All @@ -46,7 +47,10 @@ impl ModuleCache for FileSystemCache {

let res = unsafe { Module::deserialize(&engine, uncompressed) };
match res {
Ok(m) => Ok(m),
Ok(m) => {
tracing::debug!("Cache hit!");
Ok(m)
}
Err(e) => {
tracing::debug!(
%key,
Expand All @@ -69,6 +73,7 @@ impl ModuleCache for FileSystemCache {
}
}

#[tracing::instrument(level = "debug", skip_all, fields(%key))]
async fn save(
&self,
key: ModuleHash,
Expand Down
15 changes: 11 additions & 4 deletions lib/wasix/src/runtime/module_cache/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ impl SharedCache {

#[async_trait::async_trait]
impl ModuleCache for SharedCache {
#[tracing::instrument(level = "debug", skip_all, fields(%key))]
async fn load(&self, key: ModuleHash, engine: &Engine) -> Result<Module, CacheError> {
let key = (key, engine.deterministic_id().to_string());
self.modules
.get(&key)
.map(|m| m.value().clone())
.ok_or(CacheError::NotFound)

match self.modules.get(&key) {
Some(m) => {
tracing::debug!("Cache hit!");
Ok(m.value().clone())
}

None => Err(CacheError::NotFound),
}
}

#[tracing::instrument(level = "debug", skip_all, fields(%key))]
async fn save(
&self,
key: ModuleHash,
Expand Down
11 changes: 9 additions & 2 deletions lib/wasix/src/runtime/module_cache/thread_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,18 @@ impl ThreadLocalCache {

#[async_trait::async_trait]
impl ModuleCache for ThreadLocalCache {
#[tracing::instrument(level = "debug", skip_all, fields(%key))]
async fn load(&self, key: ModuleHash, engine: &Engine) -> Result<Module, CacheError> {
self.lookup(key, engine.deterministic_id())
.ok_or(CacheError::NotFound)
match self.lookup(key, engine.deterministic_id()) {
Some(m) => {
tracing::debug!("Cache hit!");
Ok(m)
}
None => Err(CacheError::NotFound),
}
}

#[tracing::instrument(level = "debug", skip_all, fields(%key))]
async fn save(
&self,
key: ModuleHash,
Expand Down

0 comments on commit 7201dec

Please sign in to comment.