Unnecessary SLOAD
s and MLOAD
s in for-each loops
#36
Labels
bug
Warden finding
G (Gas Optimization)
sponsor confirmed
Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Handle
pants
Vulnerability details
There are many for loops that follows this for-each pattern:
In such for loops, the
array.length
is read on every iteration, instead of caching it once in a local variable and read it from there.Impact
Storage reads are much more expensive than reading local variables. Memory reads are a bit more expensive than reading local variables.
Tool Used
Manual code review.
Recommended Mitigation Steps
Read these values from storage / memory once, cache them in local variables and then read them again from the local variables. For example:
The text was updated successfully, but these errors were encountered: