Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mask generation optimization #15870

Open
moh-eulith opened this issue Feb 16, 2025 · 2 comments
Open

Mask generation optimization #15870

moh-eulith opened this issue Feb 16, 2025 · 2 comments
Labels
feature nice to have We don’t see a good reason not to have it but won’t go out of our way to implement it. optimizer

Comments

@moh-eulith
Copy link

moh-eulith commented Feb 16, 2025

Abstract

I have a large contract compiled with --via-ir --optimize --optimize-runs 2
Looking at the generated opcodes I see the following mask generation patterns that can be optimized:

I've implemented a change to the constant optimizer for this here: develop...moh-eulith:solidity:mask_generation

May I create a PR from the above?

  1. Mask generation
    15 instances of PUSH1 0x1 PUSH1 0x1 PUSH1 0x[0-9A-F]* SHL SUB NOT
    (sub case of the next one)
    -> PUSH0 NOT PUSH1 $bits SHL (saves 4 bytes)
    247 instances of PUSH1 0x1 PUSH1 0x1 PUSH1 0x[0-9A-F]* SHL SUB, saves 3*247 bytes
    PUSH1 0x1 PUSH1 0x1 PUSH1 0x40 SHL SUB : 8 bytes, 3 + 3 + 3 + 3 + 3 = 15 gas
    -> PUSH0 NOT PUSH1 0xC0 SHR: 5 bytes, 2 + 3 + 3 + 3 = 11 gas

  2. Shifted mask generation
    53 instances of PUSH1 0x1 PUSH1 0x[0-9A-F]* SHL PUSH1 0x1 PUSH1 0x[0-9A-F]* SHL SUB: saves 3*53
    PUSH1 0x1 PUSH1 0x40 SHL PUSH1 0x1 PUSH1 0x80 SHL SUB: 11 opcodes
    -> PUSH0 NOT PUSH1 0xC0 SHR PUSH1 0x40 SHL: 8 opcodes

Motivation

Reduce both binary size and gas.

Specification

just an optimizer change, so no spec changes.

Backwards Compatibility

just an optimizer change, so backwards compatible.

@ekpyron
Copy link
Member

ekpyron commented Mar 12, 2025

Sorry again for the delay! We'd be happy to review a PR on this! We can also continue this in review, but be sure to note the coding style https://github.com/ethereum/solidity/blob/develop/CODING_STYLE.md (e.g. in the branch I see braces in single line if-statements that we usually skip); also the PR should get a Changelog entry (once #15927 is merged)

@moh-eulith
Copy link
Author

Thank you for taking a look. PR submitted with the feedback. #15935

@ekpyron ekpyron added nice to have We don’t see a good reason not to have it but won’t go out of our way to implement it. optimizer labels Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature nice to have We don’t see a good reason not to have it but won’t go out of our way to implement it. optimizer
Projects
None yet
Development

No branches or pull requests

2 participants