Gas Optimizations #251
Labels
bug
Something isn't working
duplicate
This issue or pull request already exists
G (Gas Optimization)
++i
is cheaper thani++
Use prefix increment instead of postfix increment.
Unnecessary checked arithmetic in for loops
The
i++
part of for loops can be surrounded by theunchecked
keyword to save gas. For example:can be optimized to:
!= 0
is cheaper than> 0
(unsigned integers)When dealing with unsigned integers the
!= 0
comparison is equivalent to> 0
comparison. It is also cheaper, therefore you should use it instead.Cache Array Length Outside of Loop
You can optimize for loops that iterate over array elements by caching the array length in a local variable. This will reduce the number of MLOADs and will save gas. For example:
can be optimized to:
Repeated computation of the same value in
AuraLocker._processExpiredLocks()
That function computes
length - 1
multiple times. The result can be computed once and be cached in a local variable in order to save gas.Repeated access to the current array element in for loops
Many for loops that iterate over array elements access the current element (
array[i]
) more than once in each iteration. This element can be accessed only once (and be cached in a local variable) in order to avoid the additional array-boundaries check that Solidity performs with each array access, and save gas.The text was updated successfully, but these errors were encountered: