Skip to content

EVM Memory Repricing

The EVM's memory is a word-addressed byte array that stores its volatile state. Accessing memory, like any instruction in the EVM, incurs fees in a unit called gas.

Gas does not measure the direct cost of execution, but rather the computational effort required by a node's hardware to execute EVM instructions. Transactors pay for per unit of gas at market value which ultimately determines the execution cost.

Efficiency gains from hardware advancements, and client code optimizations warrants periodic repricing of gas costs.

This project focuses on analyzing the gas costs associated with EVM memory opcodes MSTORE, MSTORE8, and MLOAD.

Gas metering: An open problem

Evm throughput: Measures rate at which EVM performs computation.

The gas cost of an instruction (GiG_i) is calculated one of two ways:

Absolute gas consumed on a reference hardware

Method

  1. Measure the EVM throughput (TevmT_{evm}) on a reference hardware.
  2. Measure the execution time in seconds (tit_i) for the instruction.
  3. Gi=TevmtiG_i = T_{evm} * t_i .

Pros

  • Absolute measurement that ties gas to bare metal compute.

Cons

  • Requires defining a reference hardware targeting a desired EVM throughput (10 mgas/s for example).
  • Reliably measuring gas cost on the reference hardware is challenging.
  • Gas cost of the instruction is execution client dependant and may vary across clients. Further analysis may be required to come up with client independent cost.

Gas consumed relative to ecrecover

The goal is to measure the gas cost proportional to that of ecrecover.

Method

  1. Compute the gas cost ecrecover using method (1). Currently, ecrecover is priced at 3000 gas.
  2. Measure the execution time in seconds (tecrecovert_{ecrecover}) for ecrecover.
  3. Measure the execution time in seconds (tit_i) for the instruction.
  4. Gi=titecrecover×GecrecoverG_i = \frac{t_i}{t_{ecrecover}} \times G_{ecrecover}.

Pros

  • Largely hardware agnoistic.

Cons

  • The ecrecover-anchored gas cost calculation assumes that the executed machine code by the processor is comparable. It is probable that the processor optimizations may favor one over the other. Thus the calculated gas cost could be a close approximation.
  • Assumes ecrecover is fairly priced. Any change in the gas cost of ecrecover will have a cascading impact on the instruction's gas cost.

Analysis

Analysis of memory instructions using method (2).

1. Geth

Geth can be benchmarked using the Go testing package.

First, benchmark ecrecover:

# 1. Clone geth
git clone https://github.com/ethereum/go-ethereum.git 
 
# 2. Navigate to the EVM.
cd core/vm
 
# 3. Benchmark `ecrecover` precompile.
go test . -run - -bench BenchmarkPrecompiledEcrecover
 
goos: linux
goarch: amd64
pkg: github.com/ethereum/go-ethereum/core/vm
cpu: Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz
BenchmarkPrecompiledEcrecover/-Gas=3000-8         	   21684	     55048 ns/op	      3000 gas/op	        54.49 mgas/s	     800 B/op	       7 allocs/op
PASS
ok  	github.com/ethereum/go-ethereum/core/vm	1.772s
 

The benchmark reports that tecrecover=55048 nst_{ecrecover} = 55048 \text{ ns}.

Next, benchmark MSTORE:

go test . -run - -bench Mstore                       
goos: linux
goarch: amd64
pkg: github.com/ethereum/go-ethereum/core/vm
cpu: Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz
BenchmarkOpMstore-8   	58072719	        17.82 ns/op
PASS
ok  	github.com/ethereum/go-ethereum/core/vm	1.076s

tMstore=17.82 nst_{Mstore} = 17.82 \text{ ns}

GMstore=17.8255048×3000=0.97115251 gasG_{Mstore} = \frac{17.82}{55048} \times 3000 = 0.9711525 \approx 1 \text{ gas}

Resources