[Certora Audit] G-09. Cache array length outside of loop #896
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request includes several optimizations to the
OwnerManager
andERC165Handler
contracts by reducing the number of times array lengths are accessed within loops. These changes aim to improve the efficiency of the code by storing the array lengths in variables before the loops.If not cached, the solidity compiler will always read the length of the array during each iteration. That is, if it is a storage array, this is an extra sload operation (100 additional extra gas for each iteration except for the first) and if it is a memory array, this is an extra mload operation (3 additional gas for each iteration except for the first).
Optimizations in
OwnerManager
contract:contracts/base/OwnerManager.sol
: Stored_owners.length
in a variableownersLength
before the loop to avoid repeatedly accessing the array length.contracts/base/OwnerManager.sol
: UpdatedownerCount
to use theownersLength
variable instead of accessing_owners.length
again.Optimizations in
ERC165Handler
contract:contracts/handler/extensible/ERC165Handler.sol
: StoredhandlerWithSelectors.length
in a variablelen
before the loop inaddSupportedInterfaceBatch
to avoid repeatedly accessing the array length.contracts/handler/extensible/ERC165Handler.sol
: Storedselectors.length
in a variablelen
before the loop inremoveSupportedInterfaceBatch
to avoid repeatedly accessing the array length.