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 following sections detail the gas optimizations found throughout the codebase. Each optimization is documented with the setup, an explainer for the optimization, a gas report and line identifiers for each optimization across the codebase. For each section's gas report, the optimizer was turned on and set to 10000 runs. You can replicate any tests/gas reports by heading to 0xKitsune/gas-lab and cloning the repo. Then, simply copy/paste the contract examples from any section and run forge test --gas-report. You can also easily update the optimizer runs in the foundry.toml.
Use custom errors instead of string error messages
unchecked{++i} instead of i++ (or use assembly when applicable)
Use ++i instead of i++. This is especially useful in for loops but this optimization can be used anywhere in your code. You can also use unchecked{++i;} for even more gas savings but this will not check to see if i overflows. For extra safety if you are worried about this, you can add a require statement after the loop checking if i is equal to the final incremented value. For best gas savings, use inline assembly, however this limits the functionality you can achieve. For example you cant use Solidity syntax to internally call your own contract within an assembly block and external calls must be done with the call() or delegatecall() instruction. However when applicable, inline assembly will save much more gas.
contractGasTestisDSTest{Contract0c0;Contract1c1;Contract2c2;Contract3c3;Contract4c4;functionsetUp()public{c0=newContract0();c1=newContract1();c2=newContract2();c3=newContract3();c4=newContract4();}functiontestGas()public{c0.iPlusPlus();c1.plusPlusI();c2.uncheckedPlusPlusI();c3.safeUncheckedPlusPlusI();c4.inlineAssemblyLoop();}}contractContract0{//loop with i++functioniPlusPlus()publicpure{uint256j=0;for(uint256i;i<10;i++){j++;}}}contractContract1{//loop with ++ifunctionplusPlusI()publicpure{uint256j=0;for(uint256i;i<10;++i){j++;}}}contractContract2{//loop with unchecked{++i}functionuncheckedPlusPlusI()publicpure{uint256j=0;for(uint256i;i<10;){j++;unchecked{++i;}}}}contractContract3{//loop with unchecked{++i} with additional overflow checkfunctionsafeUncheckedPlusPlusI()publicpure{uint256j=0;uint256i=0;for(i;i<10;){j++;unchecked{++i;}}//check for overflowassembly{iflt(i,10){mstore(0x00,"loop overflow")revert(0x00,0x20)}}}}contractContract4{//loop with inline assemblyfunctioninlineAssemblyLoop()publicpure{assembly{letj :=0for{leti :=0}lt(i,10){i :=add(i,0x01)}{j :=add(j,0x01)}}}}
Use assembly for math instead of Solidity. You can check for overflow/underflow in assembly to ensure safety. If using Solidity versions < 0.8.0 and you are using Safemath, you can gain significant gas savings by using assembly to calculate values and checking for overflow/underflow.
contractGasTestisDSTest{Contract0c0;Contract1c1;Contract2c2;Contract3c3;Contract4c4;Contract5c5;Contract6c6;Contract7c7;functionsetUp()public{c0=newContract0();c1=newContract1();c2=newContract2();c3=newContract3();c4=newContract4();c5=newContract5();c6=newContract6();c7=newContract7();}functiontestGas()public{c0.addTest(34598345,100);c1.addAssemblyTest(34598345,100);c2.subTest(34598345,100);c3.subAssemblyTest(34598345,100);c4.mulTest(34598345,100);c5.mulAssemblyTest(34598345,100);c6.divTest(34598345,100);c7.divAssemblyTest(34598345,100);}}contractContract0{//addition in SolidityfunctionaddTest(uint256a,uint256b)publicpure{uint256c=a+b;}}contractContract1{//addition in assemblyfunctionaddAssemblyTest(uint256a,uint256b)publicpure{assembly{letc :=add(a,b)iflt(c,a){mstore(0x00,"overflow")revert(0x00,0x20)}}}}contractContract2{//subtraction in SolidityfunctionsubTest(uint256a,uint256b)publicpure{uint256c=a-b;}}contractContract3{//subtraction in assemblyfunctionsubAssemblyTest(uint256a,uint256b)publicpure{assembly{letc :=sub(a,b)ifgt(c,a){mstore(0x00,"underflow")revert(0x00,0x20)}}}}contractContract4{//multiplication in SolidityfunctionmulTest(uint256a,uint256b)publicpure{uint256c=a*b;}}contractContract5{//multiplication in assemblyfunctionmulAssemblyTest(uint256a,uint256b)publicpure{assembly{letc :=mul(a,b)iflt(c,a){mstore(0x00,"overflow")revert(0x00,0x20)}}}}contractContract6{//division in SolidityfunctiondivTest(uint256a,uint256b)publicpure{uint256c=a*b;}}contractContract7{//division in assemblyfunctiondivAssemblyTest(uint256a,uint256b)publicpure{assembly{letc :=div(a,b)ifgt(c,a){mstore(0x00,"underflow")revert(0x00,0x20)}}}}
Solidity >= 0.8.0 checks for overflow/underflow by default. Using Safemath when using version >= 0.8.0 is redundant and will incur additional gas costs. Instead of safemath, you can simply use Solidity's built in arithmetic. For further gas savings, you can also use assembly and check for overflow/underflow as seen below.
Gas Optimizations
The following sections detail the gas optimizations found throughout the codebase. Each optimization is documented with the setup, an explainer for the optimization, a gas report and line identifiers for each optimization across the codebase. For each section's gas report, the optimizer was turned on and set to 10000 runs. You can replicate any tests/gas reports by heading to 0xKitsune/gas-lab and cloning the repo. Then, simply copy/paste the contract examples from any section and run
forge test --gas-report
. You can also easily update the optimizer runs in thefoundry.toml
.Use custom errors instead of string error messages
Gas Report
Lines
BaseRewardPool.sol:122
BaseRewardPool.sol:123
BaseRewardPool.sol:124
BaseRewardPool.sol:132
BaseRewardPool.sol:173
BaseRewardPool.sol:196
BaseRewardPool.sol:215
BaseRewardPool.sol:301
Booster.sol:124
Booster.sol:130
Booster.sol:136
Booster.sol:146
Booster.sol:163
Booster.sol:169
Booster.sol:179
Booster.sol:194
Booster.sol:226
Booster.sol:231
Booster.sol:244
Booster.sol:261
Booster.sol:262
Booster.sol:309
Booster.sol:326
Booster.sol:350
Booster.sol:352
Booster.sol:360
Booster.sol:448
Booster.sol:458
Booster.sol:468
Booster.sol:477
Booster.sol:485
Booster.sol:498
Booster.sol:570
Booster.sol:604
VE3DRewardPool.sol:135
VE3DRewardPool.sol:136
VE3DRewardPool.sol:142
VE3DRewardPool.sol:210
VE3DRewardPool.sol:234
VE3DRewardPool.sol:253
VE3DRewardPool.sol:342
VeAssetDepositor.sol:132
VeAssetDepositor.sol:54
VeAssetDepositor.sol:60
VeAssetDepositor.sol:69
VeTokenMinter.sol:42
VeTokenMinter.sol:49
VoterProxy.sol:110
VoterProxy.sol:128
VoterProxy.sol:139
VoterProxy.sol:151
VoterProxy.sol:159
VoterProxy.sol:167
VoterProxy.sol:173
VoterProxy.sol:186
VoterProxy.sol:211
VoterProxy.sol:225
VoterProxy.sol:257
VoterProxy.sol:263
VoterProxy.sol:279
VoterProxy.sol:282
VoterProxy.sol:63
VoterProxy.sol:68
VoterProxy.sol:78
VoterProxy.sol:84
VoterProxy.sol:92
unchecked{++i}
instead ofi++
(or use assembly when applicable)Use
++i
instead ofi++
. This is especially useful in for loops but this optimization can be used anywhere in your code. You can also useunchecked{++i;}
for even more gas savings but this will not check to see ifi
overflows. For extra safety if you are worried about this, you can add a require statement after the loop checking ifi
is equal to the final incremented value. For best gas savings, use inline assembly, however this limits the functionality you can achieve. For example you cant use Solidity syntax to internally call your own contract within an assembly block and external calls must be done with thecall()
ordelegatecall()
instruction. However when applicable, inline assembly will save much more gas.Gas Report
Lines
BaseRewardPool.sol:176
BaseRewardPool.sol:199
BaseRewardPool.sol:218
BaseRewardPool.sol:245
BaseRewardPool.sol:282
Booster.sol:329
VE3DRewardPool.sol:148
VE3DRewardPool.sol:214
VE3DRewardPool.sol:238
VE3DRewardPool.sol:257
VE3DRewardPool.sol:281
VE3DRewardPool.sol:326
VoterProxy.sol:217
Use assembly for math (add, sub, mul, div)
Use assembly for math instead of Solidity. You can check for overflow/underflow in assembly to ensure safety. If using Solidity versions < 0.8.0 and you are using Safemath, you can gain significant gas savings by using assembly to calculate values and checking for overflow/underflow.
Gas Report
Lines
BaseRewardPool.sol:311
BaseRewardPool.sol:314
BaseRewardPool.sol:59
VE3DRewardPool.sol:352
VeAssetDepositor.sol:103
VeAssetDepositor.sol:18
VeAssetDepositor.sol:74
VeTokenMinter.sol:15
Don't use SafeMath when using solidity >= 0.8.0
Solidity >= 0.8.0 checks for overflow/underflow by default. Using Safemath when using version >= 0.8.0 is redundant and will incur additional gas costs. Instead of safemath, you can simply use Solidity's built in arithmetic. For further gas savings, you can also use assembly and check for overflow/underflow as seen below.
Gas report
Lines
BaseRewardPool.sol:157
BaseRewardPool.sol:158
BaseRewardPool.sol:167
BaseRewardPool.sol:168
BaseRewardPool.sol:169
BaseRewardPool.sol:180
BaseRewardPool.sol:181
BaseRewardPool.sol:204
BaseRewardPool.sol:205
BaseRewardPool.sol:222
BaseRewardPool.sol:223
BaseRewardPool.sol:249
BaseRewardPool.sol:250
BaseRewardPool.sol:296
BaseRewardPool.sol:303
BaseRewardPool.sol:312
BaseRewardPool.sol:315
BaseRewardPool.sol:317
BaseRewardPool.sol:328
BaseRewardPool.sol:330
BaseRewardPool.sol:332
BaseRewardPool.sol:333
BaseRewardPool.sol:334
BaseRewardPool.sol:335
BaseRewardPool.sol:339
BaseRewardPool.sol:344
Booster.sol:228
Booster.sol:518
Booster.sol:519
Booster.sol:520
Booster.sol:523
Booster.sol:528
Booster.sol:529
Booster.sol:535
Booster.sol:536
Booster.sol:537
Booster.sol:538
Booster.sol:582
Booster.sol:583
Booster.sol:607
VE3DRewardPool.sol:111
VE3DRewardPool.sol:115
VE3DRewardPool.sol:176
VE3DRewardPool.sol:178
VE3DRewardPool.sol:179
VE3DRewardPool.sol:180
VE3DRewardPool.sol:181
VE3DRewardPool.sol:188
VE3DRewardPool.sol:189
VE3DRewardPool.sol:193
VE3DRewardPool.sol:194
VE3DRewardPool.sol:202
VE3DRewardPool.sol:206
VE3DRewardPool.sol:219
VE3DRewardPool.sol:221
VE3DRewardPool.sol:243
VE3DRewardPool.sol:245
VE3DRewardPool.sol:261
VE3DRewardPool.sol:262
VE3DRewardPool.sol:344
VE3DRewardPool.sol:353
VE3DRewardPool.sol:354
VE3DRewardPool.sol:358
VE3DRewardPool.sol:373
VE3DRewardPool.sol:375
VE3DRewardPool.sol:376
VE3DRewardPool.sol:377
VE3DRewardPool.sol:378
VE3DRewardPool.sol:382
VeAssetDepositor.sol:106
VeAssetDepositor.sol:140
VeAssetDepositor.sol:147
VeAssetDepositor.sol:148
VeAssetDepositor.sol:151
VeTokenMinter.sol:29
VeTokenMinter.sol:33
VeTokenMinter.sol:57
VeTokenMinter.sol:61
VeTokenMinter.sol:63
VeTokenMinter.sol:66
VoterProxy.sol:131
VoterProxy.sol:132
VoterProxy.sol:140
Use multiple
require()
statments insted ofrequire(expression && expression && ...)
Gas Report
Lines
Booster.sol:261
Booster.sol:262
Use assembly to hash instead of Solidity
Gas Report
Lines
Use assembly to check for address(0)
Gas Report
Lines
BaseRewardPool.sol:123
BaseRewardPool.sol:140
Booster.sol:151
Booster.sol:183
Booster.sol:262
Booster.sol:297
Booster.sol:360
Booster.sol:365
Booster.sol:417
Booster.sol:507
Booster.sol:526
VE3DRewardPool.sol:136
VE3DRewardPool.sol:152
VeAssetDepositor.sol:154
VoterProxy.sol:70
VoterProxy.sol:85
The text was updated successfully, but these errors were encountered: