Skip to content

Outdated: Large constants via MLOAD.#16692

Closed
DanielVF wants to merge 1 commit into
argotorg:developfrom
DanielVF:danielvf-memorymasking-03
Closed

Outdated: Large constants via MLOAD.#16692
DanielVF wants to merge 1 commit into
argotorg:developfrom
DanielVF:danielvf-memorymasking-03

Conversation

@DanielVF

@DanielVF DanielVF commented May 8, 2026

Copy link
Copy Markdown
Contributor

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.

image

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)

image

(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.

000000000000FFFFFFFFFFFF
        |----------|
     |----------|

This makes the cost of most large constants, not just for masks be reduced to 4 bytes, 9 gas, with a PUSH1 MLOAD AND

This 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

  • No AI tools were used
  • AI tools were used (details below)

Code written with codex 5.5 on xhigh.

@DanielVF

Copy link
Copy Markdown
Contributor Author

Note that I'll have a much better draft PR up today.

@DanielVF

Copy link
Copy Markdown
Contributor Author

Closing this, will be posting a much cleaner one.

@DanielVF DanielVF closed this May 12, 2026
@DanielVF DanielVF changed the title Large constants via MLOAD. Outdated: Large constants via MLOAD. May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants