dfinity / canister-profiling

Collection of canister performance benchmarks
Apache License 2.0
21 stars 8 forks source link

bump ic-stable-structures #100

Closed chenyan-dfinity closed 10 months ago

github-actions[bot] commented 10 months ago

Note Diffing the performance result against the published result from main branch. Unchanged benchmarks are omitted.

Map

binary_size generate 1m max mem batch_get 50 batch_put 50 batch_remove 50 upgrade
hashmap 160_221 6_984_044_999 61_987_852 288_670 5_536_856_410 310_195 9_128_784_003
triemap 163_474 11_463_655_150 74_216_172 222_926 549_435 540_205 13_075_158_546
rbtree 158_149 5_979_229_900 57_996_060 88_905 268_573 278_352 5_771_880_608
splay 159_956 11_568_250_103 53_995_996 552_014 581_765 810_321 3_722_474_749
btree 187_897 8_224_242_789 31_104_012 277_542 384_171 429_041 2_517_941_583
zhenya_hashmap 160_509 2_201_622_562 22_773_100 48_627 61_839 70_872 2_695_448_620
btreemap_rs 478_816 ($\textcolor{red}{0.25\%}$) 1_651_590_463 27_590_656 66_862 112_477 76_234 2_660_975_735 ($\textcolor{green}{-0.00\%}$)
imrc_hashmap_rs 482_941 ($\textcolor{red}{0.66\%}$) 2_392_906_831 244_973_568 32_763 163_245 98_394 5_191_575_314 ($\textcolor{green}{-0.00\%}$)
hashmap_rs 469_257 ($\textcolor{red}{0.27\%}$) 403_296_648 73_138_176 16_851 21_680 20_263 1_144_828_112 ($\textcolor{red}{0.00\%}$)

Priority queue

binary_size heapify 1m max mem pop_min 50 put 50 pop_min 50.1 upgrade
heap 147_638 4_684_519_403 29_995_956 511_505 186_471 487_225 2_655_609_909
heap_rs 462_981 ($\textcolor{green}{-0.19\%}$) 121_602_221 18_284_544 51_661 18_245 51_802 440_739_960 ($\textcolor{green}{-0.00\%}$)

Growable array

binary_size generate 5k max mem batch_get 500 batch_put 500 batch_remove 500 upgrade
buffer 151_004 2_082_623 65_644 73_092 671_517 127_592 2_474_639
vector 152_551 1_588_260 24_580 105_191 149_932 148_094 3_844_445
vec_rs 460_777 ($\textcolor{red}{0.24\%}$) 265_683 1_310_720 13_014 25_363 21_247 2_743_803 ($\textcolor{green}{-0.00\%}$)

Stable structures

binary_size generate 50k max mem batch_get 50 batch_put 50 batch_remove 50 upgrade
btreemap_rs 478_816 ($\textcolor{red}{0.25\%}$) 70_026_986 2_555_904 57_181 86_494 75_309 113_837_919 ($\textcolor{green}{-0.00\%}$)
btreemap_stable_rs 488_874 ($\textcolor{red}{2.13\%}$) 4_608_383_725 ($\textcolor{red}{9.09\%}$) 2_031_616 ($\textcolor{green}{-22.50\%}$) 2_778_442 ($\textcolor{red}{9.87\%}$) 5_070_568 ($\textcolor{red}{10.10\%}$) 8_637_788 ($\textcolor{red}{10.49\%}$) 653_330 ($\textcolor{green}{-0.00\%}$)
heap_rs 462_981 ($\textcolor{green}{-0.19\%}$) 6_139_838 2_293_760 44_362 18_477 44_345 23_149_344 ($\textcolor{green}{-0.00\%}$)
heap_stable_rs 450_949 ($\textcolor{green}{-0.02\%}$) 278_284_330 ($\textcolor{green}{-0.41\%}$) 458_752 2_329_886 ($\textcolor{green}{-0.72\%}$) 240_249 ($\textcolor{green}{-0.38\%}$) 2_312_267 ($\textcolor{green}{-0.73\%}$) 653_433
vec_rs 460_777 ($\textcolor{red}{0.24\%}$) 2_866_886 2_228_224 13_014 14_113 13_710 21_249_880 ($\textcolor{green}{-0.00\%}$)
vec_stable_rs 448_899 ($\textcolor{red}{0.64\%}$) 65_186_207 ($\textcolor{green}{-0.00\%}$) 458_752 59_189 ($\textcolor{red}{0.33\%}$) 77_384 ($\textcolor{green}{-0.00\%}$) 79_580 ($\textcolor{red}{0.25\%}$) 653_447

Statistics

SHA-2

binary_size SHA-256 SHA-512 account_id neuron_id
Motoko 173_220 247_480_401 228_033_044 30_017 20_760
Rust 478_975 ($\textcolor{red}{0.22\%}$) 82_511_945 ($\textcolor{green}{-0.00\%}$) 56_525_949 ($\textcolor{green}{-0.00\%}$) 42_419 ($\textcolor{green}{-0.03\%}$) 44_417 ($\textcolor{green}{-0.03\%}$)

Certified map

binary_size generate 10k max mem inc witness upgrade
Motoko 206_484 4_390_019_361 3_430_044 519_711 327_767 225_153_243
Rust 503_666 ($\textcolor{red}{0.62\%}$) 6_199_155_471 2_228_224 983_538 288_311 ($\textcolor{green}{-0.01\%}$) 5_816_677_999 ($\textcolor{green}{-0.00\%}$)

Statistics

Basic DAO

binary_size init transfer_token submit_proposal vote_proposal upgrade
Motoko 236_862 497_618 16_244 ($\textcolor{green}{-0.56\%}$) 12_672 ($\textcolor{red}{0.02\%}$) 14_136 ($\textcolor{red}{0.01\%}$) 128_910 ($\textcolor{green}{-0.06\%}$)
Rust 782_736 ($\textcolor{red}{0.41\%}$) 547_986 ($\textcolor{green}{-0.02\%}$) 86_609 ($\textcolor{red}{0.01\%}$) 105_996 ($\textcolor{red}{0.03\%}$) 117_902 ($\textcolor{green}{-0.00\%}$) 1_624_429 ($\textcolor{green}{-0.01\%}$)

DIP721 NFT

binary_size init mint_token transfer_token upgrade
Motoko 195_127 472_267 22_357 4_729 71_602
Rust 804_410 ($\textcolor{red}{0.59\%}$) 217_220 ($\textcolor{green}{-0.02\%}$) 325_588 ($\textcolor{green}{-0.04\%}$) 78_144 ($\textcolor{green}{-0.00\%}$) 1_797_069 ($\textcolor{green}{-0.05\%}$)

Statistics

Heartbeat

binary_size heartbeat
Motoko 123_696 3_758 ($\textcolor{green}{-49.21\%}$)
Rust 23_839 469

Timer

binary_size setTimer cancelTimer
Motoko 129_966 15_227 1_684
Rust 423_634 ($\textcolor{red}{0.27\%}$) 43_472 ($\textcolor{red}{0.00\%}$) 7_569

Statistics

Publisher & Subscriber

pub_binary_size sub_binary_size subscribe_caller subscribe_callee publish_caller publish_callee
Motoko 144_769 131_630 14_651 8_456 10_539 3_669
Rust 458_430 ($\textcolor{red}{0.27\%}$) 511_900 ($\textcolor{red}{0.22\%}$) 51_628 ($\textcolor{red}{0.01\%}$) 34_416 ($\textcolor{red}{0.01\%}$) 74_394 ($\textcolor{green}{-0.00\%}$) 44_008 ($\textcolor{green}{-0.01\%}$)

Statistics

github-actions[bot] commented 10 months ago

Note The flamegraph link only works after you merge. Unchanged benchmarks are omitted.

Collection libraries

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:

💎 Takeaways

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 that ic-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 of batch_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 with buffer, 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 the fxhash crate, which is the same as std::collections::HashMap, but with a deterministic hasher. This ensures reproducible result.
  • imrc_hashmap_rs uses the im-rc crate, which is the immutable version hashmap in Rust.

Map

binary_size generate 1m max mem batch_get 50 batch_put 50 batch_remove 50 upgrade
hashmap 160_221 6_984_044_999 61_987_852 288_670 5_536_856_410 310_195 9_128_784_003
triemap 163_474 11_463_655_150 74_216_172 222_926 549_435 540_205 13_075_158_546
rbtree 158_149 5_979_229_900 57_996_060 88_905 268_573 278_352 5_771_880_608
splay 159_956 11_568_250_103 53_995_996 552_014 581_765 810_321 3_722_474_749
btree 187_897 8_224_242_789 31_104_012 277_542 384_171 429_041 2_517_941_583
zhenya_hashmap 160_509 2_201_622_562 22_773_100 48_627 61_839 70_872 2_695_448_620
btreemap_rs 478_816 1_651_590_463 27_590_656 66_862 112_477 76_234 2_660_975_735
imrc_hashmap_rs 482_941 2_392_906_831 244_973_568 32_763 163_245 98_394 5_191_575_314
hashmap_rs 469_257 403_296_648 73_138_176 16_851 21_680 20_263 1_144_828_112

Priority queue

binary_size heapify 1m max mem pop_min 50 put 50 pop_min 50 upgrade
heap 147_638 4_684_519_403 29_995_956 511_505 186_471 487_225 2_655_609_909
heap_rs 462_981 121_602_221 18_284_544 51_661 18_245 51_802 440_739_960

Growable array

binary_size generate 5k max mem batch_get 500 batch_put 500 batch_remove 500 upgrade
buffer 151_004 2_082_623 65_644 73_092 671_517 127_592 2_474_639
vector 152_551 1_588_260 24_580 105_191 149_932 148_094 3_844_445
vec_rs 460_777 265_683 1_310_720 13_014 25_363 21_247 2_743_803

Stable structures

binary_size generate 50k max mem batch_get 50 batch_put 50 batch_remove 50 upgrade
btreemap_rs 478_816 70_026_986 2_555_904 57_181 86_494 75_309 113_837_919
btreemap_stable_rs 488_874 4_608_383_725 2_031_616 2_778_442 5_070_568 8_637_788 653_330
heap_rs 462_981 6_139_838 2_293_760 44_362 18_477 44_345 23_149_344
heap_stable_rs 450_949 278_284_330 458_752 2_329_886 240_249 2_312_267 653_433
vec_rs 460_777 2_866_886 2_228_224 13_014 14_113 13_710 21_249_880
vec_stable_rs 448_899 65_186_207 458_752 59_189 77_384 79_580 653_447

Environment

  • dfx 0.15.1
  • Motoko compiler 0.10.0 (source a3ywvw0a-p5a03qy6-vscbl9j8-qxszbxa6)
  • rustc 1.73.0 (cc66ad468 2023-10-03)
  • ic-repl 0.5.1
  • ic-wasm 0.6.0

    Cryptographic libraries

Measure different cryptographic libraries written in both Motoko and Rust.

SHA-2

binary_size SHA-256 SHA-512 account_id neuron_id
Motoko 173_220 247_480_401 228_033_044 30_017 20_760
Rust 478_975 82_511_945 56_525_949 42_419 44_417

Certified map

binary_size generate 10k max mem inc witness upgrade
Motoko 206_484 4_390_019_361 3_430_044 519_711 327_767 225_153_243
Rust 503_666 6_199_155_471 2_228_224 983_538 288_311 5_816_677_999

Environment

  • dfx 0.15.1
  • Motoko compiler 0.10.0 (source a3ywvw0a-p5a03qy6-vscbl9j8-qxszbxa6)
  • rustc 1.73.0 (cc66ad468 2023-10-03)
  • ic-repl 0.5.1
  • ic-wasm 0.6.0

    Sample Dapps

Measure the performance of some typical dapps:

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.

Basic DAO

binary_size init transfer_token submit_proposal vote_proposal upgrade
Motoko 236_862 497_618 16_244 12_672 14_136 128_910
Rust 782_736 547_986 86_609 105_996 117_902 1_624_429

DIP721 NFT

binary_size init mint_token transfer_token upgrade
Motoko 195_127 472_267 22_357 4_729 71_602
Rust 804_410 217_220 325_588 78_144 1_797_069

Environment

  • dfx 0.15.1
  • Motoko compiler 0.10.0 (source a3ywvw0a-p5a03qy6-vscbl9j8-qxszbxa6)
  • rustc 1.73.0 (cc66ad468 2023-10-03)
  • ic-repl 0.5.1
  • ic-wasm 0.6.0

    Heartbeat / Timer

Measure the cost of empty heartbeat and timer job.

Heartbeat

binary_size heartbeat
Motoko 123_696 3_758
Rust 23_839 469

Timer

binary_size setTimer cancelTimer
Motoko 129_966 15_227 1_684
Rust 423_634 43_472 7_569

Environment

  • dfx 0.15.1
  • Motoko compiler 0.10.0 (source a3ywvw0a-p5a03qy6-vscbl9j8-qxszbxa6)
  • rustc 1.73.0 (cc66ad468 2023-10-03)
  • ic-repl 0.5.1
  • ic-wasm 0.6.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 144_769 131_630 14_651 8_456 10_539 3_669
Rust 458_430 511_900 51_628 34_416 74_394 44_008

Environment

  • dfx 0.15.1
  • Motoko compiler 0.10.0 (source a3ywvw0a-p5a03qy6-vscbl9j8-qxszbxa6)
  • rustc 1.73.0 (cc66ad468 2023-10-03)
  • ic-repl 0.5.1
  • ic-wasm 0.6.0