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

0 stars 0 forks source link

Gas Optimizations #22

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Use ++index instead of index++ to increment a loop counter

Context: MyStrategy.sol#L288-L343 (For both)

Description: Due to reduced stack operations, using ++index saves 5 gas per iteration.

Recommendation: Use ++indexto increment a loop counter.

Catching The Array Length Prior To Loop

Context: MyStrategy.sol#L288-L343 (For both)

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.

Function Ordering via Method ID

Context: MyStrategy.sol

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.

GalloDaSballo commented 2 years ago

Just a bot submission