Module finite_wasm::gas
source · Expand description
Analysis of the amount of time (gas) a function takes to execute.
The gas analysis is a two-pass linear-time algorithm. This algorithm first constructs a table containing instrumentation offsets (represented as instructions in the example below), their costs and kinds:
| Instructions | Cost | Kind | | ============ | ==== | ==== | | i32.const 0 | 1 | Pure | | i32.const 1 | 1 | Pure | | i32.add | 1 | Pure |
In this table the instructions with certain instrumentation kind combinations can then be
coalesced in order to reduce the number of gas instrumentation points. For example all
instrumentation can be merged together across all instructions considered Pure to produce a
table like this:
| Instructions | Cost | Kind | | ====================================== | ==== | ==== | | (i32.add (i32.const 0) (i32.const 1)) | 3 | Pure |
Instrumentation can then, instead of inserting a gas charge before each of the 3 instructions, insert a charge of 3 gas before the entire sequence, all without any observable difference in execution semantics.
Why two passes? A short answer is – branching. As the algorithm goes through the function
code for the first time, it can mark certain instructions as being a branch or a branch target.
For example end in block…end can be either pure or a branch target. Table entries for the
instructions that participate in control flow cannot be merged together if an eventually
accurate gas count is desired.
Structs
- The per-function state used by the
Visitor. - The core algorihtm of the
gasanalysis.
Enums
- The type of a particular instrumentation point (as denoted by its offset.)
Traits
- The configuration for the gas analysis.