Closed chenyan-dfinity closed 7 months 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.
The _stable
and _stable_rs
suffix represents that the library directly writes the state to stable memory using Region
in Motoko and ic-stable-stuctures
in Rust.
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 very large collections.
- The
upgrade
column uses Candid for serializing stable data. In Rust, you may get better cycle cost by using a different serialization format. Another slowdown in Rust is thatic-stable-structures
tends to be slower than the region memory in Motoko.- Different library has different ways for persisting data during upgrades, there are mainly three categories:
- Use stable variable directly in Motoko:
zhenya_hashmap
,btree
,vector
- Expose and serialize external state (
share/unshare
in Motoko,candid::Encode
in Rust):rbtree
,heap
,btreemap_rs
,hashmap_rs
,heap_rs
,vector_rs
- Use pre/post-upgrade hooks to convert data into an array:
hashmap
,splay
,triemap
,buffer
,imrc_hashmap_rs
- The stable benchmarks are much more expensive than their non-stable counterpart, because the stable memory API is much more expensive. The benefit is that they get fast upgrade. The upgrade still needs to parse the metadata when initializing the upgraded Wasm module.
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 | upgrade | |
---|---|---|---|---|---|---|---|
hashmap | 189_929 | 8_184_618_025 | 56_000_256 | 342_784 | 6_462_528_122 | 368_420 | 10_728_193_099 |
triemap | 195_472 | 13_661_315_924 | 68_228_576 | 252_649 | 657_794 | 648_084 | 15_499_470_884 |
rbtree | 185_907 | 7_009_043_570 | 52_000_464 | 116_348 | 318_320 | 330_226 | 6_870_900_152 |
splay | 190_457 | 13_157_617_583 | 48_000_400 | 631_329 | 662_998 | 928_144 | 4_308_925_798 |
btree | 230_321 | 10_223_929_607 | 25_108_416 | 357_912 | 485_794 | 539_490 | 2_861_974_825 |
zhenya_hashmap | 188_894 | 2_360_638_679 | 16_777_504 | 58_204 | 66_552 | 79_675 | 3_018_208_083 |
btreemap_rs | 553_671 | 1_792_613_493 | 27_590_656 | 75_414 | 125_227 | 86_761 | 3_204_184_669 |
imrc_hashmap_rs | 555_693 | 2_584_502_429 | 244_973_568 | 38_020 | 179_184 | 116_050 | 6_262_892_389 |
hashmap_rs | 541_994 | 443_248_419 | 73_138_176 | 21_565 | 26_650 | 24_961 | 1_565_649_003 |
binary_size | heapify 1m | max mem | pop_min 50 | put 50 | pop_min 50 | upgrade | |
---|---|---|---|---|---|---|---|
heap | 166_903 | 5_554_617_018 | 24_000_360 | 621_690 | 227_224 | 592_588 | 3_189_831_485 |
heap_rs | 533_034 | 139_670_245 | 18_284_544 | 57_437 | 22_991 | 57_485 | 648_953_257 |
binary_size | generate 5k | max mem | batch_get 500 | batch_put 500 | batch_remove 500 | upgrade | |
---|---|---|---|---|---|---|---|
buffer | 173_903 | 2_601_059 | 65_644 | 95_506 | 803_474 | 173_506 | 3_091_310 |
vector | 171_932 | 1_952_689 | 24_580 | 126_130 | 186_485 | 176_123 | 4_675_192 |
vec_rs | 533_368 | 289_403 | 1_376_256 | 17_283 | 30_525 | 23_285 | 3_826_760 |
binary_size | generate 50k | max mem | batch_get 50 | batch_put 50 | batch_remove 50 | upgrade | |
---|---|---|---|---|---|---|---|
btreemap_rs | 553_671 | 76_164_138 | 2_555_904 | 64_972 | 97_154 | 85_766 | 139_625_748 |
btreemap_stable_rs | 555_424 | 4_564_073_317 | 2_031_616 | 2_706_123 | 5_033_940 | 8_578_143 | 729_341 |
heap_rs | 533_034 | 7_052_097 | 2_293_760 | 49_946 | 23_239 | 49_834 | 33_661_680 |
heap_stable_rs | 517_798 | 272_319_598 | 458_752 | 2_306_666 | 239_309 | 2_289_588 | 729_349 |
vec_rs | 533_368 | 3_079_779 | 2_293_760 | 17_283 | 18_375 | 17_673 | 31_322_355 |
vec_stable_rs | 515_280 | 63_345_046 | 458_752 | 64_434 | 79_730 | 83_628 | 729_352 |
Environment
- dfx 0.18.0
- Motoko compiler 0.11.0 (source lndfxrzc-zr7pf1k6-nr3nr3d7-jfla8nbn)
- rustc 1.76.0 (07dca489a 2024-02-04)
- ic-repl 0.7.0
- ic-wasm 0.7.0
Cryptographic libraries
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.upgrade
. Upgrade the canister with the same Wasm. In Motoko, we use stable variable. In Rust, we convert the tree to a vector before serialization.binary_size | SHA-256 | SHA-512 | account_id | neuron_id | |
---|---|---|---|---|---|
Motoko | 193_686 | 267_743_355 | 247_834_501 | 33_636 | 24_532 |
Rust | 538_677 | 82_788_763 | 56_793_160 | 47_956 | 50_870 |
binary_size | generate 10k | max mem | inc | witness | upgrade | |
---|---|---|---|---|---|---|
Motoko | 243_641 | 4_666_119_661 | 3_430_044 | 553_629 | 407_936 | 274_434_719 |
Rust | 579_504 | 6_409_378_412 | 2_228_224 | 1_020_591 | 304_778 | 6_025_902_205 |
Environment
- dfx 0.18.0
- Motoko compiler 0.11.0 (source lndfxrzc-zr7pf1k6-nr3nr3d7-jfla8nbn)
- rustc 1.76.0 (07dca489a 2024-02-04)
- ic-repl 0.7.0
- ic-wasm 0.7.0
Sample Dapps
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 | upgrade | |
---|---|---|---|---|---|---|
Motoko | 273_938 | 510_793 | 22_320 | 18_541 | 19_600 | 157_928 |
Rust | 847_032 | 627_444 | 100_778 | 131_566 | 139_344 | 1_829_093 |
binary_size | init | mint_token | transfer_token | upgrade | |
---|---|---|---|---|---|
Motoko | 220_403 | 481_158 | 30_204 | 8_764 | 89_833 |
Rust | 877_894 | 243_128 | 386_990 | 90_821 | 2_088_864 |
Environment
- dfx 0.18.0
- Motoko compiler 0.11.0 (source lndfxrzc-zr7pf1k6-nr3nr3d7-jfla8nbn)
- rustc 1.76.0 (07dca489a 2024-02-04)
- ic-repl 0.7.0
- ic-wasm 0.7.0
Heartbeat / Timer
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 | 137_183 | 23_432 |
Rust | 23_657 | 480 |
binary_size | setTimer | cancelTimer | |
---|---|---|---|
Motoko | 145_619 | 51_778 | 4_626 |
Rust | 497_687 | 68_338 | 11_698 |
Environment
- dfx 0.18.0
- Motoko compiler 0.11.0 (source lndfxrzc-zr7pf1k6-nr3nr3d7-jfla8nbn)
- rustc 1.76.0 (07dca489a 2024-02-04)
- ic-repl 0.7.0
- ic-wasm 0.7.0
Motoko Specific Benchmarks
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 700k | max mem | batch_get 50 | batch_put 50 | batch_remove 50 | |
---|---|---|---|---|---|
default | 1_068_192_695 | 47_793_792 | 119 | 119 | 119 |
copying | 1_068_192_577 | 47_793_792 | 1_067_924_316 | 1_068_004_203 | 1_067_925_853 |
compacting | 1_545_586_176 | 47_793_792 | 1_192_139_528 | 1_415_425_189 | 1_439_317_325 |
generational | 2_304_140_531 | 47_802_256 | 882_208_645 | 1_211_144 | 1_103_549 |
incremental | 29_503_170 | 976_097_188 | 471_911_803 | 497_465_467 | 1_221_308_722 |
binary size | put new bucket | put existing bucket | get | |
---|---|---|---|---|
Map | 299_217 | 813_521 | 16_115 | 16_660 |
Environment
- dfx 0.18.0
- Motoko compiler 0.11.0 (source lndfxrzc-zr7pf1k6-nr3nr3d7-jfla8nbn)
- rustc 1.76.0 (07dca489a 2024-02-04)
- ic-repl 0.7.0
- ic-wasm 0.7.0
Publisher & Subscriber
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 | 161_290 | 145_741 | 28_593 | 11_963 | 22_854 | 6_446 |
Rust | 534_397 | 572_921 | 69_856 | 43_803 | 94_183 | 53_351 |
Environment
- dfx 0.18.0
- Motoko compiler 0.11.0 (source lndfxrzc-zr7pf1k6-nr3nr3d7-jfla8nbn)
- rustc 1.76.0 (07dca489a 2024-02-04)
- ic-repl 0.7.0
- ic-wasm 0.7.0
Map
Priority queue
Growable array
Stable structures
Statistics
SHA-2
Certified map
Statistics
Basic DAO
DIP721 NFT
Statistics
Heartbeat
Timer
Statistics
Garbage Collection
Actor class
Statistics
Publisher & Subscriber
Statistics
binary_size: 0.64% [-1.11%, 2.40%]
max_mem: no change
cycles: 1.59% [0.54%, 2.63%]
Overall Statistics
binary_size: 0.83% [0.43%, 1.22%]
max_mem: -11.50% [-15.09%, -7.92%]
cycles: 0.30% [-0.41%, 1.01%]