2

I'm trying to figure out how to optimize one of my smart contracts for less billed CPU-usage.

There are typically different methods to optimize performance of algorithms but I'm not clear about if a high-performance smart-contract also results in less CPU-usage.

From the eos.io-whitepaper:

On a launched blockchain adopting the EOS.IO software, at a network level all transactions are billed a computational bandwidth cost based on the number of WASM instructions executed. However, each individual block producer using the software may calculate resource usage using their own algorithm and measurements.

  1. Is this CPU-billing-scheme still in use?

  2. A very basic example of a wasm-instruction is for example the sqrt instruction. Let's imagine i implement a more performant sqrt-algorithtm (i know it's nearly impossoble and i will never do that!) which doesn't use a single wasm sqrt and instead uses multiple shifts, subs and divs or whatever and therefore executes more WASM Instructions, will the billed CPU-usage for execution be higher?

  3. Another different and very basic example is multiplying two int32 vs multiplying two floats. In traditional environemts multiplying two floats will result in slightly higher CPU-usage than when multiplying two integers - but if we only count wasm instructions both calculations execute the same amount of wasm instructions - 1.

To get to the Point:

If I want to optimize the code of a smart contract for less billed CPU-usage, do i have to write high-performance code - or do i have to reduce the amount of wasm instructions executed by the contract?

2
  • Check this: medium.com/@pEOS_one/…
    – Nat
    Commented Apr 27, 2019 at 17:26
  • 1
    Using the number of WASM instructions for billing CPU bandwidth consumption is dropped. Instead, timer is used as Phillip explained, and the amount of CPU bandwidth consumed by each transaction is shipped in block and propagated. (to let all BPs record same resource consumption)
    – conr2d
    Commented Jul 4, 2019 at 9:12

1 Answer 1

1
  1. Is this CPU-billing-scheme still in use?

To the best of my knowledge, yes

Points 2. and 3.

My understanding of CPU billing works is as follows:

  1. A BP receives your transaction, starts a timer, processes it as fast as they can, stops a timer.
  2. The time they record is the CPU billing time that you get charged.

Therefore, the number of wasm instructions itself is less important than the amount of time it takes to execute those instructions in the CPU of the block producer. So you should write high performance code.

2
  • Simply measuring CPU time would not be deterministic, as block producers run on different CPUs. Commented Jul 6, 2019 at 11:48
  • Correct. Which is why you sometimes get subjective failures when a transaction lasts longer than 30ms. Depends on the BP Commented Jul 6, 2019 at 12:14

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.