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

[optimisation] quick win - byte size optimised address mask #15762

Open
radeksvarz opened this issue Jan 23, 2025 · 1 comment
Open

[optimisation] quick win - byte size optimised address mask #15762

radeksvarz opened this issue Jan 23, 2025 · 1 comment
Labels

Comments

@radeksvarz
Copy link

Abstract and Motivation

Solidity uses bitmask (0xffffffffffffffffffffffffffffffffffffffff) to sanity clean address value.

It compiles that into this code:

PUSH20 0xffffffffffffffffffffffffffffffffffffffff

This requires 21 bytes and 3 gas for each situation address is used. (70+ times in some example ERC20 bytecode).

I propose much shorter approach for the case the optimiser is set to optimise bytecode length.

Solution

Compile to this instead (in the case the optimiser is set to optimise bytecode length):

PUSH0
NOT
PUSH1 12
SHR

This results into 5 bytes code and 11 gas.

In some common ERC20 bytecode it has impact on being 1100+ bytes shorter. Wrt to functions - there can be just +40 gas needed in the case the address is sanity cleaned 5 times within 1 call.

Backwards Compatibility

The optimised bytecode is shorter (-16 bytes), but has +8 gas. Functionally the same.

@krishpranav
Copy link

PR: #15764

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants