Skip to content

protocol: Optimize gas counting

Yann Regis-Gianas requested to merge yrg@optimize-gas-counting into proto-proposal

The current implementation witnesses gas consumption by updating two gas levels, one to respect the operation gas limit and the other to respect the block gas limit. These updates decrement the levels by exactly the same amount. Two ZArith subtractions and two ZArith comparisons are paid while only one subtraction and only one comparison are actually needed. Indeed, let min_gas be the minimum between block_gas and operation_gas, and max_gas for their maximum. If one of the two counters has to hit 0, then it must be min_gas.

In this MR, gas updates and checks are only done on min_gas. By remembering which of block_gas and operation_gas was the minimum using a boolean, we can update max_gas afterward. This boolean can also be used to produce the right error message in case of gas exhaustion. This optimization removes two ZArith operations over four in the critical routine consume_gas.

This optimization is part of a sequence to optimize gas update and check for gas exhaustion documented here: https://hackmd.io/@yrg/rkLwB17wD

Edited by Yann Regis-Gianas

Merge request reports