protocol: PayGas factorizes gas updates for instrs with static costs
In a Michelson script, most of the instructions have a statically determined cost in terms of gas consumption. We can update the gas counter and check for its exhaustion at the beginning of any sequence of such instructions:
- this is safe because we may only stop earlier than previously in case of gas exhaustion, and
- this is also better for performance. Indeed, preliminary benchmarks show that the Michelson interpreter spends a lot of time updating and checking the gas level. If we reduce the frequency of gas-related operations, we have more CPU time to actually execute the script instructions.
We proceed through the following steps:
- The introduction of
PayGas (n, i)
, a new Michelson instruction to payn
milligas before executingi
without monitoring gas. (Changes onScript_typed_ir
.) - The introduction of a preprocessing phase that inserts
PayGas
on appropriate sequences of instructions. - The introduction of a
gas_monitoring_status
in the Michelson step function.
We had to separate the cost_of_instr
function in two functions static_cost_of_instr
and dynamic_cost_of_instr
. To avoid any code duplication between these two functions, we introduce a new parameter to instr
which statically tells if the cost of an instruction can be statically determined.
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