From 927a6be157836c8e63ff8bc65922629895ab266c Mon Sep 17 00:00:00 2001 From: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com> Date: Thu, 14 Nov 2024 10:49:01 -0800 Subject: [PATCH] add orderedmap benchmark --- .github/workflows/perf.yml | 2 +- collections/motoko/dfx.json | 4 +++ collections/motoko/src/orderedmap.mo | 40 ++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 collections/motoko/src/orderedmap.mo diff --git a/.github/workflows/perf.yml b/.github/workflows/perf.yml index 2d3b915a..6afc17d0 100644 --- a/.github/workflows/perf.yml +++ b/.github/workflows/perf.yml @@ -13,7 +13,7 @@ jobs: env: DFX_VERSION: 0.24.0 IC_REPL_VERSION: 0.7.6 - MOC_VERSION: 0.13.0 + MOC_VERSION: 0.13.3 IC_WASM_VERSION: 0.9.0 RUSTC_VERSION: 1.81.0 steps: diff --git a/collections/motoko/dfx.json b/collections/motoko/dfx.json index 9f83328e..698d4dda 100644 --- a/collections/motoko/dfx.json +++ b/collections/motoko/dfx.json @@ -12,6 +12,10 @@ "type": "motoko", "main": "src/rbtree.mo" }, + "orderedmap": { + "type": "motoko", + "main": "src/orderedmap.mo" + }, "splay": { "type": "motoko", "main": "src/splay.mo" diff --git a/collections/motoko/src/orderedmap.mo b/collections/motoko/src/orderedmap.mo new file mode 100644 index 00000000..7d1ced02 --- /dev/null +++ b/collections/motoko/src/orderedmap.mo @@ -0,0 +1,40 @@ +import RBTree "mo:base/OrderedMap"; +import Nat64 "mo:base/Nat64"; +import Iter "mo:base/Iter"; +import Option "mo:base/Option"; +import Random "random"; +import Profiling "../../../utils/motoko/Profiling"; + +actor { + stable let profiling = Profiling.init(); + + var Ops = RBTree.Make(Nat64.compare); + stable var map : RBTree.Map = Ops.empty(); + let rand = Random.new(null, 42); + + public func generate(size: Nat32) : async () { + let rand = Random.new(?size, 1); + let iter = Iter.map(rand, func x = (x, x)); + map := Ops.fromIter(iter); + }; + public query func get_mem() : async (Nat,Nat,Nat) { + Random.get_memory() + }; + public func batch_get(n : Nat) : async () { + for (_ in Iter.range(1, n)) { + ignore Ops.get(map, Option.get(rand.next(), 0)); + } + }; + public func batch_put(n : Nat) : async () { + for (_ in Iter.range(1, n)) { + let k = Option.get(rand.next(), 0); + map := Ops.put(map, k, k); + } + }; + public func batch_remove(n : Nat) : async () { + let rand = Random.new(null, 1); + for (_ in Iter.range(1, n)) { + map := Ops.remove(map, Option.get(rand.next(), 0)).0; + } + }; +}