-
Notifications
You must be signed in to change notification settings - Fork 75
improve(gas-golf): Minor gas tweaks #73
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
Conversation
chrismaree
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏌️
contracts/HubPool.sol
Outdated
| l1Token, // l1Token. | ||
| l2Token, // l2Token. | ||
| uint256(netSendAmounts[i]), // amount. | ||
| crossChainContracts[chainId].spokePool // to. This should be the spokePool. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Loading this data in each loop adds gas, we should pre-load it instead
mrice32
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed IRL, I think we might want to hold off on merging this until we decide whether we think there's enough time for OZ to review this along with other fixes.
| // The bond should be the passed in bondAmount + the final fee. | ||
| bondToken = newBondToken; | ||
| bondAmount = newBondAmount + _getBondTokenFinalFee(); | ||
| emit BondSet(address(newBondToken), bondAmount); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't SLOAD unnecessarily
|
|
||
| // L1 tokens length won't be > types(uint32).length | ||
| unchecked { | ||
| ++i; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pre-increment saves a STORE vs post-increment
| function setFxChild(address newFxChild) public onlyAdmin { | ||
| fxChild = newFxChild; | ||
| emit SetFxChild(fxChild); | ||
| emit SetFxChild(newFxChild); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't SLOAD unnecessarily
| // can be submitted. | ||
| RootBundle public rootBundleProposal; | ||
|
|
||
| // Whether the bundle proposal process is paused. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any changes in the order of state variables is done to take advantage of variable packing
|
|
||
| // WETH contract for Ethereum. | ||
| WETH9 public weth; | ||
| WETH9 public immutable weth; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use immutable where possible
contracts/HubPool.sol
Outdated
| uint256[] memory bundleLpFees, | ||
| int256[] memory netSendAmounts, | ||
| int256[] memory runningBalances, | ||
| uint256[] calldata bundleLpFees, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use calldata for variables that will just be loaded into an in memory PoolRebalance struct
contracts/HubPool.sol
Outdated
|
|
||
| // Before interacting with a particular chain's adapter, ensure that the adapter is set. | ||
| require(address(crossChainContracts[chainId].adapter) != address(0), "No adapter for chain"); | ||
| CrossChainContract memory _crossChainContracts = crossChainContracts[chainId]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reduce one SLOAD
| AdapterInterface adapter = crossChainContracts[chainId].adapter; | ||
|
|
||
| for (uint32 i = 0; i < l1Tokens.length; i++) { | ||
| uint32 length = uint32(l1Tokens.length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cache loop length
mrice32
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Signed-off-by: Matt Rice <matthewcrice32@gmail.com>
Makes modest gas cost reductions, I think its easy to argue we don't need these optimizations but curious what you think.
Unit test gas report from running
yarn test:gas-analyticsStress testing results
Changes
calldatainstead ofmemoryfor variables we don't change