5

Branchless Programming: Why "If" is Sloowww... and what we can do about it! is an example of low level optimizations that you might pursue in a blockchain system to reduce the overhead of your runtime. But in the context of FRAME, a fair amount of the implementation details of much of the runtime logic is behind macros that may interfere with efforts to optimize, and worst case might even lead to worsening the operational overhead of of the logic you are trying to improve.

What best practices when you are trying to optimize are there in rumtime developement? What are the "footguns"?

1
  • perhaps a new question could be around: "how much is it the Substrate consumer's job to optimize FRAME logic at a low level?" or something similar... looking to understand how the structure of Substrate is crafted to help help 9or sometimes mislead!) runtime devs on best practices.
    – Nuke
    Commented Mar 27, 2022 at 17:31

1 Answer 1

5

Branchless programming doesn't make any sense in a blockchain. Yes that is exaggerated, only if you implement your own crypto (which you should not do) or anything else that is running in a really tight loop you could benefit of Branchless programming. However, for 99.999% of the code you write in your runtime this isn't required.

The runtime is mainly bound by IO, because it is costly to read/write to the state. This is also the reason weighting mainly boils down to measuring storage read/writes. Yes, if you are only doing computation that will also have a weight. However, if you do some complex computation in your runtime you are probably already doing something wrong.

So, when you want to optimize your pallet calls, ensure that you don't read data multiple times or that you write too much data.

3
  • (If you happen to read the exact same data multiple times though I belive the additional reads are free)
    – Squirrel
    Commented Mar 27, 2022 at 19:14
  • Not literally free. Nothing is literally free. But the overhead compared to the main trie access is nominal. Still, writing efficient code means accessing the sp-io layers as few times as possible.
    – Shawn Tabrizi
    Commented Mar 27, 2022 at 19:17
  • 2
    Exactly. It still involves going to the host and decoding the data on the runtime side.
    – bkchr
    Commented Mar 27, 2022 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.