You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The code can be optimized by minimising the number of SLOADs. SLOADs are expensive (100 gas) compared to MLOADs/MSTOREs (3 gas). In the paragraphs below, please see the @audit-issue tags in the pieces of code's comments for more information about SLOADs that could be saved by caching the mentioned storage variables in memory variables.
@audit tags
The code is annotated at multiple places with //@audit comments to pinpoint the issues. Please, pay attention to them for more details.
Cache block.timestamp + MINIMUM_MIGRATION_DELAY in memory to save gas
It's possible to save 1 SLOAD by saving block.timestamp + MINIMUM_MIGRATION_DELAY in a memory variable and use this variable at L80 and L84 instead of emitting the storage variable at L84.
Cache (lastUpdated = block.timestamp) + vestingPeriod_ in memory to save gas
It's possible to save 1 SLOAD by saving (lastUpdated = block.timestamp) + vestingPeriod_ in a memory variable and use this variable at L90 and L92 instead of emitting the storage variable vestingPeriodFinish at L92.
Emit the memory variable issuanceRate_ instead of the storage variable issuanceRate L92
> 0 is less efficient than != 0 for unsigned integers (with proof)
!= 0 costs less gas compared to > 0 for unsigned integers in require statements with the optimizer enabled (6 gas)
Proof: While it may seem that > 0 is cheaper than !=, this is only true without the optimizer enabled and outside a require statement. If you enable the optimizer at 10k AND you're in a require statement, this will save gas. You can see this tweet for more proofs: https://twitter.com/gzeon/status/1485428085885640706
An array's length should be cached to save gas in for-loops
Reading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack.
Caching the array length in the stack saves around 3 gas per iteration.
Here, I suggest storing the array's length in a variable before the for-loop, and use it instead:
File: MapleLoanInternals.sol:258: for (uint256 i; i < calls_.length; ++i) {
Increments can be unchecked
In Solidity 0.8+, there's a default overflow check on unsigned integers. It's possible to uncheck this in for-loops and save some gas at each iteration, but at the cost of some code readability, as this uncheck cannot be made inline.
Gas Report
Table of Contents:
block.timestamp + MINIMUM_MIGRATION_DELAY
in memory to save gas(lastUpdated = block.timestamp) + vestingPeriod_
in memory to save gasissuanceRate_
instead of the storage variableissuanceRate
L92issuanceRate
vestingPeriodFinish
> 0
is less efficient than!= 0
for unsigned integers (with proof)Foreword
@audit
tagsFile: xMPL.sol
function scheduleMigration()
Cache
block.timestamp + MINIMUM_MIGRATION_DELAY
in memory to save gasIt's possible to save 1 SLOAD by saving
block.timestamp + MINIMUM_MIGRATION_DELAY
in a memory variable and use this variable at L80 and L84 instead of emitting the storage variable at L84.File: RevenueDistributionToken.sol
function updateVestingSchedule()
Cache
(lastUpdated = block.timestamp) + vestingPeriod_
in memory to save gasIt's possible to save 1 SLOAD by saving
(lastUpdated = block.timestamp) + vestingPeriod_
in a memory variable and use this variable at L90 and L92 instead of emitting the storage variablevestingPeriodFinish
at L92.Emit the memory variable
issuanceRate_
instead of the storage variableissuanceRate
L92This can save 1 SLOAD
function totalAssets()
Cache
issuanceRate
Caching this in memory can save around 1 SLOAD
Cache
vestingPeriodFinish
Caching this in memory can save around 1 SLOAD
General recommendations
Comparisons
> 0
is less efficient than!= 0
for unsigned integers (with proof)!= 0
costs less gas compared to> 0
for unsigned integers inrequire
statements with the optimizer enabled (6 gas)Proof: While it may seem that
> 0
is cheaper than!=
, this is only true without the optimizer enabled and outside a require statement. If you enable the optimizer at 10k AND you're in arequire
statement, this will save gas. You can see this tweet for more proofs: https://twitter.com/gzeon/status/1485428085885640706I suggest changing
> 0
with!= 0
here:Also, please enable the Optimizer.
For-Loops
An array's length should be cached to save gas in for-loops
Reading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack.
Caching the array length in the stack saves around 3 gas per iteration.
Here, I suggest storing the array's length in a variable before the for-loop, and use it instead:
Increments can be unchecked
In Solidity 0.8+, there's a default overflow check on unsigned integers. It's possible to uncheck this in for-loops and save some gas at each iteration, but at the cost of some code readability, as this uncheck cannot be made inline.
ethereum/solidity#10695
Instances include:
The code would go from:
to:
The risk of overflow is inexistant for a
uint256
here.The text was updated successfully, but these errors were encountered: