Closed chenyan-dfinity closed 1 year ago
Note The flamegraph link only works after you merge. Unchanged benchmarks are omitted.
Measure different collection libraries written in both Motoko and Rust.
The library names with _rs
suffix are written in Rust; the rest are written in Motoko.
We use the same random number generator with fixed seed to ensure that all collections contain the same elements, and the queries are exactly the same. Below we explain the measurements of each column in the table:
rts_max_heap_size
after generate
call; For Rust, it reports the Wasm's memory page * 32Kb.Note
- The Candid interface of the benchmark is minimal, therefore the serialization cost is negligible in this measurement.
- Due to the instrumentation overhead and cycle limit, we cannot profile computations with large collections. Hopefully, when deterministic time slicing is ready, we can measure the performance on larger memory footprint.
hashmap
uses amortized data structure. When the initial capacity is reached, it has to copy the whole array, thus the cost ofbatch_put 50
is much higher than other data structures.btree
comes from mops.one/stableheapbtreemap.zhenya_hashmap
comes from mops.one/map.vector
comes from mops.one/vector. Compare withbuffer
,put
has better worst case time and space complexity ($O(\sqrt{n})$ vs $O(n)$);get
has a slightly larger constant overhead.hashmap_rs
uses thefxhash
crate, which is the same asstd::collections::HashMap
, but with a deterministic hasher. This ensures reproducible result.imrc_hashmap_rs
uses theim-rc
crate, which is the immutable version hashmap in Rust.
binary_size | generate 1m | max mem | batch_get 50 | batch_put 50 | batch_remove 50 | |
---|---|---|---|---|---|---|
hashmap | 159_194 | 7_588_838_102 | 61_987_732 | 318_951 | 6_008_157_311 | 341_403 |
triemap | 162_066 | 12_638_993_478 | 74_216_052 | 250_401 | 611_116 | 600_960 |
rbtree | 162_408 | 6_161_146_247 | 57_995_940 | 116_202 | 277_258 | 302_170 |
splay | 157_707 | 12_285_354_629 | 53_995_876 | 589_148 | 619_660 | 870_643 |
btree | 214_489 | 9_139_742_724 | 31_103_892 | 314_208 | 429_672 | 480_823 |
zhenya_hashmap | 168_445 | 2_822_042_495 | 65_987_480 | 73_056 | 89_710 | 108_471 |
btreemap_rs | 446_200 | 1_637_381_286 | 13_762_560 | 65_726 | 112_100 | 81_944 |
imrc_hashmap_rs | 446_100 | 2_388_802_442 | 122_454_016 | 33_200 | 163_343 | 97_441 |
hashmap_rs | 439_281 | 414_097_976 | 36_536_320 | 17_125 | 22_229 | 20_323 |
binary_size | heapify 1m | max mem | pop_min 50 | put 50 | ||
---|---|---|---|---|---|---|
heap | 152_465 | 5_200_975_509 | 29_995_836 | 574_107 | 208_978 | 546_783 |
heap_rs | 437_212 | 125_346_655 | 9_109_504 | 53_855 | 18_525 | 54_078 |
binary_size | generate 5k | max mem | batch_get 500 | batch_put 500 | batch_remove 500 | |
---|---|---|---|---|---|---|
buffer | 161_556 | 2_296_568 | 65_508 | 84_247 | 740_528 | 142_247 |
vector | 160_836 | 1_890_122 | 24_764 | 137_383 | 179_360 | 181_415 |
vec_rs | 435_769 | 266_276 | 655_360 | 13_221 | 25_648 | 20_782 |
Measure different cryptographic libraries written in both Motoko and Rust.
SHA-256/SHA-512
. Compute the hash of a 1M Wasm binary.account_id
. Compute the ledger account id from principal, based on SHA-224.neuron_id
. Compute the NNS neuron id from principal, based on SHA-256.generate 10k
. Insert 10k 7-character word as both key and value into the certified map.max mem
. For Motoko, it reports rts_max_heap_size
after generate
call; For Rust, it reports the Wasm's memory page * 32Kb.inc
. Increment a counter and insert the counter value into the map.witness
. Generate the root hash and a witness for the counter.binary_size | SHA-256 | SHA-512 | account_id | neuron_id | |
---|---|---|---|---|---|
Motoko | 196_200 | 285_638_950 | 255_658_597 | 34_744 | 23_689 |
Rust | 528_163 | 82_512_868 | 56_526_741 | 43_318 | 45_477 |
binary_size | generate 10k | max mem | inc | witness | |
---|---|---|---|---|---|
Motoko | 205_806 | 5_050_097_115 | 3_429_924 | 598_393 | 361_240 |
Rust | 469_882 | 6_232_657_596 | 1_081_344 | 988_880 | 290_927 |
Measure the performance of some typical dapps:
heartbeat
disabled to make profiling easier. We have a separate benchmark to measure heartbeat performance.Note
- The cost difference is mainly due to the Candid serialization cost.
- Motoko statically compiles/specializes the serialization code for each method, whereas in Rust, we use
serde
to dynamically deserialize data based on data on the wire.- We could improve the performance on the Rust side by using parser combinators. But it is a challenge to maintain the ergonomics provided by
serde
.- For real-world applications, we tend to send small data for each endpoint, which makes the Candid overhead in Rust tolerable.
binary_size | init | transfer_token | submit_proposal | vote_proposal | |
---|---|---|---|---|---|
Motoko | 277_584 | 39_684 | 17_787 | 13_881 | 15_641 |
Rust | 762_892 | 484_210 | 89_052 | 107_757 | 118_705 |
binary_size | init | mint_token | transfer_token | |
---|---|---|---|---|
Motoko | 229_941 | 13_217 | 23_778 | 5_267 |
Rust | 828_098 | 128_336 | 332_435 | 79_221 |
Measure the cost of empty heartbeat and timer job.
setTimer
measures both the setTimer(0)
method and the execution of empty job.setTimer
and canister_global_timer
function. If it's not there, we may need to adjust the script.binary_size | heartbeat | |
---|---|---|
Motoko | 142_110 | 7_777 |
Rust | 25_646 | 789 |
binary_size | setTimer | cancelTimer | |
---|---|---|---|
Motoko | 149_306 | 16_471 | 1_847 |
Rust | 470_589 | 44_509 | 7_713 |
Measure various features only available in Motoko.
Garbage Collection. Measure Motoko garbage collection cost using the Triemap benchmark. The max mem column reports rts_max_heap_size
after generate
call. The cycle cost numbers reported here are garbage collection cost only. Some flamegraphs are truncated due to the 2M log size limit. The dfx
/ic-wasm
optimizer is disabled for the garbage collection test cases due to how the optimizer affects function names, making profiling trickier.
generate
will trigger the copying GC. The rest of the methods will not trigger GC.--force-gc --copying-gc
.--force-gc --compacting-gc
.--force-gc --generational-gc
.--force-gc --incremental-gc
.Actor class. Measure the cost of spawning actor class, using the Actor classes example.
generate 800k | max mem | batch_get 50 | batch_put 50 | batch_remove 50 | |
---|---|---|---|---|---|
default | 1_012_258_537 | 59_396_776 | 50 | 50 | 50 |
copying | 1_012_258_487 | 59_396_776 | 1_012_236_046 | 1_012_303_056 | 1_012_240_283 |
compacting | 1_675_009_925 | 59_396_776 | 1_292_955_500 | 1_532_273_641 | 1_558_502_986 |
generational | 2_498_146_508 | 59_405_240 | 977_578_983 | 1_044_991 | 960_405 |
incremental | 32_320_754 | 1_136_155_048 | 290_257_785 | 292_951_006 | 292_977_552 |
binary size | put new bucket | put existing bucket | get | |
---|---|---|---|---|
Map | 297_686 | 719_907 | 4_854 | 5_288 |
Measure the cost of inter-canister calls from the Publisher & Subscriber example.
pub_binary_size | sub_binary_size | subscribe_caller | subscribe_callee | publish_caller | publish_callee | |
---|---|---|---|---|---|---|
Motoko | 166_785 | 151_855 | 15_317 | 8_758 | 11_042 | 3_872 |
Rust | 511_787 | 565_325 | 52_965 | 35_499 | 76_563 | 45_397 |
Map
Priority queue
Growable array
Statistics
SHA-2
Certified map
Statistics
Basic DAO
DIP721 NFT
Statistics
Heartbeat
Timer
Statistics
Garbage Collection
Actor class
Statistics
Publisher & Subscriber
Statistics
binary_size: 11.14% [5.14%, 17.15%]
max_mem: no change
cycles: 3.51% [2.64%, 4.37%]
Overall Statistics
binary_size: 11.97% [10.28%, 13.67%]
max_mem: no change
cycles: 6.06% [5.17%, 6.95%]