From 1a5b46b690da991eb0f5a34290d1eedb1b63df06 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 14 Dec 2024 11:53:59 -0700 Subject: [PATCH 1/9] chore: clippy --- test/benches/bench.rs | 26 +++++++++----------------- test/src/test.rs | 12 ++++++------ 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/test/benches/bench.rs b/test/benches/bench.rs index 72338627..38370b13 100644 --- a/test/benches/bench.rs +++ b/test/benches/bench.rs @@ -13,12 +13,10 @@ use wasmer::Store; pub fn wasm_module_compile(c: &mut Criterion) { let mut group = c.benchmark_group("wasm_module_compile"); - for wasm in vec![ - TestWasm::Empty, + for wasm in [TestWasm::Empty, TestWasm::Io, TestWasm::Test, - TestWasm::Memory, - ] { + TestWasm::Memory] { group.bench_function(BenchmarkId::new("wasm_module_compile", wasm.name()), |b| { b.iter(|| { Module::from_binary(&Store::default(), wasm.bytes()).unwrap(); @@ -31,12 +29,10 @@ pub fn wasm_module_compile(c: &mut Criterion) { pub fn wasm_module_deserialize_from_file(c: &mut Criterion) { let mut group = c.benchmark_group("wasm_module_deserialize_from_file"); - for wasm in vec![ - TestWasm::Empty, + for wasm in [TestWasm::Empty, TestWasm::Io, TestWasm::Test, - TestWasm::Memory, - ] { + TestWasm::Memory] { let tmpdir = TempDir::new().unwrap(); let path = tmpdir.path().join(wasm.name()); let module = Module::from_binary(&Store::default(), wasm.bytes()).unwrap(); @@ -56,12 +52,10 @@ pub fn wasm_module_deserialize_from_file(c: &mut Criterion) { pub fn wasm_module(c: &mut Criterion) { let mut group = c.benchmark_group("wasm_module"); - for wasm in vec![ - TestWasm::Empty, + for wasm in [TestWasm::Empty, TestWasm::Io, TestWasm::Test, - TestWasm::Memory, - ] { + TestWasm::Memory] { group.bench_function(BenchmarkId::new("wasm_module", wasm.name()), |b| { b.iter(|| { wasm.module(false); @@ -76,12 +70,10 @@ pub fn wasm_module(c: &mut Criterion) { pub fn wasm_instance(c: &mut Criterion) { let mut group = c.benchmark_group("wasm_instance"); - for wasm in vec![ - TestWasm::Empty, + for wasm in [TestWasm::Empty, TestWasm::Io, TestWasm::Test, - TestWasm::Memory, - ] { + TestWasm::Memory] { group.bench_function(BenchmarkId::new("wasm_instance", wasm.name()), |b| { b.iter(|| { let _drop = wasm.unmetered_instance(); @@ -213,7 +205,7 @@ pub fn test_process_string(c: &mut Criterion) { let instance_with_store = TestWasm::Test.unmetered_instance(); - for n in vec![0, 1, 1_000, 1_000_000] { + for n in [0, 1, 1_000, 1_000_000] { group.throughput(Throughput::Bytes(n)); group.sample_size(10); let input = test_common::StringType::from(".".repeat(n.try_into().unwrap())); diff --git a/test/src/test.rs b/test/src/test.rs index 0e4c12a7..bfcd95ac 100644 --- a/test/src/test.rs +++ b/test/src/test.rs @@ -239,7 +239,7 @@ pub mod tests { &mut store.lock().as_store_mut(), instance, "ignore_args_process_string", - &StringType::from(String::new()), + StringType::from(String::new()), ) .expect("ignore_args_process_string call"); assert_eq!(String::new(), String::from(result)); @@ -254,7 +254,7 @@ pub mod tests { &mut store.lock().as_store_mut(), instance, "process_string", - &StringType::from(s.clone()), + StringType::from(s.clone()), ) .expect("process string call"); @@ -268,14 +268,14 @@ pub mod tests { // use a "crazy" string that is much longer than a single wasm page to show that pagination // and utf-8 are both working OK let starter_string = "╰▐ ✖ 〜 ✖ ▐╯" - .repeat(usize::try_from(10_u32 * u32::try_from(std::u16::MAX).unwrap()).unwrap()); + .repeat(usize::try_from(10_u32 * u32::from(u16::MAX)).unwrap()); let InstanceWithStore { store, instance } = TestWasm::Test.instance(); let result: StringType = guest::call( &mut store.lock().as_store_mut(), instance, "process_string", // This is by reference just to show that it can be done as borrowed or owned. - &StringType::from(starter_string.clone()), + StringType::from(starter_string.clone()), ) .expect("process string call"); @@ -320,8 +320,8 @@ pub mod tests { ) } }); - assert!(matches!(call_1.join(), Ok(_))); - assert!(matches!(call_2.join(), Ok(_))); + assert!(call_1.join().is_ok()); + assert!(call_2.join().is_ok()); } #[test] From 5f5813f309c211644102734ee4ac0c48912d9b17 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 14 Dec 2024 11:55:47 -0700 Subject: [PATCH 2/9] chore: meaningless empty features --- crates/guest/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/guest/Cargo.toml b/crates/guest/Cargo.toml index 08f4c3c3..bcceed65 100644 --- a/crates/guest/Cargo.toml +++ b/crates/guest/Cargo.toml @@ -14,7 +14,7 @@ crate-type = ["cdylib", "rlib"] path = "src/guest.rs" [dependencies] -holochain_serialized_bytes = { version = "=0.0.55", features = [] } +holochain_serialized_bytes = "=0.0.55" holochain_wasmer_common = { version = "=0.0.96", path = "../common" } serde = "1" tracing = "0.1" From e79ec8a1a96a18f369dc44eae63a07ddd7cde193 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 14 Dec 2024 11:58:10 -0700 Subject: [PATCH 3/9] chore: make internal-only types & crates private --- crates/host/src/module/mod.rs | 24 ++++++------------------ crates/host/src/module/wasmer_sys.rs | 4 ++-- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/crates/host/src/module/mod.rs b/crates/host/src/module/mod.rs index ee83528c..33822533 100644 --- a/crates/host/src/module/mod.rs +++ b/crates/host/src/module/mod.rs @@ -11,15 +11,11 @@ use std::fs::File; use std::fs::OpenOptions; use std::io::Write; use std::path::PathBuf; -use std::str::FromStr; use std::sync::Arc; -use wasmer::CpuFeature; use wasmer::Engine; use wasmer::Instance; use wasmer::Module; use wasmer::Store; -use wasmer::Target; -use wasmer::Triple; #[cfg(feature = "wasmer_sys")] mod wasmer_sys; @@ -36,9 +32,9 @@ pub use wasmer_wamr::*; pub type CacheKey = [u8; 32]; /// Plru uses a usize to track "recently used" so we need a map between 32 byte cache /// keys and the bits used to evict things from the cache. -pub type PlruKeyMap = BiMap; +type PlruKeyMap = BiMap; /// Modules serialize to a vec of bytes as per wasmer. -pub type SerializedModule = Bytes; +type SerializedModule = Bytes; #[derive(Clone, Debug)] pub struct ModuleWithStore { @@ -56,7 +52,7 @@ pub struct InstanceWithStore { /// with consistently. Default implementations for key functions are provided. /// Notably handles keeping the mapping between cache keys and items, and the /// plru tracking including touching and evicting. -pub trait PlruCache { +trait PlruCache { /// The type of items in the cache. type Item; /// Accessor for mutable reference to internal plru cache. @@ -104,12 +100,14 @@ pub trait PlruCache { /// Delete the plru for a given cache key. Care must be taken to ensure this /// is not called before a subsequent call to `plru_key` or it will panic. + #[allow(dead_code)] fn trash(&mut self, key: &CacheKey) { let plru_key = self.plru_key(key); self.plru_mut().trash(plru_key); } /// Remove an item from the cache and the associated plru entry. + #[allow(dead_code)] fn remove_item(&mut self, key: &CacheKey) -> Option> { let maybe_item = self.cache_mut().remove(key); if maybe_item.is_some() { @@ -312,7 +310,7 @@ impl SerializedModuleCache { /// the cache to create callable instances is slow. Therefore modules are /// cached in memory after deserialization. #[derive(Default, Debug)] -pub struct DeserializedModuleCache { +struct DeserializedModuleCache { plru: MicroCache, key_map: PlruKeyMap, cache: BTreeMap>, @@ -385,16 +383,6 @@ impl ModuleCache { } } -/// Configuration of a Target for wasmer for iOS -pub fn wasmer_ios_target() -> Target { - // use what I see in - // platform ios headless example - // https://github.com/wasmerio/wasmer/blob/447c2e3a152438db67be9ef649327fabcad6f5b8/examples/platform_ios_headless.rs#L38-L53 - let triple = Triple::from_str("aarch64-apple-ios").unwrap(); - let cpu_feature = CpuFeature::set(); - Target::new(triple, cpu_feature) -} - #[cfg(test)] pub mod tests { use crate::module::{CacheKey, ModuleCache, PlruCache}; diff --git a/crates/host/src/module/wasmer_sys.rs b/crates/host/src/module/wasmer_sys.rs index 2eadd709..dbba3d35 100644 --- a/crates/host/src/module/wasmer_sys.rs +++ b/crates/host/src/module/wasmer_sys.rs @@ -23,7 +23,7 @@ pub const WASM_METERING_LIMIT: u64 = 10_000_000; /// Generate an engine with a wasm compiler /// and Metering (use limits) in place. -pub fn make_engine() -> Engine { +pub(crate) fn make_engine() -> Engine { let cost_function = |_operator: &wasmparser::Operator| -> u64 { 1 }; // @todo 100 giga-ops is totally arbitrary cutoff so we probably // want to make the limit configurable somehow. @@ -50,7 +50,7 @@ pub fn make_engine() -> Engine { engine } -pub fn make_runtime_engine() -> Engine { +pub(crate) fn make_runtime_engine() -> Engine { Engine::headless() } From 22538249c6ec0cce67e61e6972cd864b282651fb Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 14 Dec 2024 11:58:49 -0700 Subject: [PATCH 4/9] chore: make scripts executable --- scripts/bench.sh | 0 scripts/test.sh | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 scripts/bench.sh mode change 100644 => 100755 scripts/test.sh diff --git a/scripts/bench.sh b/scripts/bench.sh old mode 100644 new mode 100755 diff --git a/scripts/test.sh b/scripts/test.sh old mode 100644 new mode 100755 From 773d114938307035fade483a8026aa0858fe8d0c Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 14 Dec 2024 11:59:45 -0700 Subject: [PATCH 5/9] chore: fmt --- test/benches/bench.rs | 24 ++++++++++++++++-------- test/src/test.rs | 4 ++-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/test/benches/bench.rs b/test/benches/bench.rs index 38370b13..1d566c5a 100644 --- a/test/benches/bench.rs +++ b/test/benches/bench.rs @@ -13,10 +13,12 @@ use wasmer::Store; pub fn wasm_module_compile(c: &mut Criterion) { let mut group = c.benchmark_group("wasm_module_compile"); - for wasm in [TestWasm::Empty, + for wasm in [ + TestWasm::Empty, TestWasm::Io, TestWasm::Test, - TestWasm::Memory] { + TestWasm::Memory, + ] { group.bench_function(BenchmarkId::new("wasm_module_compile", wasm.name()), |b| { b.iter(|| { Module::from_binary(&Store::default(), wasm.bytes()).unwrap(); @@ -29,10 +31,12 @@ pub fn wasm_module_compile(c: &mut Criterion) { pub fn wasm_module_deserialize_from_file(c: &mut Criterion) { let mut group = c.benchmark_group("wasm_module_deserialize_from_file"); - for wasm in [TestWasm::Empty, + for wasm in [ + TestWasm::Empty, TestWasm::Io, TestWasm::Test, - TestWasm::Memory] { + TestWasm::Memory, + ] { let tmpdir = TempDir::new().unwrap(); let path = tmpdir.path().join(wasm.name()); let module = Module::from_binary(&Store::default(), wasm.bytes()).unwrap(); @@ -52,10 +56,12 @@ pub fn wasm_module_deserialize_from_file(c: &mut Criterion) { pub fn wasm_module(c: &mut Criterion) { let mut group = c.benchmark_group("wasm_module"); - for wasm in [TestWasm::Empty, + for wasm in [ + TestWasm::Empty, TestWasm::Io, TestWasm::Test, - TestWasm::Memory] { + TestWasm::Memory, + ] { group.bench_function(BenchmarkId::new("wasm_module", wasm.name()), |b| { b.iter(|| { wasm.module(false); @@ -70,10 +76,12 @@ pub fn wasm_module(c: &mut Criterion) { pub fn wasm_instance(c: &mut Criterion) { let mut group = c.benchmark_group("wasm_instance"); - for wasm in [TestWasm::Empty, + for wasm in [ + TestWasm::Empty, TestWasm::Io, TestWasm::Test, - TestWasm::Memory] { + TestWasm::Memory, + ] { group.bench_function(BenchmarkId::new("wasm_instance", wasm.name()), |b| { b.iter(|| { let _drop = wasm.unmetered_instance(); diff --git a/test/src/test.rs b/test/src/test.rs index bfcd95ac..db18b2cd 100644 --- a/test/src/test.rs +++ b/test/src/test.rs @@ -267,8 +267,8 @@ pub mod tests { fn process_string_test() { // use a "crazy" string that is much longer than a single wasm page to show that pagination // and utf-8 are both working OK - let starter_string = "╰▐ ✖ 〜 ✖ ▐╯" - .repeat(usize::try_from(10_u32 * u32::from(u16::MAX)).unwrap()); + let starter_string = + "╰▐ ✖ 〜 ✖ ▐╯".repeat(usize::try_from(10_u32 * u32::from(u16::MAX)).unwrap()); let InstanceWithStore { store, instance } = TestWasm::Test.instance(); let result: StringType = guest::call( &mut store.lock().as_store_mut(), From c3354aa720956a65c188f5ef37e06af0a450e4fd Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 14 Dec 2024 12:03:12 -0700 Subject: [PATCH 6/9] fix: high-level scripts run from root directory --- scripts/bench.sh | 6 +++--- scripts/test.sh | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/bench.sh b/scripts/bench.sh index 3dad9a66..ab0ff8aa 100755 --- a/scripts/bench.sh +++ b/scripts/bench.sh @@ -1,5 +1,5 @@ #! /usr/bin/env bash -./bench-wasmer_sys_dev.sh -./bench-wasmer_sys_prod.sh -./bench-wasmer_wamr.sh \ No newline at end of file +./scripts/bench-wasmer_sys_dev.sh +./scripts/bench-wasmer_sys_prod.sh +./scripts/bench-wasmer_wamr.sh \ No newline at end of file diff --git a/scripts/test.sh b/scripts/test.sh index f27eaeba..4e1cce97 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -1,5 +1,5 @@ #! /usr/bin/env bash -./test-wasmer_sys_dev.sh -./test-wasmer_sys_prod.sh -./test-wasmer_wamr.sh \ No newline at end of file +./scripts/test-wasmer_sys_dev.sh +./scripts/test-wasmer_sys_prod.sh +./scripts/test-wasmer_wamr.sh \ No newline at end of file From 41ac6071e65579aadc4e39681b1bfa1bee847221 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 14 Dec 2024 12:06:37 -0700 Subject: [PATCH 7/9] chore: rename file --- crates/host/src/{module/mod.rs => module.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename crates/host/src/{module/mod.rs => module.rs} (100%) diff --git a/crates/host/src/module/mod.rs b/crates/host/src/module.rs similarity index 100% rename from crates/host/src/module/mod.rs rename to crates/host/src/module.rs From c366edf1c20379e3ebde85723b763f17d7fa9ce0 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 14 Dec 2024 12:12:43 -0700 Subject: [PATCH 8/9] chore: make internal fns private --- crates/host/src/module/wasmer_wamr.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/host/src/module/wasmer_wamr.rs b/crates/host/src/module/wasmer_wamr.rs index 4852b240..07cf51b3 100644 --- a/crates/host/src/module/wasmer_wamr.rs +++ b/crates/host/src/module/wasmer_wamr.rs @@ -7,11 +7,11 @@ use wasmer::Module; /// Generate an engine with a wasm interpreter /// The interpreter used (wasm micro runtime) does not support metering /// See tracking issue: https://github.com/bytecodealliance/wasm-micro-runtime/issues/2163 -pub fn make_engine() -> Engine { +pub(crate) fn make_engine() -> Engine { Engine::default() } -pub fn make_runtime_engine() -> Engine { +pub(crate) fn make_runtime_engine() -> Engine { Engine::default() } From 792b1dd019b0d3ade7e051f06a6b2e6538c68f1f Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Mon, 16 Dec 2024 11:31:44 -0700 Subject: [PATCH 9/9] chore: remove dead code --- crates/host/src/module.rs | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/crates/host/src/module.rs b/crates/host/src/module.rs index 33822533..0d6fc181 100644 --- a/crates/host/src/module.rs +++ b/crates/host/src/module.rs @@ -98,26 +98,6 @@ trait PlruCache { self.plru_mut().touch(plru_key); } - /// Delete the plru for a given cache key. Care must be taken to ensure this - /// is not called before a subsequent call to `plru_key` or it will panic. - #[allow(dead_code)] - fn trash(&mut self, key: &CacheKey) { - let plru_key = self.plru_key(key); - self.plru_mut().trash(plru_key); - } - - /// Remove an item from the cache and the associated plru entry. - #[allow(dead_code)] - fn remove_item(&mut self, key: &CacheKey) -> Option> { - let maybe_item = self.cache_mut().remove(key); - if maybe_item.is_some() { - let plru_key = self.plru_key(key); - self.plru_mut().trash(plru_key); - self.key_map_mut().remove_by_left(&plru_key); - } - maybe_item - } - /// Attempt to retrieve an item from the cache by its cache key. fn get_item(&mut self, key: &CacheKey) -> Option> { let maybe_item = self.cache().get(key).cloned();