-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gas Optimizations #91
Comments
Use assembly to check for address(0) (disputed)Solidity audit Use assembly when getting a contract's balance of ETH. (disputed)Solidity audit Use assembly to hash instead of Solidity (disputed)Solidity audit Use assembly for math (add, sub, mul, div) (disputed)Solidity audit Use assembly to write storage values (disputed)Solidity audit |
Right shift instead of dividing by two (Duplicated)Duplicated of #89 at Use Shift Right/Left instead of Division/Multiplication |
Use custom errors instead of string error messages (Duplicated) |
unchecked{++i} instead of i++ (or use assembly when applicable (Duplicated)Duplicated of #2 at For loop optimizaion |
Use multiple require() statments insted of require(expression && expression && ...) (duplicate). |
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
.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
BeefyVaultOperator.sol:18
BeefyZapUniswapLPVaultOperator.sol:27
YearnCurveVaultOperator.sol:42
NestedFactory.sol:124
NestedFactory.sol:136
NestedFactory.sol:196
NestedFactory.sol:256
NestedFactory.sol:315
NestedFactory.sol:333
NestedFactory.sol:369
NestedFactory.sol:412
NestedFactory.sol:651
TimelockControllerEmergency.sol:84
TimelockControllerEmergency.sol:89
TimelockControllerEmergency.sol:234
TimelockControllerEmergency.sol:324
MixinOperatorResolver.sol:37
MixinOperatorResolver.sol:56
BeefyZapBiswapLPVaultOperator.sol:27
OperatorScripts.sol:67
OperatorScripts.sol:80
CurveHelpers.sol:22
CurveHelpers.sol:42
CurveHelpers.sol:62
CurveHelpers.sol:86
OperatorResolver.sol:40
OperatorResolver.sol:60
OperatorResolver.sol:75
Use assembly to check for address(0)
Gas Report
Lines
BeefyVaultOperator.sol:43
BeefyVaultOperator.sol:85
BeefyZapUniswapLPVaultOperator.sol:54
BeefyZapUniswapLPVaultOperator.sol:99
YearnVaultStorage.sol:30
YearnVaultStorage.sol:31
YearnVaultStorage.sol:32
YearnVaultStorage.sol:33
YearnVaultStorage.sol:34
YearnVaultStorage.sol:42
YearnCurveVaultOperator.sol:73
YearnCurveVaultOperator.sol:123
YearnCurveVaultOperator.sol:167
YearnCurveVaultOperator.sol:215
YearnCurveVaultOperator.sol:263
OwnerProxy.sol:17
NestedFactory.sol:67
NestedFactory.sol:68
NestedFactory.sol:69
NestedFactory.sol:70
NestedFactory.sol:71
NestedFactory.sol:72
NestedFactory.sol:73
NestedFactory.sol:140
NestedFactory.sol:153
NestedFactory.sol:574
MixinOperatorResolver.sol:23
MixinOperatorResolver.sol:41
MixinOperatorResolver.sol:64
MixinOperatorResolver.sol:77
BeefyVaultStorage.sol:25
BeefyVaultStorage.sol:26
BeefyVaultStorage.sol:27
BeefyVaultStorage.sol:35
BeefyZapBiswapLPVaultOperator.sol:54
BeefyZapBiswapLPVaultOperator.sol:99
OperatorScripts.sol:19
OperatorScripts.sol:20
OperatorScripts.sol:29
OperatorScripts.sol:61
ParaswapOperator.sol:16
OwnableProxyDelegation.sol:25
OwnableProxyDelegation.sol:57
OperatorResolver.sol:27
Use multiple
require()
statments insted ofrequire(expression && expression && ...)
Gas Report
Lines
BeefyVaultOperator.sol:54
BeefyZapUniswapLPVaultOperator.sol:64
BeefyZapUniswapLPVaultOperator.sol:65
NestedFactory.sol:67
BeefyZapBiswapLPVaultOperator.sol:64
BeefyZapBiswapLPVaultOperator.sol:65
ParaswapOperator.sol:16
Use assembly to hash instead of Solidity
Gas Report
Lines
StakingLPVaultHelpers.sol:105
StakingLPVaultHelpers.sol:135
TimelockControllerEmergency.sol:173
TimelockControllerEmergency.sol:187
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
BeefyVaultOperator.sol:52
BeefyVaultOperator.sol:53
BeefyVaultOperator.sol:93
BeefyVaultOperator.sol:94
BeefyZapUniswapLPVaultOperator.sol:61
BeefyZapUniswapLPVaultOperator.sol:62
BeefyZapUniswapLPVaultOperator.sol:106
BeefyZapUniswapLPVaultOperator.sol:107
BeefyZapUniswapLPVaultOperator.sol:145
BeefyZapUniswapLPVaultOperator.sol:249
BeefyZapUniswapLPVaultOperator.sol:273
BeefyZapUniswapLPVaultOperator.sol:280
BeefyZapUniswapLPVaultOperator.sol:284
NestedFactory.sol:138
NestedFactory.sol:263
NestedFactory.sol:264
NestedFactory.sol:341
NestedFactory.sol:378
NestedFactory.sol:379
NestedFactory.sol:381
NestedFactory.sol:388
NestedFactory.sol:431
NestedFactory.sol:435
NestedFactory.sol:439
NestedFactory.sol:443
NestedFactory.sol:446
NestedFactory.sol:497
NestedFactory.sol:525
NestedFactory.sol:560
NestedFactory.sol:593
NestedFactory.sol:629
NestedFactory.sol:632
NestedFactory.sol:644
StakingLPVaultHelpers.sol:44
StakingLPVaultHelpers.sol:76
StakingLPVaultHelpers.sol:102
StakingLPVaultHelpers.sol:132
TimelockControllerEmergency.sol:245
BeefyZapBiswapLPVaultOperator.sol:61
BeefyZapBiswapLPVaultOperator.sol:62
BeefyZapBiswapLPVaultOperator.sol:106
BeefyZapBiswapLPVaultOperator.sol:107
BeefyZapBiswapLPVaultOperator.sol:145
BeefyZapBiswapLPVaultOperator.sol:249
BeefyZapBiswapLPVaultOperator.sol:275
BeefyZapBiswapLPVaultOperator.sol:282
BeefyZapBiswapLPVaultOperator.sol:286
ParaswapOperator.sol:37
ParaswapOperator.sol:38
Use assembly when getting a contract's balance of ETH.
You can use
selfbalance()
instead ofaddress(this).balance
when getting your contract's balance of ETH to save gas. Additionally, you can usebalance(address)
instead ofaddress.balance()
when getting an external contract's balance of ETH.Gas Report
Lines
Use custom errors instead of string error messages
Gas Report
Lines
BeefyVaultOperator.sol:15
BeefyVaultOperator.sol:41
BeefyVaultOperator.sol:43
BeefyVaultOperator.sol:50
BeefyVaultOperator.sol:54
BeefyVaultOperator.sol:55
BeefyVaultOperator.sol:83
BeefyVaultOperator.sol:85
BeefyVaultOperator.sol:91
BeefyVaultOperator.sol:95
BeefyVaultOperator.sol:96
BeefyZapUniswapLPVaultOperator.sol:23
BeefyZapUniswapLPVaultOperator.sol:52
BeefyZapUniswapLPVaultOperator.sol:54
BeefyZapUniswapLPVaultOperator.sol:64
BeefyZapUniswapLPVaultOperator.sol:65
BeefyZapUniswapLPVaultOperator.sol:97
BeefyZapUniswapLPVaultOperator.sol:99
BeefyZapUniswapLPVaultOperator.sol:108
BeefyZapUniswapLPVaultOperator.sol:109
BeefyZapUniswapLPVaultOperator.sol:142
BeefyZapUniswapLPVaultOperator.sol:187
BeefyZapUniswapLPVaultOperator.sol:198
BeefyZapUniswapLPVaultOperator.sol:269
BeefyZapUniswapLPVaultOperator.sol:270
YearnVaultStorage.sol:30
YearnVaultStorage.sol:31
YearnVaultStorage.sol:32
YearnVaultStorage.sol:33
YearnVaultStorage.sol:34
YearnVaultStorage.sol:42
YearnCurveVaultOperator.sol:39
YearnCurveVaultOperator.sol:70
YearnCurveVaultOperator.sol:73
YearnCurveVaultOperator.sol:121
YearnCurveVaultOperator.sol:123
YearnCurveVaultOperator.sol:164
YearnCurveVaultOperator.sol:167
YearnCurveVaultOperator.sol:212
YearnCurveVaultOperator.sol:215
YearnCurveVaultOperator.sol:260
YearnCurveVaultOperator.sol:263
Withdrawer.sol:21
OwnerProxy.sol:17
NestedFactory.sol:74
NestedFactory.sol:99
NestedFactory.sol:107
NestedFactory.sol:122
NestedFactory.sol:125
NestedFactory.sol:153
NestedFactory.sol:160
NestedFactory.sol:161
NestedFactory.sol:168
NestedFactory.sol:169
NestedFactory.sol:191
NestedFactory.sol:250
NestedFactory.sol:251
NestedFactory.sol:252
NestedFactory.sol:286
NestedFactory.sol:288
NestedFactory.sol:289
NestedFactory.sol:312
NestedFactory.sol:313
NestedFactory.sol:330
NestedFactory.sol:331
NestedFactory.sol:359
NestedFactory.sol:379
NestedFactory.sol:406
NestedFactory.sol:407
NestedFactory.sol:428
NestedFactory.sol:469
NestedFactory.sol:495
NestedFactory.sol:543
NestedFactory.sol:544
NestedFactory.sol:553
NestedFactory.sol:612
NestedFactory.sol:656
StakingLPVaultHelpers.sol:108
StakingLPVaultHelpers.sol:138
TimelockControllerEmergency.sol:229
TimelockControllerEmergency.sol:230
TimelockControllerEmergency.sol:243
TimelockControllerEmergency.sol:244
TimelockControllerEmergency.sol:256
TimelockControllerEmergency.sol:319
TimelockControllerEmergency.sol:320
TimelockControllerEmergency.sol:334
TimelockControllerEmergency.sol:335
TimelockControllerEmergency.sol:342
TimelockControllerEmergency.sol:359
TimelockControllerEmergency.sol:375
MixinOperatorResolver.sol:23
MixinOperatorResolver.sol:103
MixinOperatorResolver.sol:104
BeefyVaultStorage.sol:25
BeefyVaultStorage.sol:26
BeefyVaultStorage.sol:27
BeefyVaultStorage.sol:35
BeefyZapBiswapLPVaultOperator.sol:23
BeefyZapBiswapLPVaultOperator.sol:52
BeefyZapBiswapLPVaultOperator.sol:54
BeefyZapBiswapLPVaultOperator.sol:64
BeefyZapBiswapLPVaultOperator.sol:65
BeefyZapBiswapLPVaultOperator.sol:97
BeefyZapBiswapLPVaultOperator.sol:99
BeefyZapBiswapLPVaultOperator.sol:108
BeefyZapBiswapLPVaultOperator.sol:109
BeefyZapBiswapLPVaultOperator.sol:142
BeefyZapBiswapLPVaultOperator.sol:187
BeefyZapBiswapLPVaultOperator.sol:198
BeefyZapBiswapLPVaultOperator.sol:271
BeefyZapBiswapLPVaultOperator.sol:272
OperatorScripts.sol:19
OperatorScripts.sol:20
OperatorScripts.sol:29
OperatorScripts.sol:54
OperatorScripts.sol:55
OperatorScripts.sol:61
ParaswapOperator.sol:16
ParaswapOperator.sol:27
ParaswapOperator.sol:35
ParaswapOperator.sol:39
ParaswapOperator.sol:40
OwnableProxyDelegation.sol:25
OwnableProxyDelegation.sol:26
OwnableProxyDelegation.sol:27
OwnableProxyDelegation.sol:41
OwnableProxyDelegation.sol:57
OperatorResolver.sol:39
OperatorResolver.sol:57
Right shift instead of dividing by two
Gas Report
Lines
BeefyZapUniswapLPVaultOperator.sol:273
BeefyZapBiswapLPVaultOperator.sol:275
Use assembly to write storage values
Gas Report
Lines
YearnCurveVaultOperator.sol:48
NestedFactory.sol:162
NestedFactory.sol:170
TimelockControllerEmergency.sol:93
TimelockControllerEmergency.sol:377
OperatorScripts.sol:21
OperatorScripts.sol:22
ParaswapOperator.sol:17
ParaswapOperator.sol:18
OwnableProxyDelegation.sol:31
OwnableProxyDelegation.sol:65
The text was updated successfully, but these errors were encountered: