From 792ed429a53a4116b97e1abdbc47f9bf615e6469 Mon Sep 17 00:00:00 2001 From: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com> Date: Thu, 14 Nov 2024 11:21:51 -0800 Subject: [PATCH] fix --- collections/perf.sh | 15 +- collections/rust/src/movm/Cargo.toml | 16 -- collections/rust/src/movm/src/lib.rs | 170 --------------- collections/rust/src/movm_dynamic/Cargo.toml | 16 -- collections/rust/src/movm_dynamic/src/lib.rs | 215 ------------------- 5 files changed, 2 insertions(+), 430 deletions(-) delete mode 100644 collections/rust/src/movm/Cargo.toml delete mode 100644 collections/rust/src/movm/src/lib.rs delete mode 100644 collections/rust/src/movm_dynamic/Cargo.toml delete mode 100644 collections/rust/src/movm_dynamic/src/lib.rs diff --git a/collections/perf.sh b/collections/perf.sh index 5c67c89c..bd9938ad 100644 --- a/collections/perf.sh +++ b/collections/perf.sh @@ -6,6 +6,7 @@ let mo_config = record { start_page = 16; page_limit = 128 }; let hashmap = wasm_profiling("motoko/.dfx/local/canisters/hashmap/hashmap.wasm", mo_config); let triemap = wasm_profiling("motoko/.dfx/local/canisters/triemap/triemap.wasm", mo_config); let rbtree = wasm_profiling("motoko/.dfx/local/canisters/rbtree/rbtree.wasm", mo_config); +let orderedmap = wasm_profiling("motoko/.dfx/local/canisters/orderedmap/orderedmap.wasm", mo_config); let splay = wasm_profiling("motoko/.dfx/local/canisters/splay/splay.wasm", mo_config); let btree = wasm_profiling("motoko/.dfx/local/canisters/btreemap/btreemap.wasm", mo_config); let zhenya = wasm_profiling("motoko/.dfx/local/canisters/zhenya_hashmap/zhenya_hashmap.wasm", mo_config); @@ -23,9 +24,6 @@ let imrc_hashmap_rs = wasm_profiling("rust/.dfx/local/canisters/imrc_hashmap/imr let vector_rs = wasm_profiling("rust/.dfx/local/canisters/vector/vector.wasm", rs_config); let vector_stable_rs = wasm_profiling("rust/.dfx/local/canisters/vector_stable/vector_stable.wasm", rs_config); -//let movm_rs = wasm_profiling("rust/.dfx/local/canisters/movm/movm.wasm"); -//let movm_dynamic_rs = wasm_profiling("rust/.dfx/local/canisters/movm_dynamic/movm_dynamic.wasm"); - let file = "README.md"; function perf(wasm, title, init, batch) { @@ -68,6 +66,7 @@ let batch_size = 50; output(file, "\n## Map\n\n| |binary_size|generate 1m|max mem|batch_get 50|batch_put 50|batch_remove 50|upgrade|\n|--:|--:|--:|--:|--:|--:|--:|--:|\n"); perf(hashmap, "hashmap", init_size, batch_size); perf(triemap, "triemap", init_size, batch_size); +perf(orderedmap, "orderedmap", init_size, batch_size); perf(rbtree, "rbtree", init_size, batch_size); perf(splay, "splay", init_size, batch_size); perf(btree, "btree", init_size, batch_size); @@ -96,13 +95,3 @@ perf(heap_rs, "heap_rs", init_size, batch_size); perf(heap_stable_rs, "heap_stable_rs", init_size, batch_size); perf(vector_rs, "vec_rs", init_size, batch_size); perf(vector_stable_rs, "vec_stable_rs", init_size, batch_size); - -/* -let movm_size = 10000; -output(file, "\n## MoVM\n\n| |binary_size|generate 10k|max mem|batch_get 50|batch_put 50|batch_remove 50|\n|--:|--:|--:|--:|--:|--:|--:|\n"); -perf(hashmap, "hashmap", movm_size); -perf(hashmap_rs, "hashmap_rs", movm_size); -perf(imrc_hashmap_rs, "imrc_hashmap_rs", movm_size); -perf(movm_rs, "movm_rs", movm_size); -perf(movm_dynamic_rs, "movm_dynamic_rs", movm_size); -*/ diff --git a/collections/rust/src/movm/Cargo.toml b/collections/rust/src/movm/Cargo.toml deleted file mode 100644 index a0b93eca..00000000 --- a/collections/rust/src/movm/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "movm" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[lib] -crate-type = ["cdylib"] - -[dependencies] -candid.workspace = true -ic-cdk.workspace = true -fxhash = "0.2.1" -motoko = { rev = "5897a8b57774b55782d8e408a254e045a9afba20", git = "https://github.com/dfinity/motoko.rs" } -motoko_proc_macro = { rev = "5897a8b57774b55782d8e408a254e045a9afba20", git = "https://github.com/dfinity/motoko.rs" } diff --git a/collections/rust/src/movm/src/lib.rs b/collections/rust/src/movm/src/lib.rs deleted file mode 100644 index 119c7f68..00000000 --- a/collections/rust/src/movm/src/lib.rs +++ /dev/null @@ -1,170 +0,0 @@ -//use candid::Nat; -//use fxhash::FxHashMap; -use motoko::{ - ast::Literal, - shared::Share, - value::{Value, Value_}, - vm_types::{Core, Limits}, -}; -use motoko_proc_macro::parse_static; -use std::cell::RefCell; - -thread_local! { - static CORE: RefCell = RefCell::new( - Core::new( - parse_static!(" - var map = prim \"hashMapNew\" (); - var rand_ = prim \"fastRandIterNew\" (null, 42); - let rands = func(count) { - prim \"fastRandIterNew\" (?count, 42) - };") - .clone() - ) - ); -} - -fn val_from_u32(x: u32) -> Value_ { - Value::from_literal(&Literal::Nat(format!("{}", x))) - .unwrap() - .share() -} - -fn val_from_string(s: String) -> Value_ { - Value::from_literal(&Literal::Text(s)).unwrap().share() -} - -#[ic_cdk::update] -fn generate(size: u32) { - CORE.with(|core| { - let mut core = core.borrow_mut(); - core.continue_(&Limits::none()).unwrap(); - core.eval_open_block( - vec![("size", val_from_u32(size))], - parse_static!( - " - for (x in prim \"fastRandIterNew\" (?size, 1)) { - let (m, _) = prim \"hashMapPut\" (map, x, prim \"natToText\" x); - map := m; - }" - ) - .clone(), - ) - .unwrap(); - }) -} - -#[ic_cdk::query] -fn get_mem() -> (u128, u128, u128) { - let size = core::arch::wasm32::memory_size(0) as u128 * 32768; - (size, size, size) -} - -#[ic_cdk::update] -fn get(x: u32) -> Option { - let _r = CORE - .with(|core| { - let mut core = core.borrow_mut(); - core.continue_(&Limits::none()).unwrap(); - core.eval_open_block( - vec![("x", val_from_u32(x))], - parse_static!("prim \"hashMapGet\" (map, x)").clone(), - ) - }) - .unwrap(); - None -} - -#[ic_cdk::update] -fn put(k: u32, v: String) { - CORE.with(|core| { - let mut core = core.borrow_mut(); - core.continue_(&Limits::none()).unwrap(); - core.eval_open_block( - vec![("k", val_from_u32(k)), ("v", val_from_string(v))], - parse_static!( - " - let (m, _) = prim \"hashMapPut\" (map, k, v); - map := m" - ) - .clone(), - ) - }) - .unwrap(); -} - -#[ic_cdk::update] -fn remove(x: u32) { - CORE.with(|core| { - (core.borrow_mut()).continue_(&Limits::none()).unwrap(); - (core.borrow_mut()).eval_open_block( - vec![("x", val_from_u32(x))], - parse_static!( - " - let (m, _) = prim \"hashMapRemove\" (map, x); - map := m" - ) - .clone(), - ) - }) - .unwrap(); -} - -#[ic_cdk::update] -fn batch_get(n: u32) { - CORE.with(|core| { - (core.borrow_mut()).continue_(&Limits::none()).unwrap(); - (core.borrow_mut()).eval_open_block( - vec![("size", val_from_u32(n))], - parse_static!( - " - let j = rands(size); - for (x in j) { - let _ = prim \"hashMapGet\" (map, x); - }" - ) - .clone(), - ) - }) - .unwrap(); -} - -#[ic_cdk::update] -fn batch_put(n: u32) { - CORE.with(|core| { - (core.borrow_mut()).continue_(&Limits::none()).unwrap(); - (core.borrow_mut()).eval_open_block( - vec![("size", val_from_u32(n))], - parse_static!( - " - let j = rands(size); - for (x in j) { - let s = prim \"natToText\" x; - let (m, _) = prim \"hashMapPut\" (map, x, s); - map := m; - }" - ) - .clone(), - ) - }) - .unwrap(); -} - -#[ic_cdk::update] -fn batch_remove(n: u32) { - CORE.with(|core| { - (core.borrow_mut()).continue_(&Limits::none()).unwrap(); - (core.borrow_mut()).eval_open_block( - vec![("size", val_from_u32(n))], - parse_static!( - " - let j = rands(size); - for (x in j) { - let (m, _) = prim \"hashMapRemove\" (map, x); - map := m; - }" - ) - .clone(), - ) - }) - .unwrap(); -} diff --git a/collections/rust/src/movm_dynamic/Cargo.toml b/collections/rust/src/movm_dynamic/Cargo.toml deleted file mode 100644 index 8c71a35e..00000000 --- a/collections/rust/src/movm_dynamic/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "movm_dynamic" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[lib] -crate-type = ["cdylib"] - -[dependencies] -candid.workspace = true -ic-cdk.workspace = true -fxhash = "0.2.1" -motoko = { rev = "5897a8b57774b55782d8e408a254e045a9afba20", git = "https://github.com/dfinity/motoko.rs"} -motoko_proc_macro = { rev = "5897a8b57774b55782d8e408a254e045a9afba20", git = "https://github.com/dfinity/motoko.rs" } diff --git a/collections/rust/src/movm_dynamic/src/lib.rs b/collections/rust/src/movm_dynamic/src/lib.rs deleted file mode 100644 index 02f4b7f3..00000000 --- a/collections/rust/src/movm_dynamic/src/lib.rs +++ /dev/null @@ -1,215 +0,0 @@ -use motoko::{ - dynamic::Dynamic, - shared::{FastClone, Share}, - value::{ToMotoko, Value, Value_}, - vm_types::{Core, Interruption, Limits}, -}; -use motoko_proc_macro::parse_static; -use std::cell::RefCell; - -#[derive(Clone, Debug, Hash, Default)] -struct Random { - state: u32, - size: Option, - ind: u32, -} -impl Random { - pub fn new(size: Option, seed: u32) -> Self { - Random { - state: seed, - size, - ind: 0, - } - } -} -impl Iterator for Random { - type Item = u32; - fn next(&mut self) -> Option { - if let Some(size) = self.size { - self.ind += 1; - if self.ind > size { - return None; - } - } - self.state = self.state * 48271 % 0x7fffffff; - Some(self.state) - } -} -impl Dynamic for Random { - fn iter_next(&mut self) -> motoko::dynamic::Result { - Ok(self.next().to_motoko().unwrap().share()) - } -} - -#[derive(Clone, Debug, Default)] -struct Map(std::collections::HashMap); - -impl std::hash::Hash for Map { - fn hash(&self, _state: &mut H) { - unimplemented!() - } -} - -impl Dynamic for Map { - fn get_index(&self, _core: &Core, index: Value_) -> motoko::dynamic::Result { - self.0 - .get(&index) - .map(FastClone::fast_clone) - .ok_or_else(|| Interruption::IndexOutOfBounds) - } - - fn set_index( - &mut self, - _core: &mut Core, - key: Value_, - value: Value_, - ) -> motoko::dynamic::Result<()> { - self.0.insert(key, value); - Ok(()) - } - - fn call( - &mut self, - _core: &mut Core, - _inst: &Option, - args: Value_, - ) -> motoko::dynamic::Result { - self.0.remove(&args); - Ok(Value::Unit.share()) - } -} - -thread_local! { - static CORE: RefCell = RefCell::new({ - let mut core = Core::empty(); - core.assign_alloc("map", Map::default().into_value()); - core - }); -} - -#[ic_cdk::update] -fn generate(size: u32) { - CORE.with(|core| { - let mut core = core.borrow_mut(); - core.continue_(&Limits::none()).unwrap(); - core.eval_open_block( - vec![("rand", Random::new(Some(size), 1).into_value())], - parse_static!( - " - for (x in rand) { - map[x] := prim \"natToText\" x; - } - " - ) - .clone(), - ) - .unwrap(); - }) -} - -#[ic_cdk::query] -fn get_mem() -> (u128, u128, u128) { - let size = core::arch::wasm32::memory_size(0) as u128 * 32768; - (size, size, size) -} - -#[ic_cdk::update] -fn get(x: u32) -> Option { - let _r = CORE - .with(|core| { - let mut core = core.borrow_mut(); - core.continue_(&Limits::none()).unwrap(); - core.eval_open_block( - vec![("x", x.to_motoko().unwrap())], - parse_static!("map[x]").clone(), - ) - }) - .unwrap(); - None -} - -#[ic_cdk::update] -fn put(k: u32, v: String) { - CORE.with(|core| { - let mut core = core.borrow_mut(); - core.continue_(&Limits::none()).unwrap(); - core.eval_open_block( - vec![("k", k.to_motoko().unwrap()), ("v", v.to_motoko().unwrap())], - parse_static!("map[k] := v").clone(), - ) - }) - .unwrap(); -} - -#[ic_cdk::update] -fn remove(x: u32) { - CORE.with(|core| { - let mut core = core.borrow_mut(); - core.continue_(&Limits::none()).unwrap(); - core.eval_open_block( - vec![("x", x.to_motoko().unwrap())], - parse_static!("map(x)").clone(), - ) - }) - .unwrap(); -} - -#[ic_cdk::update] -fn batch_get(n: u32) { - CORE.with(|core| { - let mut core = core.borrow_mut(); - core.continue_(&Limits::none()).unwrap(); - core.eval_open_block( - vec![("rand", Random::new(Some(n), 1).into_value())], - parse_static!( - " - for (x in rand) { - ignore map[x]; - } - " - ) - .clone(), - ) - }) - .unwrap(); -} - -#[ic_cdk::update] -fn batch_put(n: u32) { - CORE.with(|core| { - let mut core = core.borrow_mut(); - core.continue_(&Limits::none()).unwrap(); - core.eval_open_block( - vec![("rand", Random::new(Some(n), 1).into_value())], - parse_static!( - " - for (x in rand) { - map[x] := prim \"natToText\" x; - } - " - ) - .clone(), - ) - }) - .unwrap(); -} - -#[ic_cdk::update] -fn batch_remove(n: u32) { - CORE.with(|core| { - let mut core = core.borrow_mut(); - core.continue_(&Limits::none()).unwrap(); - core.eval_open_block( - vec![("rand", Random::new(Some(n), 1).into_value())], - parse_static!( - " - for (x in rand) { - map(x); - } - " - ) - .clone(), - ) - }) - .unwrap(); -}