Skip to content

Speed up gas metering by batching instead of per-instruction #35

Open

Description

@daiagi

What we need to do

Right now every single instruction calls gas_cost() and creates a new state struct. Since all instructions cost 1 gas anyway, we can batch this and only check gas every 16 instructions or so.

Expected outcome

15-20% speedup by eliminating overhead on every instruction

Code example

Current (slow):

# This happens for EVERY instruction
gas_cost = Instructions.gas_cost(opcode)  # Always returns 1
deduct_gas = %{state_ | gas: state.gas - gas_cost}

Better (fast):

# Only check gas every 16 instructions
instruction_count = instruction_count + 1
if rem(instruction_count, 16) == 0 do
  new_state = %{state_ | gas: state.gas - 16}
  if new_state.gas < 0, do: {:out_of_gas, new_state}
end

This eliminates the pattern matching and struct creation on 15 out of every 16 instructions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions