Loop could save gas if:
1) Cache condition value
2) Use prefix ++i increment
3) Put increment inside unchecked block
It would have next view:
uint256 length = myStateVarOrArrayLength;
for (uint256 i = 0; i < length;) {
// ...
unchecked {
++i;
}
}
Next loops could be optimized:
contracts/AxelarGateway.sol:195 for (uint256 i; i < adminCount; ++i) {
contracts/AxelarGateway.sol:207 for (uint256 i = 0; i < symbols.length; i++) {
contracts/AxelarGateway.sol:292 for (uint256 i; i < commandsLength; ++i) {
contracts/auth/AxelarAuthWeighted.sol:17 for (uint256 i; i < recentOperators.length; ++i) {
contracts/auth/AxelarAuthWeighted.sol:69 for (uint256 i = 0; i < weightsLength; ++i) {
contracts/auth/AxelarAuthWeighted.sol:98 for (uint256 i = 0; i < signatures.length; ++i) {
contracts/auth/AxelarAuthWeighted.sol:101 for (; operatorIndex < operatorsLength && signer != operators[operatorIndex]; ++operatorIndex) {}
contracts/auth/AxelarAuthWeighted.sol:116 for (uint256 i; i < accounts.length - 1; ++i) {
contracts/deposit-service/AxelarDepositService.sol:114 for (uint256 i; i < refundTokens.length; i++) {
contracts/deposit-service/AxelarDepositService.sol:168 for (uint256 i; i < refundTokens.length; i++) {
contracts/deposit-service/AxelarDepositService.sol:204 for (uint256 i; i < refundTokens.length; i++) {
contracts/gas-service/AxelarGasService.sol:123 for (uint256 i; i < tokens.length; i++) {
G01 -
unchecked
block can be used for gas efficiency of the expression that can't overflow/underflowG02 - custom errors declared but never used
Using custom errors istead of revert strings could save gas: https://blog.soliditylang.org/2021/04/21/custom-errors/
G03 - Optimised
for
loopsLoop could save gas if: 1) Cache condition value 2) Use prefix
++i
increment 3) Put increment insideunchecked
block It would have next view:Next loops could be optimized: