code-423n4 / 2022-04-phuture-findings

0 stars 0 forks source link

Gas Optimizations #10

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

# Gas Report

Variables do not need to be initialized with 'empty' values such as 0, false, or address(0)

Uninitialized variables by default contain a value equivalent to 0: uints are initialized to 0; bools to false; addresses to address(0).

Explicitly assigning these values to variables when they are declared increases gas costs while providing no funciton.

e.g. change this code:

uint256 var = 0;

to

uint256 var;

For more information, please consult the following resources:

Tips and Tricks to Save Gas and Reduce Bytecode Size

The following lines of code are affected:

code4rena/2022-04-phuture/contracts/UniswapV2PathPriceOracle.sol:34:        for (uint i = 0; i < path.length - 1; i++) {
code4rena/2022-04-phuture/contracts/UniswapV2PathPriceOracle.sol:49:        for (uint i = 0; i < path.length - 1; i++) {

Unchecked increment can be used in for-loop

Newer versions of the Solidity compiler will check for integer overflows and underflows automatically. This provides safety but increases gas costs.

When an unsigned integer is guaranteed to never overflow, the unchecked feature of Solidity can be used to save gas costs.

A common case for this is for-loops using a strictly-less-than comparision in their conditional statement, e.g.:

uint256 length = someArray.length;
for (uint256 i; i < length; ++i) {
}

In cases like this, the maximum value for length is 2**256 - 1. Therefore, the maximum value of i is 2**256 - 2 as it will always be strictly less than length.

This example can be replaced with the following construction to reduce gas costs:

for (uint i = 0; i < length; i = unchecked_inc(i)) {
    // do something that doesn't change the value of i
}

function unchecked_inc(uint i) returns (uint) {
    unchecked {
        return i + 1;
    }
}

For more information, consult the following resources:

Solidity gas optimizations

Solidity docs: underflows, overflows, and unchecked

The following lines of code are affected:

code4rena/2022-04-phuture/contracts/BaseIndex.sol:78:        for (uint i; i < _assets.length; ++i) {
code4rena/2022-04-phuture/contracts/IndexLogic.sol:102:        for (uint i; i < length; ++i) {
code4rena/2022-04-phuture/contracts/IndexLogic.sol:39:        for (uint i; i < assets.length(); ++i) {
code4rena/2022-04-phuture/contracts/IndexLogic.sol:60:        for (uint i; i < inactiveAssets.length(); ++i) {
code4rena/2022-04-phuture/contracts/ManagedIndex.sol:30:        for (uint i; i < _assets.length; ++i) {
code4rena/2022-04-phuture/contracts/ManagedIndexReweightingLogic.sol:38:        for (uint i; i < assets.length(); ++i) {
code4rena/2022-04-phuture/contracts/ManagedIndexReweightingLogic.sol:50:        for (uint i; i < _updatedAssets.length; ++i) {
code4rena/2022-04-phuture/contracts/ManagedIndexReweightingLogic.sol:96:        for (uint i; i < _inactiveAssets.length; ++i) {
code4rena/2022-04-phuture/contracts/TopNMarketCapIndex.sol:48:        for (uint i; i < _assets.length; ++i) {
code4rena/2022-04-phuture/contracts/TopNMarketCapReweightingLogic.sol:104:        for (uint i; i < _inactiveAssets.length; ++i) {
code4rena/2022-04-phuture/contracts/TopNMarketCapReweightingLogic.sol:37:        for (uint i; i < assets.length(); ++i) {
code4rena/2022-04-phuture/contracts/TrackedIndex.sol:35:        for (uint i; i < _assets.length; ++i) {
code4rena/2022-04-phuture/contracts/TrackedIndexReweightingLogic.sol:37:        for (uint i; i < assets.length(); ++i) {
code4rena/2022-04-phuture/contracts/TrackedIndexReweightingLogic.sol:66:        for (uint i; i < assets.length(); ++i) {
code4rena/2022-04-phuture/contracts/UniswapV2PathPriceOracle.sol:34:        for (uint i = 0; i < path.length - 1; i++) {
code4rena/2022-04-phuture/contracts/UniswapV2PathPriceOracle.sol:49:        for (uint i = 0; i < path.length - 1; i++) {

Replace postfix increment (var++) with prefix increment (++var)

Using ++i costs less gas than using i++. In the context of a for-loop, gas is saved on each iteration.

The following lines of code are affected:

code4rena/2022-04-phuture/contracts/UniswapV2PathPriceOracle.sol:34:        for (uint i = 0; i < path.length - 1; i++) {
code4rena/2022-04-phuture/contracts/UniswapV2PathPriceOracle.sol:49:        for (uint i = 0; i < path.length - 1; i++) {

Replace strict greater-than-zero operation (> 0) with does-not-equal-zero (!= 0) operation

When checking whether a value is equal to zero, using the construction var != 0 is costs less gas than using var > 0. Note that this is true only when the comparison occurs in a conditional context and the Solidity compiler is using the Optimizer.

For more information, please consult the following resources:

Twitter discussion detailing the gas costs of != 0 vs > 0 in require() calls

Solidity Compiler: Optimizer options

The following lines of code are affected:

code4rena/2022-04-phuture/contracts/IndexLogic.sol:76:            require(lastAssetBalanceInBase > 0, "Index: INSUFFICIENT_AMOUNT");
code4rena/2022-04-phuture/contracts/IndexLogic.sol:98:        require(value > 0, "Index: INSUFFICIENT_AMOUNT");

Array length can be cached

In the context of a for-loop that iterates over an array, it costs less gas to cache the array's length in a variable and read from this variable rather than use the arrays .length property. Reading the .length property for on the array will cause a recalculation of the array's length on each iteration of the loop which is a more expensive operation than reading from a stack variable.

For example, the following code:

for (uint i; i < arr.length; ++i) {
    // ...
}

should be changed to:

uint length = arr.length;
for (uint i; i < length; ++i) {
    // ...
}

Note that in the second case, the length of the array must not change during the loop's execution.

For more information, see the following resource:

Solidity gas optimizations

The following lines of code are affected:

code4rena/2022-04-phuture/contracts/BaseIndex.sol:78:        for (uint i; i < _assets.length; ++i) {
code4rena/2022-04-phuture/contracts/ManagedIndex.sol:30:        for (uint i; i < _assets.length; ++i) {
code4rena/2022-04-phuture/contracts/ManagedIndexReweightingLogic.sol:50:        for (uint i; i < _updatedAssets.length; ++i) {
code4rena/2022-04-phuture/contracts/ManagedIndexReweightingLogic.sol:96:        for (uint i; i < _inactiveAssets.length; ++i) {
code4rena/2022-04-phuture/contracts/TopNMarketCapIndex.sol:48:        for (uint i; i < _assets.length; ++i) {
code4rena/2022-04-phuture/contracts/TopNMarketCapIndex.sol:49:        uint _i = _assets.length - 1 - i;
code4rena/2022-04-phuture/contracts/TopNMarketCapReweightingLogic.sol:104:        for (uint i; i < _inactiveAssets.length; ++i) {
code4rena/2022-04-phuture/contracts/TrackedIndex.sol:35:        for (uint i; i < _assets.length; ++i) {
jn-lp commented 2 years ago

The first and the second tips were useful, thanks