EIP-3855 · Final · Core · 2021-02-19
EIP-3855: PUSH0 instruction
Adds the PUSH0 opcode (0x5f), which pushes a zero onto the stack for 2 gas in one byte, making contracts smaller and cheaper than with PUSH1 0.
What problem does it solve?
Zero is the most common constant in EVM code: memory offsets, return-data sizes in CALL, empty values, default arguments. Before this EIP the only honest way to push it was PUSH1 0x00, which costs 3 gas and takes two bytes of bytecode (and every deployed byte costs 200 gas).
Because that felt wasteful, compilers and hand-optimisers used tricks such as RETURNDATASIZE, CALLVALUE or MSIZE, which happen to return zero in some contexts. Those tricks are fragile: if a future upgrade changes when such an opcode returns zero, contracts that depend on it may break, and core developers become reluctant to change those opcodes at all.
The authors measured that about 11.5 % of all executed PUSH* instructions on Mainnet pushed zero, so a dedicated instruction pays off broadly.
How it works
- New opcode
PUSH0at0x5f, right beforePUSH1(0x60), so it sits in the same contiguousPUSHrange. - No immediate data, pops nothing, pushes one item with value
0. - Costs 2 gas (the
basetier, same asADDRESS,ORIGIN, etc.). - Stack limits apply as usual: 1024
PUSH0s succeed, the 1025th aborts with stack overflow. - Jump-destination analysis is unaffected because there are no immediate bytes to skip.
It was activated on Mainnet in the Shanghai upgrade (April 2023), together with EIP-3651, EIP-3860 and EIP-4895.
Sample code
Bytecode comparison for "return zero bytes" (RETURN(0, 0)):
Before: 60 00 60 00 f3 PUSH1 0, PUSH1 0, RETURN -> 5 bytes, 6 gas for the pushes
After: 5f 5f f3 PUSH0, PUSH0, RETURN -> 3 bytes, 4 gas for the pushes
Solidity emits PUSH0 automatically when targeting Shanghai or later. Pin the EVM version explicitly in your build config if you deploy to several chains:
{
"language": "Solidity",
"settings": {
"evmVersion": "shanghai",
"optimizer": { "enabled": true, "runs": 200 }
}
}
In inline assembly there is no special syntax; the compiler chooses PUSH0 for a literal 0:
function zero() external pure returns (uint256 z) {
assembly { z := 0 } // compiles to PUSH0 on shanghai+
}
Things to know
- Bytecode containing
0x5ffails with an invalid-opcode error on chains or L2s that have not activated Shanghai. This was a common deployment surprise when Solidity 0.8.20 changed its default EVM target to Shanghai; setevmVersiontoparisfor such chains. - Before activation,
0x5fwas an undefined opcode, so contracts already deployed with that byte in executable code could change behaviour. In practice this was not considered a real risk. - The gain per instruction is small (1 gas, 1 byte) but it applies to almost every contract.