code-423n4 / 2022-06-nested-findings

0 stars 1 forks source link

Gas Optimizations #10

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

The Increment In For Loop Post Condition Can Be Made Unchecked

Context: NestedFactory.sol#L121-L130, NestedFactory.sol#L133-L149, NestedFactory.sol#L184-L202, NestedFactory.sol#L243-L275, NestedFactory.sol#L310-L323, NestedFactory.sol#L328-L344, NestedFactory.sol#L353-L391, NestedFactory.sol#L400-L449, NestedFactory.sol#L649-L657, OperatorResolver.sol#L32-L49, OperatorResolver.sol#L52-L70, OperatorResolver.sol#L74-L78, MixinOperatorResolver.sol#L32-L48, MixinOperatorResolver.sol#L51-L70, TimelockControllerEmergency.sol#L65-L95 (For both), TimelockControllerEmergency.sol#L221-L237, TimelockControllerEmergency.sol#L312-L328, OperatorScripts.sol#L52-L86 (For both), BeefyVaultOperator.sol#L13-L23, BeefyZapBiswapLPVaultOperator.sol#L21-L32, BeefyZapUniswapLPVaultOperator.sol#L21-L32, YearnCurveVaultOperator.sol#L31-L51, CurveHelpers.sol#L79-L93

Description: (This is only relevant if you are using the default solidity checked arithmetic). i++ involves checked arithmetic, which is not required. This is because the value of i is always strictly less than length <= 2256 - 1. Therefore, the theoretical maximum value of i to enter the for-loop body is `2256 - 2. This means that thei++` in the for loop can never overflow. Regardless, the overflow checks are performed by the compiler.

Unfortunately, the Solidity optimizer is not smart enough to detect this and remove the checks. One can manually do this by:

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;
    }
}

Note that it’s important that the call to unchecked_inc is inlined. This is only possible for solidity versions starting from 0.8.2.

Recommendation: The increment in the for loop post condition can be made unchecked.

Catching The Array Length Prior To Loop

Context: NestedFactory.sol#L121-L130, NestedFactory.sol#L649-L657, OperatorResolver.sol#L52-L70, OperatorResolver.sol#L74-L78, MixinOperatorResolver.sol#L32-L48, MixinOperatorResolver.sol#L51-L70, TimelockControllerEmergency.sol#L65-L95 (For both), TimelockControllerEmergency.sol#L221-L237, TimelockControllerEmergency.sol#L312-L328

Description: One can save gas by caching the array length (in stack) and using that set variable in the loop. Replace state variable reads and writes within loops with local variable reads and writes. This is done by assigning state variable values to new local variables, reading and/or writing the local variables in a loop, then after the loop assigning any changed local variables to their equivalent state variables.

Recommendation: Simply do something like so before the for loop: uint length = variable.length. Then add length in place of variable.length in the for loop.

Setting The Constructor To Payable

Context: All Contracts

Description: You can cut out 10 opcodes in the creation-time EVM bytecode if you declare a constructor payable. Making the constructor payable eliminates the need for an initial check of msg.value == 0 and saves 21 gas on deployment with no security risks.

Recommendation: Set the constructor to payable.

Function Ordering via Method ID

Context: All Contracts

Description: Contracts most called functions could simply save gas by function ordering via Method ID. Calling a function at runtime will be cheaper if the function is positioned earlier in the order (has a relatively lower Method ID) because 22 gas are added to the cost of a function for every position that came before it. The caller can save on gas if you prioritize most called functions. One could use This tool to help find alternative function names with lower Method IDs while keeping the original name intact.

Recommendation: Find a lower method ID name for the most called functions for example mostCalled() vs. mostCalled_41q() is cheaper by 44 gas.

maximebrugel commented 2 years ago

Clone of #9

JeeberC4 commented 2 years ago

Appears to be a double submit by the warden. Keeping #9, invalidating this one.