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

Open
code423n4 opened this issue Aug 15, 2022 · 0 comments
Open

Gas Optimizations #238

code423n4 opened this issue Aug 15, 2022 · 0 comments

Comments

@code423n4
Copy link
Contributor

State variables should not be initialized to their default values

Initializing uint variables to their default value of 0 is unnecessary and costs gas


VotingEscrow.sol: L298

        uint256 blockSlope = 0; // dblock/dt

Change to:

        uint256 blockSlope; // dblock/dt

Similarly for the additional instances below


min is initialized to zero in both the following lines:

VotingEscrow.sol: L714

VotingEscrow.sol: L737

        uint256 min = 0;

VotingEscrow.sol: L793

        uint256 dBlock = 0;

dTime is initialized to zero in both the following lines:

VotingEscrow.sol: L794

VotingEscrow.sol: L889

        uint256 dTime = 0;


Use ++i instead of i++ to increase count in a for loop

Since use of i++ (or equivalent counter) costs more gas, it is better to use ++i in the four for loops referenced below:

VotingEscrow.sol: L309-347

VotingEscrow.sol: L717-725

VotingEscrow.sol: L739-748

VotingEscrow.sol: L834-855



Avoid use of default "checked" behavior in a for loop

Underflow/overflow checks are made every time ++i (or i++ or equivalent counter) is called. Such checks are unnecessary since i is already limited. Therefore, use unchecked {++i}/unchecked {i++} instead


VotingEscrow.sol: L717-725

        for (uint256 i = 0; i < 128; i++) {
            if (min >= max) break;
            uint256 mid = (min + max + 1) / 2;
            if (pointHistory[mid].blk <= _block) {
                min = mid;
            } else {
                max = mid - 1;
            }
        }

Suggestion:

        for (uint256 i = 0; i < 128;) {
            if (min >= max) break;
            uint256 mid = (min + max + 1) / 2;
            if (pointHistory[mid].blk <= _block) {
                min = mid;
            } else {
                max = mid - 1;
            }

            unchecked {
              ++i;
          }
        }

Similarly for the three for loops referenced below:

VotingEscrow.sol: L309-347

VotingEscrow.sol: L739-748

VotingEscrow.sol: L834-855



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

No branches or pull requests

1 participant