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

Moc inline with new meter #80

Closed
wants to merge 10 commits into from

Conversation

chenyan-dfinity
Copy link
Contributor

@chenyan-dfinity chenyan-dfinity commented Sep 14, 2023

base: new metering, without wasm-opt

@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 Sep 14, 2023
@github-actions
Copy link

github-actions bot commented Sep 14, 2023

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
hashmap 167_316 ($\textcolor{red}{5.22\%}$) 9_230_374_182 ($\textcolor{green}{-2.81\%}$) 61_987_732 382_826 ($\textcolor{green}{-2.55\%}$) 7_246_862_420 ($\textcolor{green}{-0.75\%}$) 411_340 ($\textcolor{green}{-2.82\%}$)
triemap 171_390 ($\textcolor{red}{6.00\%}$) 16_198_090_366 ($\textcolor{green}{-6.37\%}$) 74_216_052 309_052 ($\textcolor{green}{-10.69\%}$) 782_396 ($\textcolor{green}{-6.99\%}$) 769_026 ($\textcolor{green}{-6.79\%}$)
rbtree 171_502 ($\textcolor{red}{5.86\%}$) 7_930_546_839 ($\textcolor{green}{-6.29\%}$) 57_995_940 137_477 ($\textcolor{green}{-13.33\%}$) 357_040 ($\textcolor{green}{-7.28\%}$) 401_036 ($\textcolor{green}{-6.16\%}$)
splay 166_269 ($\textcolor{red}{5.64\%}$) 15_696_985_410 ($\textcolor{green}{-9.90\%}$) 53_995_876 752_855 ($\textcolor{green}{-10.50\%}$) 790_594 ($\textcolor{green}{-10.64\%}$) 1_106_735 ($\textcolor{green}{-10.37\%}$)
btree 243_933 ($\textcolor{red}{14.06\%}$) 11_541_962_095 ($\textcolor{green}{-13.02\%}$) 31_103_892 400_549 ($\textcolor{green}{-13.15\%}$) 543_692 ($\textcolor{green}{-13.60\%}$) 612_045 ($\textcolor{green}{-13.38\%}$)
zhenya_hashmap 184_431 ($\textcolor{red}{9.50\%}$) 3_538_128_781 ($\textcolor{green}{-8.72\%}$) 65_987_480 93_396 ($\textcolor{green}{-12.41\%}$) 112_641 ($\textcolor{green}{-14.20\%}$) 129_949 ($\textcolor{green}{-16.74\%}$)
btreemap_rs 446_267 1_797_752_179 13_762_560 74_544 126_136 92_839
imrc_hashmap_rs 446_166 2_571_892_333 122_454_016 38_956 179_095 115_561
hashmap_rs 439_346 447_664_894 36_536_320 22_228 27_664 25_290

Priority queue

binary_size heapify 1m max mem pop_min 50 put 50
heap 158_536 ($\textcolor{red}{4.08\%}$) 6_553_514_370 ($\textcolor{green}{-10.18\%}$) 29_995_836 720_429 ($\textcolor{green}{-11.41\%}$) 265_478 ($\textcolor{green}{-10.78\%}$)
heap_rs 437_278 142_914_793 9_109_504 59_850 23_726

Growable array

binary_size generate 5k max mem batch_get 500 batch_put 500 batch_remove 500
buffer 171_769 ($\textcolor{red}{6.40\%}$) 2_839_066 ($\textcolor{green}{-12.99\%}$) 65_508 108_809 ($\textcolor{green}{-13.18\%}$) 896_597 ($\textcolor{green}{-14.00\%}$) 186_809 ($\textcolor{green}{-11.18\%}$)
vector 172_652 ($\textcolor{red}{7.39\%}$) 2_430_538 ($\textcolor{green}{-12.11\%}$) 24_764 170_991 ($\textcolor{green}{-13.44\%}$) 232_826 ($\textcolor{green}{-11.91\%}$) 231_638 ($\textcolor{green}{-13.31\%}$)
vec_rs 435_834 290_143 655_360 17_605 31_014 25_400

Statistics

  • binary_size: 7.13% [5.26%, 8.99%]
  • max_mem: no change
  • cycles: -10.11% [-11.22%, -9.01%]

SHA-2

binary_size SHA-256 SHA-512 account_id neuron_id
Motoko 230_070 ($\textcolor{red}{17.28\%}$) 326_935_518 ($\textcolor{green}{-7.34\%}$) 298_093_814 ($\textcolor{green}{-12.09\%}$) 41_503 ($\textcolor{green}{-7.39\%}$) 29_780 ($\textcolor{green}{-6.70\%}$)
Rust 528_234 82_789_387 56_794_263 50_651 53_532

Certified map

binary_size generate 10k max mem inc witness
Motoko 232_943 ($\textcolor{red}{13.59\%}$) 5_792_324_783 ($\textcolor{green}{-7.36\%}$) 3_429_924 687_589 ($\textcolor{green}{-7.39\%}$) 445_626 ($\textcolor{green}{-12.01\%}$)
Rust 469_955 6_359_442_714 1_081_344 1_012_174 305_119

Statistics

  • binary_size: 15.43% [3.79%, 27.08%]
  • max_mem: no change
  • cycles: -8.61% [-10.35%, -6.88%]

Basic DAO

binary_size init transfer_token submit_proposal vote_proposal
Motoko 301_252 ($\textcolor{red}{8.60\%}$) 50_427 ($\textcolor{green}{-1.68\%}$) 24_523 ($\textcolor{green}{-2.40\%}$) 20_382 ($\textcolor{green}{-2.77\%}$) 21_891 ($\textcolor{green}{-2.69\%}$)
Rust 763_017 552_075 105_203 128_753 139_539

DIP721 NFT

binary_size init mint_token transfer_token
Motoko 244_819 ($\textcolor{red}{6.43\%}$) 19_098 ($\textcolor{green}{-0.88\%}$) 31_854 ($\textcolor{green}{-1.37\%}$) 9_565 ($\textcolor{green}{-1.67\%}$)
Rust 828_238 146_257 380_260 93_763

Statistics

  • binary_size: 7.52% [0.67%, 14.37%]
  • max_mem: no change
  • cycles: -1.92% [-2.45%, -1.40%]

Heartbeat

binary_size heartbeat
Motoko 145_761 ($\textcolor{red}{2.57\%}$) 23_984 ($\textcolor{red}{19.19\%}$)
Rust 25_650 1_179

Timer

binary_size setTimer cancelTimer
Motoko 154_792 ($\textcolor{red}{3.72\%}$) 54_057 ($\textcolor{green}{-0.83\%}$) 4_942 ($\textcolor{green}{-0.72\%}$)
Rust 470_693 69_727 11_405

Statistics

  • binary_size: 3.72%
  • max_mem: no change
  • cycles: -0.77% [-1.10%, -0.45%]

Garbage Collection

Note
Same as main branch, skipping.

Actor class

binary size put new bucket put existing bucket get
Map 311_868 ($\textcolor{red}{4.75\%}$) 818_044 ($\textcolor{red}{4.39\%}$) 16_950 ($\textcolor{green}{-0.56\%}$) 17_406 ($\textcolor{green}{-0.71\%}$)

Statistics

  • binary_size: no change
  • max_mem: no change
  • cycles: 1.97% [-1.58%, 5.51%]

Publisher & Subscriber

pub_binary_size sub_binary_size subscribe_caller subscribe_callee publish_caller publish_callee
Motoko 172_768 ($\textcolor{red}{3.59\%}$) 155_860 ($\textcolor{red}{2.61\%}$) 29_874 ($\textcolor{green}{-0.27\%}$) 12_492 ($\textcolor{green}{-0.51\%}$) 23_937 ($\textcolor{green}{-0.51\%}$) 6_816 ($\textcolor{green}{-0.81\%}$)
Rust 511_870 565_407 71_728 44_318 95_767 53_941

Statistics

  • binary_size: 3.10% [0.02%, 6.19%]
  • max_mem: no change
  • cycles: -0.53% [-0.79%, -0.26%]

Overall Statistics

  • binary_size: 7.50% [5.67%, 9.33%]
  • max_mem: no change
  • cycles: -7.18% [-8.35%, -6.01%]

@github-actions
Copy link

github-actions bot commented Sep 14, 2023

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 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.

💎 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 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.
  • 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
hashmap 167_316 9_230_374_182 61_987_732 382_826 7_246_862_420 411_340
triemap 171_390 16_198_090_366 74_216_052 309_052 782_396 769_026
rbtree 171_502 7_930_546_839 57_995_940 137_477 357_040 401_036
splay 166_269 15_696_985_410 53_995_876 752_855 790_594 1_106_735
btree 243_933 11_541_962_095 31_103_892 400_549 543_692 612_045
zhenya_hashmap 184_431 3_538_128_781 65_987_480 93_396 112_641 129_949
btreemap_rs 446_267 1_797_752_179 13_762_560 74_544 126_136 92_839
imrc_hashmap_rs 446_166 2_571_892_333 122_454_016 38_956 179_095 115_561
hashmap_rs 439_346 447_664_894 36_536_320 22_228 27_664 25_290

Priority queue

binary_size heapify 1m max mem pop_min 50 put 50
heap 158_536 6_553_514_370 29_995_836 720_429 265_478
heap_rs 437_278 142_914_793 9_109_504 59_850 23_726

Growable array

binary_size generate 5k max mem batch_get 500 batch_put 500 batch_remove 500
buffer 171_769 2_839_066 65_508 108_809 896_597 186_809
vector 172_652 2_430_538 24_764 170_991 232_826 231_638
vec_rs 435_834 290_143 655_360 17_605 31_014 25_400

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.

SHA-2

binary_size SHA-256 SHA-512 account_id neuron_id
Motoko 230_070 326_935_518 298_093_814 41_503 29_780
Rust 528_234 82_789_387 56_794_263 50_651 53_532

Certified map

binary_size generate 10k max mem inc witness
Motoko 232_943 5_792_324_783 3_429_924 687_589 445_626
Rust 469_955 6_359_442_714 1_081_344 1_012_174 305_119

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 301_252 50_427 24_523 20_382 21_891
Rust 763_017 552_075 105_203 128_753 139_539

DIP721 NFT

binary_size init mint_token transfer_token
Motoko 244_819 19_098 31_854 9_565
Rust 828_238 146_257 380_260 93_763

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 145_761 23_984
Rust 25_650 1_179

Timer

binary_size setTimer cancelTimer
Motoko 154_792 54_057 4_942
Rust 470_693 69_727 11_405

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 800k max mem batch_get 50 batch_put 50 batch_remove 50
default 1_338_231_405 59_396_776 118 118 118
copying 1_338_231_287 59_396_776 1_337_913_569 1_338_002_371 1_337_919_144
compacting 1_911_420_608 59_396_776 1_473_824_186 1_756_485_066 1_787_369_954
generational 2_891_818_643 59_405_240 1_141_865_993 1_217_376 1_117_840
incremental 33_436_719 1_136_155_048 333_734_166 336_829_512 336_860_690

Actor class

binary size put new bucket put existing bucket get
Map 311_868 818_044 16_950 17_406

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 172_768 155_860 29_874 12_492 23_937 6_816
Rust 511_870 565_407 71_728 44_318 95_767 53_941

@chenyan-dfinity chenyan-dfinity deleted the moc-inline-with-new-meter branch November 27, 2023 20:08
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.

1 participant