code-423n4 / 2022-06-notional-coop-findings

1 stars 1 forks source link

Gas Optimizations #173

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

1. Avoiding initialization of loop index can save a little gas

POC

Examples of this issue in the codebase: https://github.com/code-423n4/2022-06-notional-coop/blob/cdf34d775d8e1deb6805968ff1b7fec93cab5387/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L618

https://github.com/code-423n4/2022-06-notional-coop/blob/cdf34d775d8e1deb6805968ff1b7fec93cab5387/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L238

impact

The local variable used for the loop index need not be initialized to 0 because the default value is 0. Avoiding this anti-pattern can save a few opcodes and therefore a tiny bit of gas.


2. Use != 0 instead of > 0

POC

Examples of this issue in the codebase:

https://github.com/code-423n4/2022-06-notional-coop/blob/cdf34d775d8e1deb6805968ff1b7fec93cab5387/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L619

https://github.com/code-423n4/2022-06-notional-coop/blob/cdf34d775d8e1deb6805968ff1b7fec93cab5387/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L607

impact

Using != 0 is slightly cheaper than > 0.


3. Using Prefix (++i) rather than postfix (i++) in increment/decrement operators in for-loops

POC

Examples of this issue in the codebase:

https://github.com/code-423n4/2022-06-notional-coop/blob/cdf34d775d8e1deb6805968ff1b7fec93cab5387/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L605

https://github.com/code-423n4/2022-06-notional-coop/blob/cdf34d775d8e1deb6805968ff1b7fec93cab5387/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L238

impact

using the prefix increment/decrement operators (++i/--i) cost less gas PER LOOP than the postfix increment/decrement operators (i++/i--)