After this optimization, client-tests still pass, with gas savings of 0.217%. Because of being a compiling process optimization, it has been applied to all contracts in scope.
via_ir = true
for an Enhanced Compiler ProcessThe via_ir
flag in the foundry.toml
file triggers a more optimized compiler. This change resulted in a 0.217% improvement over the unoptimized contract.
foundry.toml
Configuration:
đ§ Note â ī¸: Enabling
via_ir
makes the compilation process slower. Compilation-related tasks are expected to take quite a bit more of time when this flag is active.
đ Information âšī¸: The
via_ir
flag instructs Foundry to utilize Intermediate Representation (IR). IR is a low-level language bridging high-level source code and machine code. This intermediate step allows for additional optimization techniques. That's what the_ir
means invia_ir
, Intermediate Representation..
Metrics derived from gas consumption of the provided tests:
Optimization Method | Optimized Gas | Original Gas | Gas Saved | % Saved |
---|---|---|---|---|
Use of via_ir | 15,039,663 | 15,072,846 | 33,183 | 0.217% |
Metrics were taken by running the forge snapshot
command and compared with bash scripts tailored to analyze the results of forge snapshots.
đ§ Notice â ī¸: The optimization affects all the contract, so I've analyzed all the tests but invariant ones because
forge snapshot
does not provide gas consumtion metrics for those.
đ§ Notice â ī¸: Negative numbers in the
Gas Saved
column means the "optimization" increased the consumption of gas in that test. But the overall outcome in this case is positive so it can be considered an optimization.
Test Name | Optimized Gas | Original Gas | Gas Saved |
---|---|---|---|
DSCEngineTest:testCanMintWithDepositedCollateral | 202,684 | 203,164 | 480 |
DSCEngineTest:testHealthFactorCanGoBelowOne | 287,373 | 290,012 | 2,639 |
DSCEngineTest:testGetCollateralBalanceOfUser | 83,913 | 83,444 | -469 |
DSCEngineTest:testGetUsdValue | 26,044 | 26,254 | 210 |
DSCEngineTest:testGetAccountCollateralValue | 126,014 | 127,827 | 1,813 |
DSCEngineTest:testRevertsWithUnapprovedCollateral | 708,487 | 707,851 | -636 |
DSCEngineTest:testLiquidatorTakesOnUsersDebt | 474,882 | 483,522 | 8,640 |
DSCEngineTest:testUserStillHasSomeEthAfterLiquidation | 488,630 | 498,431 | 9,801 |
DSCEngineTest:testCantLiquidateGoodHealthFactor | 349,925 | 354,163 | 4,238 |
DSCEngineTest:testMustRedeemMoreThanZero | 234,352 | 236,959 | 2,607 |
DSCEngineTest:testRevertsIfTransferFails | 1,946,641 | 1,930,291 | -16,350 |
DSCEngineTest:testCantBurnMoreThanUserHas | 14,399 | 13,312 | -1,087 |
DSCEngineTest:testRevertsIfTokenLengthDoesntMatchPriceFeeds | 181,848 | 180,675 | -1,173 |
DSCEngineTest:testMustImproveHealthFactorOnLiquidation | 2,364,258 | 2,363,648 | -610 |
DSCEngineTest:testGetLiquidationThreshold | 5,880 | 5,616 | -264 |
DSCEngineTest:testGetCollateralTokenPriceFeed | 12,251 | 12,268 | 17 |
DSCEngineTest:testCanRedeemDepositedCollateral | 248,461 | 251,655 | 3,194 |
DSCEngineTest:testRevertsIfMintAmountIsZero | 199,612 | 201,106 | 1,494 |
DSCEngineTest:testUserHasNoMoreDebt | 475,044 | 483,424 | 8,380 |
DSCEngineTest:testCanRedeemCollateral | 127,418 | 127,928 | 510 |
DSCEngineTest:testProperlyReportsHealthFactor | 207,663 | 210,229 | 2,566 |
DSCEngineTest:testGetTokenAmountFromUsd | 26,493 | 26,170 | -323 |
DSCEngineTest:testCanDepositedCollateralAndGetAccountInfo | 128,518 | 130,228 | 1,710 |
DSCEngineTest:testRevertsIfTransferFromFails | 1,967,717 | 1,955,875 | -11,842 |
DecentralizedStablecoinTest:testCantMintToZeroAddress | 12,893 | 12,443 | -450 |
DSCEngineTest:testPriceRevertsOnStaleCheck | 21,836 | 21,767 | -69 |
DSCEngineTest:testGetDsc | 9,041 | 7,794 | -1,247 |
DSCEngineTest:testCanBurnDsc | 234,462 | 237,295 | 2,833 |
DSCEngineTest:testCanMintDsc | 204,032 | 204,291 | 259 |
DSCEngineTest:testPriceRevertsOnBadAnsweredInRound | 24,279 | 24,369 | 90 |
DSCEngineTest:testRevertsIfMintedDscBreaksHealthFactor | 163,893 | 165,932 | 2,039 |
DSCEngineTest:testRevertsIfMintFails | 2,026,539 | 2,032,604 | 6,065 |
DSCEngineTest:testRevertsIfBurnAmountIsZero | 200,131 | 201,181 | 1,050 |
DecentralizedStablecoinTest:testMustBurnMoreThanZero | 60,193 | 59,972 | -221 |
DecentralizedStablecoinTest:testMustMintMoreThanZero | 12,380 | 12,062 | -318 |
DSCEngineTest:testGetMinHealthFactor | 6,752 | 5,547 | -1,205 |
DecentralizedStablecoinTest:testCantBurnMoreThanYouHave | 60,278 | 59,986 | -292 |
DSCEngineTest:testRevertsIfCollateralZero | 44,899 | 43,623 | -1,276 |
DSCEngineTest:testCanDepositCollateralWithoutMinting | 89,157 | 88,270 | -887 |
DSCEngineTest:testLiquidationPayoutIsCorrect | 476,403 | 484,401 | 7,998 |
DSCEngineTest:testGetAccountCollateralValueFromInformation | 128,558 | 130,226 | 1,668 |
DSCEngineTest:testGetTimeout | 5,605 | 5,532 | -73 |
DSCEngineTest:testGetCollateralTokens | 16,802 | 15,603 | -1,199 |
TOTAL | 15,052,043 | 15,084,908 | 32,865 |
Total saved percentage => 0.217%.
đ Notice âšī¸: The percentage has been calculated with these numbers from the TOTAL:
( 32,865 / 15,084,908) * 100
They mean:
(totalGasSaved / originalGasCost) * 100
Original
Optimized
Manual audit
Forge Snapshot
Bash scripts tailored to analyze forge snapshots.
đ Notice âšī¸: I've personally created the bash scripts. Here is a link to the github repo Forge-Snapshots-Analyzer.
Implementing this gas optimization is recommended. This benefits both end-users and the protocol in terms of cost, while ensuring protocol security remains uncompromised as tests are passing.
đ§ Notice â ī¸: If a significant vulnerability necessitating major code changes arises, this gas optimization technique remains relevant and applicable. Nonetheless some gas analysis as the one used here is always needed to make sure it's actually saving gas.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.