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

rust: opt z #61

Closed
wants to merge 1 commit into from
Closed

rust: opt z #61

wants to merge 1 commit into from

Conversation

chenyan-dfinity
Copy link
Contributor

No description provided.

@chenyan-dfinity chenyan-dfinity added the build_base Build base instead of fetching from gh-pages. Note that the build tool runs in the same version label Jul 20, 2023
@github-actions
Copy link

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

Map

binary_size generate 50k max mem batch_get 50 batch_put 50 batch_remove 50
hashmap 152_580 1_195_632_150 9_102_052 545_645 365_569_669 520_876
triemap 156_424 1_338_995_779 9_715_900 459_710 1_193_026 686_569
rbtree 153_258 1_115_533_975 8_902_160 354_721 964_237 495_133
splay 152_693 1_323_550_652 8_702_096 719_103 1_214_198 717_146
btree 180_227 1_222_588_229 7_556_172 502_876 1_090_262 540_393
zhenya_hashmap 148_470 989_558_312 9_301_800 334_927 818_203 335_264
btreemap_rs 371_846 ($\textcolor{green}{-24.78\%}$) 114_550_712 ($\textcolor{red}{1.60\%}$) 1_638_400 65_784 ($\textcolor{red}{10.39\%}$) 138_061 ($\textcolor{red}{2.84\%}$) 64_872 ($\textcolor{red}{6.95\%}$)
hashmap_rs 364_644 ($\textcolor{green}{-24.85\%}$) 52_270_888 ($\textcolor{red}{5.37\%}$) 1_835_008 25_036 ($\textcolor{red}{27.09\%}$) 63_382 ($\textcolor{red}{6.33\%}$) 24_568 ($\textcolor{red}{17.36\%}$)

Priority queue

binary_size heapify 50k mem pop_min 50 put 50
heap 139_951 369_466_193 1_400_024 334_365 397_474
heap_rs 354_392 ($\textcolor{green}{-22.75\%}$) 8_202_538 ($\textcolor{red}{64.86\%}$) 819_200 50_572 ($\textcolor{red}{3.59\%}$) 24_096 ($\textcolor{red}{16.47\%}$)

MoVM

binary_size generate 10k max mem batch_get 50 batch_put 50 batch_remove 50
hashmap 152_580 238_966_334 1_820_844 543_937 73_525_914 518_626
hashmap_rs 364_644 ($\textcolor{green}{-24.85\%}$) 10_784_704 ($\textcolor{red}{5.49\%}$) 950_272 24_369 ($\textcolor{red}{28.05\%}$) 62_700 ($\textcolor{red}{6.38\%}$) 23_504 ($\textcolor{red}{18.26\%}$)
imrc_hashmap_rs 372_245 ($\textcolor{green}{-23.96\%}$) 27_113_028 ($\textcolor{red}{3.69\%}$) 1_572_864 34_382 ($\textcolor{red}{15.14\%}$) 161_210 ($\textcolor{red}{4.72\%}$) 45_176 ($\textcolor{red}{21.72\%}$)
movm_rs 1_068_493 ($\textcolor{green}{-42.09\%}$) 1_313_685_946 ($\textcolor{red}{13.56\%}$) 2_654_208 3_125_628 ($\textcolor{red}{16.63\%}$) 8_381_481 ($\textcolor{red}{13.82\%}$) 6_580_605 ($\textcolor{red}{12.84\%}$)
movm_dynamic_rs 1_072_010 ($\textcolor{green}{-45.60\%}$) 588_634_314 ($\textcolor{red}{7.82\%}$) 2_129_920 2_322_074 ($\textcolor{red}{7.12\%}$) 3_198_577 ($\textcolor{red}{8.60\%}$) 2_331_150 ($\textcolor{red}{8.62\%}$)

Basic DAO

binary_size init transfer_token submit_proposal vote_proposal
Motoko 225_805 37_469 ($\textcolor{green}{-0.13\%}$) 16_274 ($\textcolor{red}{0.05\%}$) 12_658 ($\textcolor{green}{-0.32\%}$) 14_106 ($\textcolor{green}{-0.49\%}$)
Rust 549_566 ($\textcolor{green}{-29.45\%}$) 650_777 ($\textcolor{red}{30.58\%}$) 115_227 ($\textcolor{red}{23.61\%}$) 138_295 ($\textcolor{red}{21.01\%}$) 166_189 ($\textcolor{red}{33.29\%}$)

DIP721 NFT

binary_size init mint_token transfer_token
Motoko 183_882 12_181 22_319 4_710
Rust 601_886 ($\textcolor{green}{-29.66\%}$) 169_745 ($\textcolor{red}{26.57\%}$) 456_474 ($\textcolor{red}{32.29\%}$) 115_432 ($\textcolor{red}{36.62\%}$)

Heartbeat

binary_size heartbeat
Motoko 118_909 7_392
Rust 23_508 ($\textcolor{green}{-21.39\%}$) 788 ($\textcolor{red}{44.06\%}$)

Timer

binary_size setTimer cancelTimer
Motoko 125_168 15_208 1_679
Rust 374_157 ($\textcolor{green}{-24.90\%}$) 59_409 ($\textcolor{red}{16.54\%}$) 11_037 ($\textcolor{red}{13.01\%}$)

Publisher & Subscriber

pub_binary_size sub_binary_size subscribe_caller subscribe_callee publish_caller publish_callee
Motoko 139_886 126_827 14_632 8_451 10_530 3_662
Rust 370_566 ($\textcolor{green}{-30.69\%}$) 415_286 ($\textcolor{green}{-29.61\%}$) 69_260 ($\textcolor{red}{19.09\%}$) 46_431 ($\textcolor{red}{20.65\%}$) 98_734 ($\textcolor{red}{22.75\%}$) 55_945 ($\textcolor{red}{22.78\%}$)

@github-actions
Copy link

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.

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 50k. Insert 50k Nat32 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_live_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.

💎 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 nlogn) 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 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 of batch_put 50 is much higher than other data structures.
  • hashmap_rs uses the fxhash crate, which is the same as std::collections::HashMap, but with a deterministic hasher. This ensures reproducible result.
  • btree comes from Byron Becker's stable BTreeMap library.
  • zhenya_hashmap comes from Zhenya Usenko's stable HashMap library.
  • The MoVM table measures the performance of an experimental implementation of Motoko interpreter. External developers can ignore this table for now.

Map

binary_size generate 50k max mem batch_get 50 batch_put 50 batch_remove 50
hashmap 152_580 1_195_632_150 9_102_052 545_645 365_569_669 520_876
triemap 156_424 1_338_995_779 9_715_900 459_710 1_193_026 686_569
rbtree 153_258 1_115_533_975 8_902_160 354_721 964_237 495_133
splay 152_693 1_323_550_652 8_702_096 719_103 1_214_198 717_146
btree 180_227 1_222_588_229 7_556_172 502_876 1_090_262 540_393
zhenya_hashmap 148_470 989_558_312 9_301_800 334_927 818_203 335_264
btreemap_rs 371_846 114_550_712 1_638_400 65_784 138_061 64_872
hashmap_rs 364_644 52_270_888 1_835_008 25_036 63_382 24_568

Priority queue

binary_size heapify 50k mem pop_min 50 put 50
heap 139_951 369_466_193 1_400_024 334_365 397_474
heap_rs 354_392 8_202_538 819_200 50_572 24_096

MoVM

binary_size generate 10k max mem batch_get 50 batch_put 50 batch_remove 50
hashmap 152_580 238_966_334 1_820_844 543_937 73_525_914 518_626
hashmap_rs 364_644 10_784_704 950_272 24_369 62_700 23_504
imrc_hashmap_rs 372_245 27_113_028 1_572_864 34_382 161_210 45_176
movm_rs 1_068_493 1_313_685_946 2_654_208 3_125_628 8_381_481 6_580_605
movm_dynamic_rs 1_072_010 588_634_314 2_129_920 2_322_074 3_198_577 2_331_150

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
Motoko 225_805 37_469 16_274 12_658 14_106
Rust 549_566 650_777 115_227 138_295 166_189

DIP721 NFT

binary_size init mint_token transfer_token
Motoko 183_882 12_181 22_319 4_710
Rust 601_886 169_745 456_474 115_432

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 118_909 7_392
Rust 23_508 788

Timer

binary_size setTimer cancelTimer
Motoko 125_168 15_208 1_679
Rust 374_157 59_409 11_037

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 139_886 126_827 14_632 8_451 10_530 3_662
Rust 370_566 415_286 69_260 46_431 98_734 55_945

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build_base Build base instead of fetching from gh-pages. Note that the build tool runs in the same version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant