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 2 thin #64

Closed
wants to merge 2 commits into from
Closed

rust: opt 2 thin #64

wants to merge 2 commits 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 480_751 ($\textcolor{red}{11.33\%}$) 111_078_361 ($\textcolor{green}{-0.11\%}$) 1_638_400 59_113 ($\textcolor{red}{2.24\%}$) 131_590 ($\textcolor{red}{0.47\%}$) 59_945 ($\textcolor{green}{-1.59\%}$)
hashmap_rs 470_142 ($\textcolor{red}{10.49\%}$) 47_589_394 ($\textcolor{red}{0.18\%}$) 1_835_008 18_965 ($\textcolor{red}{7.25\%}$) 56_881 ($\textcolor{red}{3.79\%}$) 20_093 ($\textcolor{red}{9.82\%}$)

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 444_993 ($\textcolor{red}{10.03\%}$) 5_174_504 ($\textcolor{green}{-0.93\%}$) 819_200 47_819 ($\textcolor{red}{4.23\%}$) 19_994 ($\textcolor{red}{7.25\%}$)

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 470_142 ($\textcolor{red}{10.49\%}$) 9_819_746 ($\textcolor{red}{0.19\%}$) 950_272 18_313 ($\textcolor{red}{7.53\%}$) 56_220 ($\textcolor{red}{3.85\%}$) 19_060 ($\textcolor{red}{10.56\%}$)
imrc_hashmap_rs 476_108 ($\textcolor{red}{10.25\%}$) 25_686_703 ($\textcolor{red}{0.44\%}$) 1_572_864 29_065 ($\textcolor{red}{2.23\%}$) 150_711 ($\textcolor{red}{0.89\%}$) 35_877 ($\textcolor{green}{-1.12\%}$)
movm_rs 1_861_422 ($\textcolor{red}{9.51\%}$) 1_134_109_403 ($\textcolor{red}{4.08\%}$) 2_654_208 2_637_662 ($\textcolor{red}{5.61\%}$) 7_233_383 ($\textcolor{red}{3.67\%}$) 5_724_409 ($\textcolor{red}{3.98\%}$)
movm_dynamic_rs 1_979_686 ($\textcolor{red}{9.17\%}$) 537_155_009 ($\textcolor{red}{5.34\%}$) 2_129_920 2_135_129 ($\textcolor{red}{4.37\%}$) 2_900_023 ($\textcolor{red}{5.30\%}$) 2_115_948 ($\textcolor{red}{3.56\%}$)

Basic DAO

binary_size init transfer_token submit_proposal vote_proposal
Motoko 225_805 37_469 ($\textcolor{green}{-0.13\%}$) 16_320 ($\textcolor{red}{0.62\%}$) 12_656 ($\textcolor{green}{-0.33\%}$) 14_105 ($\textcolor{green}{-0.50\%}$)
Rust 776_744 ($\textcolor{red}{10.14\%}$) 484_764 ($\textcolor{red}{2.77\%}$) 89_706 ($\textcolor{red}{3.74\%}$) 109_947 ($\textcolor{red}{5.06\%}$) 119_413 ($\textcolor{red}{3.17\%}$)

DIP721 NFT

binary_size init mint_token transfer_token
Motoko 183_882 12_181 22_319 4_710
Rust 846_866 ($\textcolor{red}{10.54\%}$) 129_299 ($\textcolor{red}{3.41\%}$) 334_021 ($\textcolor{red}{2.87\%}$) 81_345 ($\textcolor{red}{5.48\%}$)

Heartbeat

binary_size heartbeat
Motoko 118_909 7_392 ($\textcolor{red}{97.07\%}$)
Rust 26_592 ($\textcolor{red}{12.21\%}$) 789 ($\textcolor{green}{-0.25\%}$)

Timer

binary_size setTimer cancelTimer
Motoko 125_168 15_208 1_679
Rust 480_083 ($\textcolor{red}{10.38\%}$) 48_291 ($\textcolor{red}{10.91\%}$) 8_410 ($\textcolor{red}{9.46\%}$)

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 522_318 ($\textcolor{red}{10.63\%}$) 571_072 ($\textcolor{red}{9.82\%}$) 55_040 ($\textcolor{red}{6.69\%}$) 36_901 ($\textcolor{red}{6.46\%}$) 77_002 ($\textcolor{red}{3.82\%}$) 43_615 ($\textcolor{red}{4.81\%}$)

@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 480_751 111_078_361 1_638_400 59_113 131_590 59_945
hashmap_rs 470_142 47_589_394 1_835_008 18_965 56_881 20_093

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 444_993 5_174_504 819_200 47_819 19_994

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 470_142 9_819_746 950_272 18_313 56_220 19_060
imrc_hashmap_rs 476_108 25_686_703 1_572_864 29_065 150_711 35_877
movm_rs 1_861_422 1_134_109_403 2_654_208 2_637_662 7_233_383 5_724_409
movm_dynamic_rs 1_979_686 537_155_009 2_129_920 2_135_129 2_900_023 2_115_948

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_320 12_656 14_105
Rust 776_744 484_764 89_706 109_947 119_413

DIP721 NFT

binary_size init mint_token transfer_token
Motoko 183_882 12_181 22_319 4_710
Rust 846_866 129_299 334_021 81_345

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 26_592 789

Timer

binary_size setTimer cancelTimer
Motoko 125_168 15_208 1_679
Rust 480_083 48_291 8_410

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 522_318 571_072 55_040 36_901 77_002 43_615

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