Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experiment: perf run with tagging disabled #110

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Conversation

crusso
Copy link

@crusso crusso commented Feb 18, 2024

Using code from dfinity/motoko#4400 which incorporates but disables tagging, wideing scalars to their original width.

To see ramifications of checking the tagging feature in, but gated off, until future use.

Copy link

github-actions bot commented Feb 18, 2024

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 189_916 ($\textcolor{red}{0.16\%}$) 8_184_618_025 ($\textcolor{green}{-2.22\%}$) 56_000_256 ($\textcolor{green}{-9.66\%}$) 342_784 ($\textcolor{green}{-0.62\%}$) 6_462_528_122 ($\textcolor{green}{-1.98\%}$) 368_420 ($\textcolor{green}{-0.76\%}$) 10_728_193_099 ($\textcolor{green}{-2.71\%}$)
triemap 195_458 ($\textcolor{green}{-0.03\%}$) 13_661_315_924 ($\textcolor{green}{-1.40\%}$) 68_228_576 ($\textcolor{green}{-8.07\%}$) 252_649 ($\textcolor{green}{-0.76\%}$) 657_794 ($\textcolor{green}{-0.56\%}$) 648_084 ($\textcolor{green}{-0.42\%}$) 15_499_470_884 ($\textcolor{green}{-2.01\%}$)
rbtree 185_901 ($\textcolor{green}{-0.35\%}$) 7_009_043_570 ($\textcolor{green}{-1.66\%}$) 52_000_464 ($\textcolor{green}{-10.34\%}$) 116_348 ($\textcolor{red}{1.79\%}$) 318_320 ($\textcolor{green}{-0.01\%}$) 330_226 ($\textcolor{red}{0.59\%}$) 6_870_900_152 ($\textcolor{green}{-4.16\%}$)
splay 190_447 ($\textcolor{green}{-0.04\%}$) 13_157_617_583 ($\textcolor{green}{-0.68\%}$) 48_000_400 ($\textcolor{green}{-11.10\%}$) 631_329 ($\textcolor{red}{0.42\%}$) 662_998 ($\textcolor{red}{0.21\%}$) 928_144 ($\textcolor{red}{0.67\%}$) 4_308_925_798 ($\textcolor{green}{-5.67\%}$)
btree 230_305 ($\textcolor{red}{0.19\%}$) 10_223_929_607 ($\textcolor{green}{-0.41\%}$) 25_108_416 ($\textcolor{green}{-19.28\%}$) 357_912 ($\textcolor{red}{1.21\%}$) 485_794 ($\textcolor{red}{0.76\%}$) 539_490 ($\textcolor{red}{1.04\%}$) 2_861_974_825 ($\textcolor{green}{-8.68\%}$)
zhenya_hashmap 188_888 ($\textcolor{green}{-0.21\%}$) 2_360_638_679 ($\textcolor{green}{-8.17\%}$) 16_777_504 ($\textcolor{green}{-26.33\%}$) 58_204 ($\textcolor{green}{-3.31\%}$) 66_552 ($\textcolor{green}{-5.11\%}$) 79_675 ($\textcolor{green}{-3.37\%}$) 3_018_208_083 ($\textcolor{green}{-8.69\%}$)
btreemap_rs 537_393 1_793_333_047 27_590_656 75_328 125_166 86_260 2_937_041_107
imrc_hashmap_rs 542_882 2_584_501_850 244_973_568 37_762 178_926 115_385 5_796_587_958
hashmap_rs 529_458 439_248_112 73_138_176 21_501 26_711 25_024 1_298_646_667

Priority queue

binary_size heapify 1m max mem pop_min 50 put 50 pop_min 50.1 upgrade
heap 166_898 ($\textcolor{green}{-0.36\%}$) 5_554_617_018 ($\textcolor{green}{-2.51\%}$) 24_000_360 ($\textcolor{green}{-19.99\%}$) 621_690 ($\textcolor{red}{0.06\%}$) 227_224 ($\textcolor{green}{-0.63\%}$) 592_588 ($\textcolor{red}{0.07\%}$) 3_189_831_485 ($\textcolor{green}{-3.62\%}$)
heap_rs 525_853 139_669_830 18_284_544 57_419 23_051 57_545 510_960_192

Growable array

binary_size generate 5k max mem batch_get 500 batch_put 500 batch_remove 500 upgrade
buffer 173_895 ($\textcolor{green}{-0.01\%}$) 2_601_059 ($\textcolor{red}{1.17\%}$) 65_644 95_506 ($\textcolor{red}{0.02\%}$) 803_474 ($\textcolor{red}{0.38\%}$) 173_506 ($\textcolor{red}{1.77\%}$) 3_091_310 ($\textcolor{red}{0.98\%}$)
vector 171_927 ($\textcolor{green}{-0.19\%}$) 1_952_689 ($\textcolor{red}{1.65\%}$) 24_580 126_130 ($\textcolor{red}{0.01\%}$) 186_485 ($\textcolor{red}{1.69\%}$) 176_123 ($\textcolor{red}{0.08\%}$) 4_675_192 ($\textcolor{green}{-0.43\%}$)
vec_rs 520_881 289_040 1_376_256 17_251 30_571 23_331 3_161_017

Stable structures

Note
Same as main branch, skipping.

Statistics

  • binary_size: -0.09% [-0.22%, 0.03%]
  • max_mem: -14.97% [-20.03%, -9.90%]
  • cycles: -1.24% [-1.91%, -0.58%]

SHA-2

binary_size SHA-256 SHA-512 account_id neuron_id
Motoko 193_678 ($\textcolor{green}{-1.43\%}$) 267_743_355 ($\textcolor{green}{-1.94\%}$) 247_834_501 ($\textcolor{green}{-4.62\%}$) 33_636 ($\textcolor{green}{-2.14\%}$) 24_532 ($\textcolor{green}{-1.47\%}$)
Rust 537_397 82_787_911 56_792_991 47_914 50_388

Certified map

binary_size generate 10k max mem inc witness upgrade
Motoko 243_625 ($\textcolor{green}{-0.64\%}$) 4_666_119_661 ($\textcolor{green}{-2.04\%}$) 3_430_044 553_629 ($\textcolor{green}{-2.03\%}$) 407_936 ($\textcolor{red}{1.46\%}$) 274_434_719 ($\textcolor{red}{0.06\%}$)
Rust 565_792 6_409_147_805 2_228_224 1_019_959 303_897 6_019_483_730

Statistics

  • binary_size: -1.03% [-3.55%, 1.48%]
  • max_mem: no change
  • cycles: -1.59% [-2.78%, -0.40%]

Basic DAO

binary_size init transfer_token submit_proposal vote_proposal upgrade
Motoko 273_924 ($\textcolor{green}{-0.56\%}$) 510_825 ($\textcolor{red}{0.03\%}$) 22_316 ($\textcolor{red}{0.24\%}$) 18_599 ($\textcolor{green}{-0.07\%}$) 19_665 ($\textcolor{red}{0.15\%}$) 157_946 ($\textcolor{red}{0.22\%}$)
Rust 849_921 599_916 ($\textcolor{green}{-0.01\%}$) 99_156 123_702 136_655 1_799_828 ($\textcolor{green}{-0.00\%}$)

DIP721 NFT

binary_size init mint_token transfer_token upgrade
Motoko 220_392 ($\textcolor{green}{-1.00\%}$) 481_158 30_204 ($\textcolor{red}{1.32\%}$) 8_764 ($\textcolor{green}{-0.14\%}$) 89_833 ($\textcolor{red}{0.42\%}$)
Rust 869_104 236_542 368_044 91_941 1_999_207 ($\textcolor{green}{-0.00\%}$)

Statistics

  • binary_size: -0.78% [-2.16%, 0.61%]
  • max_mem: no change
  • cycles: 0.20% [-0.03%, 0.42%]

Heartbeat

binary_size heartbeat
Motoko 137_178 ($\textcolor{green}{-0.52\%}$) 19_507 ($\textcolor{green}{-0.02\%}$)
Rust 23_637 1_112

Timer

binary_size setTimer cancelTimer
Motoko 145_613 ($\textcolor{green}{-0.46\%}$) 51_778 ($\textcolor{red}{0.24\%}$) 4_626 ($\textcolor{red}{0.35\%}$)
Rust 487_585 68_173 11_184

Statistics

  • binary_size: -0.46%
  • max_mem: no change
  • cycles: 0.29% [-0.05%, 0.64%]

Garbage Collection

generate 700k max mem batch_get 50 batch_put 50 batch_remove 50
default 1_068_192_695 ($\textcolor{green}{-8.81\%}$) 47_793_792 ($\textcolor{green}{-8.07\%}$) 119 119 119
copying 1_068_192_577 ($\textcolor{green}{-8.81\%}$) 47_793_792 ($\textcolor{green}{-8.07\%}$) 1_067_924_316 ($\textcolor{green}{-8.81\%}$) 1_068_004_203 ($\textcolor{green}{-8.81\%}$) 1_067_925_853 ($\textcolor{green}{-8.81\%}$)
compacting 1_545_586_176 ($\textcolor{green}{-7.57\%}$) 47_793_792 ($\textcolor{green}{-8.07\%}$) 1_192_139_528 ($\textcolor{green}{-7.59\%}$) 1_415_425_189 ($\textcolor{green}{-7.69\%}$) 1_439_317_325 ($\textcolor{green}{-8.00\%}$)
generational 2_304_140_531 ($\textcolor{green}{-8.89\%}$) 47_802_256 ($\textcolor{green}{-8.07\%}$) 882_208_645 ($\textcolor{green}{-11.74\%}$) 1_211_144 ($\textcolor{green}{-1.75\%}$) 1_103_549 ($\textcolor{green}{-0.03\%}$)
incremental 29_503_170 976_097_188 ($\textcolor{green}{-0.99\%}$) 471_911_803 ($\textcolor{green}{-1.27\%}$) 497_465_467 ($\textcolor{red}{0.78\%}$) 1_221_308_722 ($\textcolor{red}{11.81\%}$)

Actor class

binary size put new bucket put existing bucket get
Map 299_202 ($\textcolor{green}{-0.33\%}$) 813_521 ($\textcolor{green}{-0.31\%}$) 16_115 ($\textcolor{red}{0.10\%}$) 16_660 ($\textcolor{red}{0.10\%}$)

Statistics

  • binary_size: no change
  • max_mem: -6.66% [-9.68%, -3.64%]
  • cycles: -4.32% [-6.52%, -2.12%]

Publisher & Subscriber

pub_binary_size sub_binary_size subscribe_caller subscribe_callee publish_caller publish_callee
Motoko 161_274 ($\textcolor{green}{-0.41\%}$) 145_736 ($\textcolor{green}{-0.33\%}$) 28_593 11_963 22_854 ($\textcolor{green}{-0.04\%}$) 6_446 ($\textcolor{red}{0.25\%}$)
Rust 519_866 570_028 68_903 42_634 92_131 51_818

Statistics

  • binary_size: -0.37% [-0.60%, -0.14%]
  • max_mem: no change
  • cycles: 0.10% [-0.82%, 1.03%]

Overall Statistics

  • binary_size: -0.35% [-0.54%, -0.17%]
  • max_mem: -11.50% [-15.09%, -7.92%]
  • cycles: -1.73% [-2.37%, -1.08%]

Copy link

github-actions bot commented Feb 18, 2024

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:

  • generate 1m. Insert 1m Nat64 integers into the collection. For Motoko collections, it usually triggers the GC; the rest of the column are not likely to trigger GC.
  • max mem. For Motoko, it reports rts_max_heap_size after generate call; For Rust, it reports the Wasm's memory page * 32Kb.
  • batch_get 50. Find 50 elements from the collection.
  • batch_put 50. Insert 50 elements to the collection.
  • batch_remove 50. Remove 50 elements from the collection.
  • upgrade. Upgrade the canister with the same Wasm module. For non-stable benchmarks, the map state is persisted by serializing and deserializing states into stable memory. For stable benchmarks, the upgrade takes no cycles, as the state is already in the stable memory.

💎 Takeaways

  • The platform only charges for instruction count. Data structures which make use of caching and locality have no impact on the cost.
  • We have a limit on the maximal cycles per round. This means asymptotic behavior doesn't matter much. We care more about the performance up to a fixed N. In the extreme cases, you may see an $O(10000 n\log n)$ algorithm hitting the limit, while an $O(n^2)$ algorithm runs just fine.
  • Amortized algorithms/GC may need to be more eager to avoid hitting the cycle limit on a particular round.
  • Rust costs more cycles to process complicated Candid data, but it is more efficient in performing core computations.

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 189_916 8_184_618_025 56_000_256 342_784 6_462_528_122 368_420 10_728_193_099
triemap 195_458 13_661_315_924 68_228_576 252_649 657_794 648_084 15_499_470_884
rbtree 185_901 7_009_043_570 52_000_464 116_348 318_320 330_226 6_870_900_152
splay 190_447 13_157_617_583 48_000_400 631_329 662_998 928_144 4_308_925_798
btree 230_305 10_223_929_607 25_108_416 357_912 485_794 539_490 2_861_974_825
zhenya_hashmap 188_888 2_360_638_679 16_777_504 58_204 66_552 79_675 3_018_208_083
btreemap_rs 537_393 1_793_333_047 27_590_656 75_328 125_166 86_260 2_937_041_107
imrc_hashmap_rs 542_882 2_584_501_850 244_973_568 37_762 178_926 115_385 5_796_587_958
hashmap_rs 529_458 439_248_112 73_138_176 21_501 26_711 25_024 1_298_646_667

Priority queue

binary_size heapify 1m max mem pop_min 50 put 50 pop_min 50 upgrade
heap 166_898 5_554_617_018 24_000_360 621_690 227_224 592_588 3_189_831_485
heap_rs 525_853 139_669_830 18_284_544 57_419 23_051 57_545 510_960_192

Growable array

binary_size generate 5k max mem batch_get 500 batch_put 500 batch_remove 500 upgrade
buffer 173_895 2_601_059 65_644 95_506 803_474 173_506 3_091_310
vector 171_927 1_952_689 24_580 126_130 186_485 176_123 4_675_192
vec_rs 520_881 289_040 1_376_256 17_251 30_571 23_331 3_161_017

Stable structures

binary_size generate 50k max mem batch_get 50 batch_put 50 batch_remove 50 upgrade
btreemap_rs 537_393 76_200_333 2_555_904 64_886 97_044 85_272 126_265_270
btreemap_stable_rs 543_609 4_561_985_735 2_031_616 2_707_064 5_026_642 8_594_683 729_311
heap_rs 525_853 7_051_730 2_293_760 49_928 23_299 49_894 26_768_703
heap_stable_rs 506_559 271_553_517 458_752 2_294_851 238_596 2_277_771 729_317
vec_rs 520_881 3_079_382 2_293_760 17_251 18_421 17_719 24_671_551
vec_stable_rs 503_829 63_394_912 458_752 62_491 79_685 81_633 729_320

Environment

  • dfx 0.16.1
  • Motoko compiler 0.10.4 (source kfk8w3jy-5b63clja-3y6iac6j-vqw16jlw)
  • rustc 1.75.0 (82e1608df 2023-12-21)
  • ic-repl 0.6.2
  • ic-wasm 0.7.0

Cryptographic libraries

Measure different cryptographic libraries written in both Motoko and Rust.

  • SHA-2 benchmarks
    • 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.
  • Certified map. Merkle Tree for storing key-value pairs and generate witness according to the IC Interface Specification.
    • 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.

SHA-2

binary_size SHA-256 SHA-512 account_id neuron_id
Motoko 193_678 267_743_355 247_834_501 33_636 24_532
Rust 537_397 82_787_911 56_792_991 47_914 50_388

Certified map

binary_size generate 10k max mem inc witness upgrade
Motoko 243_625 4_666_119_661 3_430_044 553_629 407_936 274_434_719
Rust 565_792 6_409_147_805 2_228_224 1_019_959 303_897 6_019_483_730

Environment

  • dfx 0.16.1
  • Motoko compiler 0.10.4 (source kfk8w3jy-5b63clja-3y6iac6j-vqw16jlw)
  • rustc 1.75.0 (82e1608df 2023-12-21)
  • ic-repl 0.6.2
  • ic-wasm 0.7.0

Sample Dapps

Measure the performance of some typical dapps:

  • Basic DAO,
    with heartbeat disabled to make profiling easier. We have a separate benchmark to measure heartbeat performance.
  • DIP721 NFT

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 273_924 510_825 22_316 18_599 19_665 157_946
Rust 849_921 599_916 99_156 123_702 136_655 1_799_828

DIP721 NFT

binary_size init mint_token transfer_token upgrade
Motoko 220_392 481_158 30_204 8_764 89_833
Rust 869_104 236_542 368_044 91_941 1_999_207

Environment

  • dfx 0.16.1
  • Motoko compiler 0.10.4 (source kfk8w3jy-5b63clja-3y6iac6j-vqw16jlw)
  • rustc 1.75.0 (82e1608df 2023-12-21)
  • ic-repl 0.6.2
  • 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.
  • It is not easy to reliably capture the above events in one flamegraph, as the implementation detail
    of the replica can affect how we measure this. Typically, a correct flamegraph contains both setTimer and canister_global_timer function. If it's not there, we may need to adjust the script.

Heartbeat

binary_size heartbeat
Motoko 137_178 19_507
Rust 23_637 1_112

Timer

binary_size setTimer cancelTimer
Motoko 145_613 51_778 4_626
Rust 487_585 68_173 11_184

Environment

  • dfx 0.16.1
  • Motoko compiler 0.10.4 (source kfk8w3jy-5b63clja-3y6iac6j-vqw16jlw)
  • rustc 1.75.0 (82e1608df 2023-12-21)
  • ic-repl 0.6.2
  • 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.

    • default. Compile with the default GC option. With the current GC scheduler, generate will trigger the copying GC. The rest of the methods will not trigger GC.
    • copying. Compile with --force-gc --copying-gc.
    • compacting. Compile with --force-gc --compacting-gc.
    • generational. Compile with --force-gc --generational-gc.
    • incremental. Compile with --force-gc --incremental-gc.
  • Actor class. Measure the cost of spawning actor class, using the Actor classes example.

Garbage Collection

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

Actor class

binary size put new bucket put existing bucket get
Map 299_202 813_521 16_115 16_660

Environment

  • dfx 0.16.1
  • Motoko compiler 0.10.4 (source kfk8w3jy-5b63clja-3y6iac6j-vqw16jlw)
  • rustc 1.75.0 (82e1608df 2023-12-21)
  • ic-repl 0.6.2
  • 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_274 145_736 28_593 11_963 22_854 6_446
Rust 519_866 570_028 68_903 42_634 92_131 51_818

Environment

  • dfx 0.16.1
  • Motoko compiler 0.10.4 (source kfk8w3jy-5b63clja-3y6iac6j-vqw16jlw)
  • rustc 1.75.0 (82e1608df 2023-12-21)
  • ic-repl 0.6.2
  • ic-wasm 0.7.0

.github/workflows/perf.yml Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants