Skip to content

Commit

Permalink
Optimize bytecode (#701)
Browse files Browse the repository at this point in the history
  • Loading branch information
jubeira authored Jul 1, 2024
1 parent b24dfc7 commit 78ffc4d
Show file tree
Hide file tree
Showing 43 changed files with 55 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179.6k
180.1k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
197.4k
198.0k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
211.7k
212.3k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
168.7k
169.3k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
186.9k
187.5k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
208.7k
209.4k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
175.1k
175.4k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
158.6k
158.8k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
235.0k
235.6k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
199.6k
200.2k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
228.4k
229.1k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
223.9k
224.6k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
188.8k
189.5k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
210.6k
211.3k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
208.4k
208.7k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
174.8k
175.1k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178.3k
178.8k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
347.8k
348.0k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
335.2k
335.4k
36 changes: 10 additions & 26 deletions pkg/vault/contracts/RouterCommon.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ contract RouterCommon is IRouterCommon, VaultGuard {
// they will be executed every time the constant is used.
// solhint-disable-next-line var-name-mixedcase
bytes32 private immutable _SENDER_SLOT = TransientStorageHelpers.calculateSlot(type(RouterCommon).name, "sender");
// solhint-disable-next-line var-name-mixedcase
bytes32 private immutable _SENDER_LOCKED_SLOT =
TransientStorageHelpers.calculateSlot(type(RouterCommon).name, "senderLocked");

/// @dev Incoming ETH transfer from an address that is not WETH.
error EthTransfer();
Expand Down Expand Up @@ -78,36 +75,27 @@ contract RouterCommon is IRouterCommon, VaultGuard {
* (Not nested, as the original router call from contractA has returned. Sender is now ContractB.)
*/
modifier saveSender() {
// isSenderLocked = false means that the sender is the most external one, so lock the sender slot and save the
// sender in the transient storage
bool isSenderLocked = _getSenderLockedSlot().tload();
if (isSenderLocked == false) {
_getSenderLockedSlot().tstore(true);
}

_saveSender();
bool isExternalSender = _saveSender();
_;

if (isSenderLocked == false) {
// isSenderLocked = false means that the sender is the most external one, which means that the router
// operation is over. Discard the sender (so sender can be used in the same transaction by another router
// call) and unlock the sender slot.
_discardSender();
_getSenderLockedSlot().tstore(false);
}
_discardSenderIfRequired(isExternalSender);
}

function _saveSender() internal {
function _saveSender() internal returns (bool isExternalSender) {
address sender = _getSenderSlot().tload();

// NOTE: Only the most external sender will be saved by the router.
if (sender == address(0)) {
_getSenderSlot().tstore(msg.sender);
isExternalSender = true;
}
}

function _discardSender() internal {
_getSenderSlot().tstore(address(0));
function _discardSenderIfRequired(bool isExternalSender) internal {
// Only the external sender shall be cleaned up; if it's not an external sender it means that
// the value was not saved in this modifier.
if (isExternalSender) {
_getSenderSlot().tstore(address(0));
}
}

constructor(IVault vault, IWETH weth, IPermit2 permit2) VaultGuard(vault) {
Expand Down Expand Up @@ -215,8 +203,4 @@ contract RouterCommon is IRouterCommon, VaultGuard {
function _getSenderSlot() internal view returns (StorageSlot.AddressSlotType) {
return _SENDER_SLOT.asAddress();
}

function _getSenderLockedSlot() internal view returns (StorageSlot.BooleanSlotType) {
return _SENDER_LOCKED_SLOT.asBoolean();
}
}
4 changes: 2 additions & 2 deletions pkg/vault/test/.contract-sizes/BatchRouter
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Bytecode 15.316
InitCode 16.953
Bytecode 15.151
InitCode 16.684
4 changes: 2 additions & 2 deletions pkg/vault/test/.contract-sizes/Router
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Bytecode 24.999*
InitCode 26.381
Bytecode 23.659
InitCode 24.739
4 changes: 2 additions & 2 deletions pkg/vault/test/.contract-sizes/Vault
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Bytecode 24.229*
InitCode 25.512
Bytecode 23.921
InitCode 25.203
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179.6k
180.2k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
197.2k
197.8k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
195.6k
196.2k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
168.7k
169.3k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
186.7k
187.3k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
192.8k
193.5k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
175.2k
175.4k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
158.6k
158.8k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
235.1k
235.7k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
199.3k
199.9k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
212.1k
212.7k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
223.9k
224.6k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
188.6k
189.3k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
194.7k
195.5k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
208.5k
208.8k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
174.8k
175.0k
2 changes: 1 addition & 1 deletion pkg/vault/test/gas/.hardhat-snapshots/[PoolMock] donation
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178.3k
178.9k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
342.5k
342.8k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
330.0k
330.2k
2 changes: 1 addition & 1 deletion pvt/common/hardhat-base-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const contractSettings: ContractSettings = {
},
'@balancer-labs/v3-vault/contracts/Vault.sol': {
version: '0.8.24',
runs: 500,
runs: 200,
viaIR: true,
},
'@balancer-labs/v3-vault/contracts/VaultExtension.sol': {
Expand Down

0 comments on commit 78ffc4d

Please sign in to comment.