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 #48

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

Gas Optimizations #48

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

Comments

@code423n4
Copy link
Contributor

USDC ENS

Author: Rolezn

=================
Gas Optimizations

(1) Constants can be set to uint8 for gas optimizations

uint16 constant DNSCLASS_IN =1;
uint16 constant DNSTYPE_DS =43;
uint16 constant DNSTYPE_DNSKEY =48;

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/DNSSECImpl.sol#L21-L24

(2) ++i costs less gas than i++, especially when it’s used in for-loops (--i/i-- too)

Saves gas per loop

Proof Of Concept

for(uint i = 0; i < len; i++) {

From https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#L266

for(uint i = 0; i < input.length; i++) {

From https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/DNSSECImpl.sol#L93

for (uint256 i = 0; i < data.length; i++) {

From https://github.com/code-423n4/2022-07-ens/blob/main/contracts/ethregistrar/ETHRegistrarController.sol#L256

(3) Using private rather than public for constants, saves 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

Proof Of Concept

uint256 public constant MIN_REGISTRATION_DURATION = 28 days;

From https://github.com/code-423n4/2022-07-ens/blob/main/contracts/ethregistrar/ETHRegistrarController.sol#L21

(4) It costs more gas to initialize variables to zero than to let the default of zero be applied

Proof Of Concept

for (uint256 i = 0; i < data.length; i++) {

From https://github.com/code-423n4/2022-07-ens/blob/main/contracts/ethregistrar/ETHRegistrarController.sol#L256

for (uint256 i = 0; i < accounts.length; ++i) {

From https://github.com/code-423n4/2022-07-ens/blob/main/contracts/wrapper/ERC1155Fuse.sol#L92

for (uint256 i = 0; i < ids.length; ++i) {

From https://github.com/code-423n4/2022-07-ens/blob/main/contracts/wrapper/ERC1155Fuse.sol#L92

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jul 17, 2022
code423n4 added a commit that referenced this issue Jul 17, 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