Repository navigation
Conversation
Contributor
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1788 +/- ##
=======================================
Coverage 98.24% 98.25%
=======================================
Files 186 186
Lines 17175 17264 +89
Branches 3921 3939 +18
=======================================
+ Hits 16873 16962 +89
Misses 233 233
Partials 69 69
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
chfast
force-pushed
the
evm/grow-memory-writes
branch
3 times, most recently
from
October 28, 2026 18:13
fbed073 to
ae491d8
Compare
chfast
marked this pull request as ready for review
October 28, 2026 18:15
chfast
force-pushed
the
evm/grow-memory-writes
branch
2 times, most recently
from
October 28, 2026 20:15
6afb0d1 to
f8fc9f8
Compare
chfast
added a commit
that referenced
this pull request
Oct 29, 2026
`Memory::grow()` grows the capacity with `realloc()` only once per doubling of the buffer, but that code sat inline in every growth. It moves to `Memory::grow_capacity()`, out of line and under `[[unlikely]]`, which then zeroes the extent with `grow_within_capacity()`, as `grow()` does within the capacity. This is split out of #1788, where `grow_within_capacity()` leaves the words the instruction overwrites unzeroed. There, zeroing after a call that returns to `grow()` would keep the range of those words across the call: on x86 that costs every growth by more than a word 2–3 more instructions with GCC and 5–8 with Clang. On master alone, that shape would take as many instructions within the capacity and 5 fewer past it. Instructions per growth, from the growth call to its return with memset and `realloc()` included, counted by single-stepping (x86-64, GCC 15 and Clang 21): a growth by more than a word, in `grow_memory_general()`, takes 2 fewer with both compilers, e.g. 65 instead of 67 with GCC and 61 instead of 63 with Clang for `mstore(0x40, 0x80)` on empty memory. It no longer loads the capacity and the data pointer for `realloc()` before the capacity check, and it falls through to the memset. A growth past the capacity takes 13 more: 219 instead of 206 with GCC and 215 instead of 202 with Clang. The one-word growths in `grow_memory()` are unchanged, and so is the rest of the library, but for the numbering of Clang's constant-pool labels. In callgrind, the evmone-bench cases take 0.007% fewer instructions in total with either compiler, from the general growths, and the EEST benchmark fixtures of the memory and copy instructions change by less than 0.001%. On rv64im with the SP1 guest flags, the growths within the capacity take the same instructions as before with GCC 15 and Clang 21, and a growth past the capacity takes 17–18 more.
chfast
force-pushed
the
evm/grow-memory-writes
branch
from
October 29, 2026 10:04
f8fc9f8 to
7c0c17f
Compare
MSTORE and the copies (CALLDATACOPY, CODECOPY, EXTCODECOPY and RETURNDATACOPY) overwrite the memory they check right after it grows. check_memory() takes how the instruction overwrites it as a template parameter, and the growth leaves the words overwritten whole unzeroed: MSTORE grows with grow_memory_for_store(), which leaves the new word unzeroed when the store starts at the memory end, and a copy growing the memory by more than a word goes to grow_memory_general_for_copy(), which zeroes only the gap up to the first word the copy covers whole, and the last word. Any other growth is zeroed as before. Memory::grow() takes the range of the words left unzeroed and sets the size before zeroing, within the capacity and past it. On 1000 mainnet blocks, 64.6% of the 1,025M bytes the growths zeroed were overwritten right after by the instruction that grew the memory. An MSTORE starting at the memory end makes 42% of the growths. The copies make 5% but zero 30% of the bytes, nearly all of them overwritten.
chfast
force-pushed
the
evm/grow-memory-writes
branch
from
October 29, 2026 10:44
7c0c17f to
fe8c553
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MSTORE and the copies (CALLDATACOPY, CODECOPY, EXTCODECOPY and RETURNDATACOPY) overwrite the memory they check right after it grows, so zeroing it first is wasted work.
check_memory()now takes how the instruction overwrites the memory as a template parameter. MSTORE grows withgrow_memory_for_store(), which leaves the new word unzeroed when the store starts at the memory end. A copy grows withgrow_memory_for_copy(). It zeroes a one-word growth whole and sends any other togrow_memory_general_for_copy(), which zeroes only the gap below the first word the copy covers whole, and the last word. Reads, MSTORE8, MCOPY (its source may lie in the new memory) and the calls zero the whole growth as before. This resolves the TODO ingrow_memory().On 1000 recent mainnet blocks, run through zilkworm's evmone fork, the memory grew 16.0M times, and 64.6% of the 1,025M bytes zeroed for it were overwritten right after by the instruction that grew it. An MSTORE starting exactly at the memory end makes 42% of the growths. An MSTORE straddling the end makes 28%, but it covers only part of the new word. The copies make 5% of the growths and 30% of the zeroed bytes, nearly all of them overwritten.
Memory::grow()takes the words to leave unzeroed, within the capacity and past it. It sets the size first and zeroes the last word before the gap, so that nothing is needed after the memset of the gap. Ingrow_memory_for_store(), a store straddling the memory end and one starting at it take separate paths, so that the straddling store still comparesnew_sizeonly once, asgrow_memory()does.grow_memory_for_copy()takesgas_leftlast: as the first argument, it made GCC allocate the registers of the out-of-linereturndatacopy()differently, with one more instruction on every execution.Instructions per growth, from the growth call to its return with memset included, counted by single-stepping (x86-64, GCC 15 and Clang 21):
mstore(0x40, 0x80)on empty memoryTwo kinds of growth take more instructions than on master. A copy within the last word of a growth by more than a word zeroes that word twice: a copy of 20 bytes at offset 1000 of empty memory takes 129 instead of 124 with GCC, and 122 instead of 120 with Clang. A growth by one word past the capacity takes 224–227 instructions instead of 219 with GCC and 217–218 instead of 215 with Clang,
realloc()included. That happens once per doubling of the memory buffer, and a copy of more than a word past the capacity takes fewer: 225 instead of 500 for 5000 bytes with GCC.On the paths that do not grow the memory, the handlers and the interpreter loops keep their instructions, except GCC's EXTCODECOPY, which gets 2–3 instructions shorter. GCC's three interpreter loops are unchanged, and Clang's differ only in the arguments of RETURNDATACOPY's growth call. In the Advanced interpreter, GCC's RETURNDATACOPY takes one more instruction on every execution, a register exchange at its return data bounds check, which no order of the growth call's arguments avoids. In callgrind, the evmone-bench cases take 0.22% (GCC) and 0.27% (Clang) fewer instructions in total, structarray_alloc 1.4% and 1.8% fewer. The EEST benchmark fixtures of the memory and copy instructions take 0.015% and 0.011% fewer.
In cycles, measured with perf on an AMD Zen 3 together with #1793 and #1796, against the master before them, the change stays within code-layout noise: with the interpreter loops at the same addresses in both builds, the main benchmarks change by -0.22% (GCC) and -0.13% (Clang) in geomean, while shifting the loops by a few hundred bytes alone changes single benchmarks by up to 2.4%, and one micro-benchmark by 7%.
On rv64im with the SP1 guest flags (GCC 15), an MSTORE at the memory end takes 17 instructions instead of 22, and a straddling one 22 as before. Growths by more than a word for the reads and stores take 39–40 instead of 44, plus the same memset. A copy at the memory end takes 41 instructions instead of 44 plus a memset of its size. A one-word copy takes 23 instead of 22, from
gas_leftgoing last. A copy within the last word of a longer growth takes 49 instead of 44, plus the same memset, and a one-word growth past the capacity 77–79 instead of 74, plusrealloc()and memset.