Using memory loads for large constant creation#16708
Closed
DanielVF wants to merge 1 commit into
Closed
Conversation
Contributor
Author
This was referenced May 13, 2026
Contributor
Author
|
Closing this pull request, in favor of #16729. While this one provides a bit more speed up and size reduction, it does change the "contract" about what users are allowed to do with memory from assembly, which would be a breaking change. |
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.
Description
This is draft PR for discussion. It has been benchmarked against live transactions, but tests are still lacking.
This PR makes an approximately 5% bytecode size reduction for contracts compiled with the normal defaults of optimizer + runs 200. Gas usage is also reduced on average.
Bitmasking for stack cleanup currently makes up a double digit portion of solidity code. The current methods used by solidity either use large amounts of bytecode for raw push bytes, or use a 9 bytes, 18 gas sequence for in place computing for computing a large constant. This PR the cost of most large constants, not just for masks, reduce to only 4 bytes, 9 gas, with a
PUSH1 MLOAD AND.By adding a 32 bytes of 0xFF after the zero constant memory slot, we can create any byte aligned, right aligned constant by just selecting a memory region that contains the right amounts of 0x00's and 0xFF's.
Tradeoffs and Decisions
The goal of this PR is to have an only positive effect on code bytecode size and gas usage.
I've chosen not to enable this optimization for compiling constructor bytecode. It's a single line change to use it, but contracts without constructor code would be penalized, and that makes up a lot of contract deploys.
This is also turned off when compiling raw yul outside of solidity, since the memory may not have been setup.
This is currently assuming that well behave user assembly code will NOT write into memory that the solidity compiler has marked for itself by advancing the free memory pointer past. This is the core correctness assumption of this code.
Remaining Work
Checklist
AI Disclosure
Codex 5.5