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 () is calculated one of two ways:
Absolute gas consumed on a reference hardware
Method
- Measure the EVM throughput () on a reference hardware.
- Measure the execution time in seconds () for the instruction.
- .
Pros
- Absolute measurement that ties gas to bare metal compute.
Cons
- Requires defining a reference hardware targeting a desired EVM throughput (
10 mgas/sfor 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
- Compute the gas cost
ecrecoverusing method (1). Currently,ecrecoveris priced at3000gas. - Measure the execution time in seconds () for
ecrecover. - Measure the execution time in seconds () for the instruction.
- .
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
ecrecoveris fairly priced. Any change in the gas cost ofecrecoverwill 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 .
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
Resources
- 📄 Gavin W., Ethereum Yellow Paper
- 📄 EPF Wiki, EVM
- 📄 Eth Research, On Block Sizes, Gas Limits and Scalability
- 📄 John A., Wait, It's All Resource Pricing?
- 📄 John A., Induced Demand from Blockchain Resource Pricing
- 📄 Martin H., Gas benchmarks
- 📜 Ipsilon, EVM benchmarks
- 📄 Ethereum Research, Gas Price Table
- 📄 Ipsilon et al., EVM384 Update 5: First Gas Cost Estimates
- 📜 Geth, Protocol Params
- 📄 Eth Research,EIP-1380: Reduced gas cost for call to self
- 📄 Michael K., A Scalable Method to Analyze Gas Costs, Loops and Related Security Vulnerabilities on the Ethereum Virtual Machine