From 05d68ab3c13d2073818c19f6b8c8e720cd9d0208 Mon Sep 17 00:00:00 2001 From: Iris Pupo Date: Tue, 8 Nov 2022 17:40:50 -0500 Subject: [PATCH 1/4] enabled wasmtime built in caching --- src/backend/wasmtime.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/backend/wasmtime.rs b/src/backend/wasmtime.rs index 6551565..7df636a 100644 --- a/src/backend/wasmtime.rs +++ b/src/backend/wasmtime.rs @@ -13,12 +13,14 @@ type Linker = wasmtime::Linker; pub struct WasmtimeBackend { engine: wasmtime::Engine, } + impl WasmtimeBackend { pub fn new() -> anyhow::Result { let mut config = wasmtime::Config::new(); config.wasm_simd(true); config.wasm_bulk_memory(true); config.cranelift_opt_level(wasmtime::OptLevel::Speed); + config.cache_config_load_default()?; let engine = wasmtime::Engine::new(&config)?; @@ -26,7 +28,9 @@ impl WasmtimeBackend { } } + impl Backend for WasmtimeBackend { + fn load_module(&self, module: &[u8]) -> anyhow::Result> { let module = wasmtime::Module::new(&self.engine, module)?; let abi = ScriptAbiImpl::default(); From 57768d041a2742d09675b22821afb97b451a8642 Mon Sep 17 00:00:00 2001 From: Iris Pupo Date: Fri, 25 Nov 2022 19:43:58 -0500 Subject: [PATCH 2/4] Added module caching --- Cargo.toml | 1 + src/backend/wasmtime.rs | 39 ++++++++++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fe2e0dd..55b8413 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ ouroboros = "^0.15" parking_lot = "0.12" slab = "0.4" wasmtime = "0.38" +prehash = "0.3.3" [dependencies.font-kit] version = "*" diff --git a/src/backend/wasmtime.rs b/src/backend/wasmtime.rs index 7df636a..22aaacd 100644 --- a/src/backend/wasmtime.rs +++ b/src/backend/wasmtime.rs @@ -1,4 +1,10 @@ -use std::ops::DerefMut; +use std::collections::hash_map::RandomState; +use std::hash::{Hash, BuildHasher}; +use std::{ops::DerefMut, collections::hash_map}; +use std::collections::HashMap; + +use std::collections::hash_map::DefaultHasher; +use std::hash::Hasher; use super::{Arc, Backend, Instance, PanelId}; use crate::{DrawCommand, ScriptAbi, ScriptAbiImpl}; @@ -9,9 +15,14 @@ use parking_lot::Mutex; type Caller<'a> = wasmtime::Caller<'a, ScriptAbiImpl>; type Store = wasmtime::Store; type Linker = wasmtime::Linker; +type ModuleCache = Mutex, wasmtime::Module, DefaultPrehasher>>; + +use prehash::{new_prehashed_set, DefaultPrehasher, Prehasher, Prehashed}; +use wasmtime::Module; pub struct WasmtimeBackend { engine: wasmtime::Engine, + module_cache: ModuleCache, } impl WasmtimeBackend { @@ -23,21 +34,35 @@ impl WasmtimeBackend { config.cache_config_load_default()?; let engine = wasmtime::Engine::new(&config)?; - - Ok(Self { engine }) + let module_cache = Default::default(); + Ok(Self { engine, module_cache }) } } - impl Backend for WasmtimeBackend { - fn load_module(&self, module: &[u8]) -> anyhow::Result> { - let module = wasmtime::Module::new(&self.engine, module)?; + + let mut hasher = DefaultHasher::new(); + hasher.write(module); + let hashed = hasher.finish(); + + let prehasher = DefaultPrehasher::new(); + let prehashed = prehasher.prehash(hashed); + let mut cache = self.module_cache.lock(); + + let gotten = if let Some(module) = cache.get(&prehashed){ + module + } else { + let module = wasmtime::Module::new(&self.engine, module)?; + cache.insert(prehashed, module); + cache.get(&prehashed).unwrap() + }; + let abi = ScriptAbiImpl::default(); let mut store = wasmtime::Store::new(&self.engine, abi); let mut linker = Linker::new(&self.engine); WasmtimeInstance::link(&mut linker)?; - let instance = linker.instantiate(&mut store, &module)?; + let instance = linker.instantiate(&mut store, gotten)?; let bind_panel = instance.get_typed_func(&mut store, "bind_panel")?; let update = instance.get_typed_func(&mut store, "update")?; let draw = instance.get_typed_func(&mut store, "draw")?; From 57c627595826095f078d3234f2f53cbbab4c6d48 Mon Sep 17 00:00:00 2001 From: mars Date: Fri, 25 Nov 2022 17:49:38 -0700 Subject: [PATCH 3/4] Sort prehash crate dep alphabetically --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 55b8413..287df87 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,9 +22,9 @@ canary-script = { path = "crates/script" } lyon = "1" ouroboros = "^0.15" parking_lot = "0.12" +prehash = "0.3.3" slab = "0.4" wasmtime = "0.38" -prehash = "0.3.3" [dependencies.font-kit] version = "*" From d43a62dfda01c59f92e89a59c51f1b6767b1bf8c Mon Sep 17 00:00:00 2001 From: mars Date: Fri, 25 Nov 2022 17:52:24 -0700 Subject: [PATCH 4/4] Clean up wasmtime caching code --- src/backend/wasmtime.rs | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/backend/wasmtime.rs b/src/backend/wasmtime.rs index 22aaacd..2f7f553 100644 --- a/src/backend/wasmtime.rs +++ b/src/backend/wasmtime.rs @@ -1,25 +1,19 @@ -use std::collections::hash_map::RandomState; -use std::hash::{Hash, BuildHasher}; -use std::{ops::DerefMut, collections::hash_map}; -use std::collections::HashMap; - -use std::collections::hash_map::DefaultHasher; +use std::collections::{hash_map::DefaultHasher, HashMap}; use std::hash::Hasher; +use std::ops::DerefMut; use super::{Arc, Backend, Instance, PanelId}; use crate::{DrawCommand, ScriptAbi, ScriptAbiImpl}; use canary_script::{Color, CursorEventKind, Rect, Vec2}; use parking_lot::Mutex; +use prehash::{DefaultPrehasher, Prehashed, Prehasher}; type Caller<'a> = wasmtime::Caller<'a, ScriptAbiImpl>; type Store = wasmtime::Store; type Linker = wasmtime::Linker; type ModuleCache = Mutex, wasmtime::Module, DefaultPrehasher>>; -use prehash::{new_prehashed_set, DefaultPrehasher, Prehasher, Prehashed}; -use wasmtime::Module; - pub struct WasmtimeBackend { engine: wasmtime::Engine, module_cache: ModuleCache, @@ -35,34 +29,36 @@ impl WasmtimeBackend { let engine = wasmtime::Engine::new(&config)?; let module_cache = Default::default(); - Ok(Self { engine, module_cache }) + Ok(Self { + engine, + module_cache, + }) } } impl Backend for WasmtimeBackend { fn load_module(&self, module: &[u8]) -> anyhow::Result> { - let mut hasher = DefaultHasher::new(); hasher.write(module); let hashed = hasher.finish(); let prehasher = DefaultPrehasher::new(); let prehashed = prehasher.prehash(hashed); - let mut cache = self.module_cache.lock(); + let mut cache = self.module_cache.lock(); - let gotten = if let Some(module) = cache.get(&prehashed){ + let module = if let Some(module) = cache.get(&prehashed) { module } else { let module = wasmtime::Module::new(&self.engine, module)?; cache.insert(prehashed, module); cache.get(&prehashed).unwrap() }; - + let abi = ScriptAbiImpl::default(); let mut store = wasmtime::Store::new(&self.engine, abi); let mut linker = Linker::new(&self.engine); WasmtimeInstance::link(&mut linker)?; - let instance = linker.instantiate(&mut store, gotten)?; + let instance = linker.instantiate(&mut store, module)?; let bind_panel = instance.get_typed_func(&mut store, "bind_panel")?; let update = instance.get_typed_func(&mut store, "update")?; let draw = instance.get_typed_func(&mut store, "draw")?;