Skip to content

Commit

Permalink
Chore/cleanup make private (#130)
Browse files Browse the repository at this point in the history
* chore: clippy

* chore: meaningless empty features

* chore: make internal-only types & crates private

* chore: make scripts executable

* chore: fmt

* fix: high-level scripts run from root directory

* chore: rename file

* chore: make internal fns private

* chore: remove dead code
  • Loading branch information
mattyg authored Dec 16, 2024
1 parent b7eb114 commit 3b0b4f3
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 59 deletions.
2 changes: 1 addition & 1 deletion crates/guest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 4 additions & 36 deletions crates/host/src/module/mod.rs → crates/host/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<usize, CacheKey>;
type PlruKeyMap = BiMap<usize, CacheKey>;
/// Modules serialize to a vec of bytes as per wasmer.
pub type SerializedModule = Bytes;
type SerializedModule = Bytes;

#[derive(Clone, Debug)]
pub struct ModuleWithStore {
Expand All @@ -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.
Expand Down Expand Up @@ -102,24 +98,6 @@ pub 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.
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.
fn remove_item(&mut self, key: &CacheKey) -> Option<Arc<Self::Item>> {
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<Arc<Self::Item>> {
let maybe_item = self.cache().get(key).cloned();
Expand Down Expand Up @@ -312,7 +290,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<CacheKey, Arc<Module>>,
Expand Down Expand Up @@ -385,16 +363,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};
Expand Down
4 changes: 2 additions & 2 deletions crates/host/src/module/wasmer_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
}

Expand Down
4 changes: 2 additions & 2 deletions crates/host/src/module/wasmer_wamr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
6 changes: 3 additions & 3 deletions scripts/bench.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#! /usr/bin/env bash

./bench-wasmer_sys_dev.sh
./bench-wasmer_sys_prod.sh
./bench-wasmer_wamr.sh
./scripts/bench-wasmer_sys_dev.sh
./scripts/bench-wasmer_sys_prod.sh
./scripts/bench-wasmer_wamr.sh
6 changes: 3 additions & 3 deletions scripts/test.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#! /usr/bin/env bash

./test-wasmer_sys_dev.sh
./test-wasmer_sys_prod.sh
./test-wasmer_wamr.sh
./scripts/test-wasmer_sys_dev.sh
./scripts/test-wasmer_sys_prod.sh
./scripts/test-wasmer_wamr.sh
10 changes: 5 additions & 5 deletions test/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use wasmer::Store;
pub fn wasm_module_compile(c: &mut Criterion) {
let mut group = c.benchmark_group("wasm_module_compile");

for wasm in vec![
for wasm in [
TestWasm::Empty,
TestWasm::Io,
TestWasm::Test,
Expand All @@ -31,7 +31,7 @@ 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![
for wasm in [
TestWasm::Empty,
TestWasm::Io,
TestWasm::Test,
Expand All @@ -56,7 +56,7 @@ 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![
for wasm in [
TestWasm::Empty,
TestWasm::Io,
TestWasm::Test,
Expand All @@ -76,7 +76,7 @@ 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![
for wasm in [
TestWasm::Empty,
TestWasm::Io,
TestWasm::Test,
Expand Down Expand Up @@ -213,7 +213,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()));
Expand Down
14 changes: 7 additions & 7 deletions test/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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");

Expand All @@ -267,15 +267,15 @@ 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::try_from(std::u16::MAX).unwrap()).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(),
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");

Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 3b0b4f3

Please sign in to comment.