Outdated: Large constants via MLOAD.#16692
Closed
DanielVF wants to merge 1 commit into
Closed
Conversation
Contributor
Author
|
Note that I'll have a much better draft PR up today. |
Contributor
Author
|
Closing this, will be posting a much cleaner one. |
9 tasks
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 PR makes an approximately 5% bytecode size reduction for contracts compiled with the normal defaults of optimizer + runs 200 vs without this PR.
There are also gas savings as well. (note that the total gas used in these transactions includes other contracts, these are real mainnet contracts, with just one selected contract swapped out)
(Some benchmarking results available here: https://danielvf.github.io/purplebench/site/compilers/solc-0.8.36-memorymasking-03.html)
Bitmasking for stack cleanup currently makes up a double digit portion of solidity code (see https://ethereum-magicians.org/t/do-we-need-a-bitmasking-opcode-23-3-of-aave-v4-core-bytecode-is-bitmasking/28323). The current methods used by solidity either use up a lot of bytecode for raw push bytes, or use 9 bytes, 18 gas for computing a large constant.
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.
This makes the cost of most large constants, not just for masks be reduced to 4 bytes, 9 gas, with a
PUSH1 MLOAD ANDThis PR is yolo code intended to demonstrate that the concept works. It is not intended for merging as is.
Setting optimizer runs to a high value forces using raw large constants and this no longer provides benefits.
Checklist
AI Disclosure
Code written with codex 5.5 on xhigh.