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

Gas Optimizations #258

Open
code423n4 opened this issue Jul 19, 2022 · 0 comments
Open

Gas Optimizations #258

code423n4 opened this issue Jul 19, 2022 · 0 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

G-1 .LENGTH SHOULD NOT BE LOOKED UP IN EVERY LOOP OF A FOR-LOOP
The overheads outlined below are PER LOOP, excluding the first loop

storage arrays incur a sload(100 gas)
memory arrays use MLOAD (3 gas)
calldata arrays use CALLDATALOAD (3 gas)
Caching the length changes each of these to a DUP<N> (3 gas), and gets rid of the extra DUP<N> needed to store the stack offset
2 instances for this issue:
ERC1155Fuse.sol L#92
ERC1155Fuse.sol L#205

G-2 REQUIRE()/REVERT() STRINGS LONGER THAN 32 BYTES COST EXTRA GAS
Each extra chunk of byetes past the original 32 iincurs an MSTORE which costs 3 gas

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/ethregistrar/ETHRegistrarController.sol#:~:text=%3E%200)%20%7B-,require(,)%3B,-%7D

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/ethregistrar/ETHRegistrarController.sol#:~:text=(name%2C%20duration)%3B-,require(,)%3B,-_consumeCommitment(

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/ethregistrar/ETHRegistrarController.sol#:~:text=%3A36%5D)%3B-,require(,)%3B,-resolver.functionCall

G-3 USING PRIVATE RATHER THAN PUBLIC FOR CONSTANTS, SAVE GAS
If needed, the value can be read from the verified contract source code. Savings are due to the compiler not having to create non-payable getter functions for deployment calldata, and not adding another entry to the method ID table

There is 1 instance of this issue:

ETHRegistrarController.sol L#21

G-4 ABI.ENCODE() IS LESS EFFICIENT THAN ABI.ENCODEPACKED()
There is one issue of this:
ETHRegistrarController.sol L#106

G-5 USE CUSTOM ERRORS RATHER THAN REVERT()/REQUIRE() STRINGS TO SAVE GAS
Custom errors are available from solidity version 0.8.4. Custom errors save ~50 gas each time they’re hitby avoiding having to allocate and store the revert string. Not defining the strings also save deployment gas.
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/ethregistrar/ETHRegistrarController.sol#:~:text=%3E%200)%20%7B-,require(,)%3B,-%7D

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/ethregistrar/ETHRegistrarController.sol#:~:text=(name%2C%20duration)%3B-,require(,)%3B,-_consumeCommitment(

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/ethregistrar/ETHRegistrarController.sol#:~:text=%3A36%5D)%3B-,require(,)%3B,-resolver.functionCall

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jul 19, 2022
code423n4 added a commit that referenced this issue Jul 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

1 participant