Skip to content

Commit

Permalink
Optimization/avoid unnecessary sload (#73)
Browse files Browse the repository at this point in the history
* optimization: unify pool initializer checker to save one sload

* optimization: extract the gas a bit more by rewriting the revert logic in assembly
  • Loading branch information
chefburger authored Jun 12, 2024
1 parent 3a99be9 commit f051737
Show file tree
Hide file tree
Showing 38 changed files with 81 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179107
179055
Original file line number Diff line number Diff line change
@@ -1 +1 @@
182580
182536
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
329193
329132
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
190695
190631
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134814
134765
Original file line number Diff line number Diff line change
@@ -1 +1 @@
32311
32346
Original file line number Diff line number Diff line change
@@ -1 +1 @@
30548
30536
Original file line number Diff line number Diff line change
@@ -1 +1 @@
143911
143850
Original file line number Diff line number Diff line change
@@ -1 +1 @@
290014
289965
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
127941
127892
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
116371
116330
Original file line number Diff line number Diff line change
@@ -1 +1 @@
971054
970993
Original file line number Diff line number Diff line change
@@ -1 +1 @@
329375
329314
Original file line number Diff line number Diff line change
@@ -1 +1 @@
339361
339300
Original file line number Diff line number Diff line change
@@ -1 +1 @@
141771
141710
Original file line number Diff line number Diff line change
@@ -1 +1 @@
174486
174422
Original file line number Diff line number Diff line change
@@ -1 +1 @@
180359
180295
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134374
134310
Original file line number Diff line number Diff line change
@@ -1 +1 @@
306120
306059
Original file line number Diff line number Diff line change
@@ -1 +1 @@
34468
34492
Original file line number Diff line number Diff line change
@@ -1 +1 @@
354532
354468
Original file line number Diff line number Diff line change
@@ -1 +1 @@
169969
169905
Original file line number Diff line number Diff line change
@@ -1 +1 @@
240680
240616
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
165995
165934
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
110429
110368
Original file line number Diff line number Diff line change
@@ -1 +1 @@
117154
117090
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133908
133844
Original file line number Diff line number Diff line change
@@ -1 +1 @@
166673
166609
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154038
153974
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_simple.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
73539
73475
Original file line number Diff line number Diff line change
@@ -1 +1 @@
146083
146019
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withHooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
89753
89689
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withNative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
73542
73478
Original file line number Diff line number Diff line change
@@ -1 +1 @@
32060
32107
34 changes: 18 additions & 16 deletions src/pool-bin/BinPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,16 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
if (amountIn == 0) revert InsufficientAmountIn();

PoolId id = key.toId();
_checkPoolInitialized(id);
BinPool.State storage pool = pools[id];
pool.checkPoolInitialized();

(uint128 amountToSwap, BeforeSwapDelta beforeSwapDelta, uint24 lpFeeOverride) =
BinHooks.beforeSwap(key, swapForY, amountIn, hookData);

/// @dev fix stack too deep
{
BinPool.SwapState memory state;
(delta, state) = pools[id].swap(
(delta, state) = pool.swap(
BinPool.SwapParams({
swapForY: swapForY,
binStep: key.parameters.getBinStep(),
Expand Down Expand Up @@ -182,7 +183,8 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
returns (uint128 amountIn, uint128 amountOutLeft, uint128 fee)
{
PoolId id = key.toId();
_checkPoolInitialized(id);
BinPool.State storage pool = pools[id];
pool.checkPoolInitialized();

uint24 lpFee;
if (key.fee.isDynamicLPFee()) {
Expand All @@ -194,7 +196,7 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
}
lpFee.validate(LPFeeLibrary.TEN_PERCENT_FEE);

(amountIn, amountOutLeft, fee) = pools[id].getSwapIn(
(amountIn, amountOutLeft, fee) = pool.getSwapIn(
BinPool.SwapViewParams({swapForY: swapForY, binStep: key.parameters.getBinStep(), lpFee: lpFee}), amountOut
);
}
Expand All @@ -207,7 +209,8 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
returns (uint128 amountInLeft, uint128 amountOut, uint128 fee)
{
PoolId id = key.toId();
_checkPoolInitialized(id);
BinPool.State storage pool = pools[id];
pool.checkPoolInitialized();

uint24 lpFee;
if (key.fee.isDynamicLPFee()) {
Expand All @@ -218,7 +221,7 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
}
lpFee.validate(LPFeeLibrary.TEN_PERCENT_FEE);

(amountInLeft, amountOut, fee) = pools[id].getSwapOut(
(amountInLeft, amountOut, fee) = pool.getSwapOut(
BinPool.SwapViewParams({swapForY: swapForY, binStep: key.parameters.getBinStep(), lpFee: lpFee}), amountIn
);
}
Expand All @@ -231,13 +234,14 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
returns (BalanceDelta delta, BinPool.MintArrays memory mintArray)
{
PoolId id = key.toId();
_checkPoolInitialized(id);
BinPool.State storage pool = pools[id];
pool.checkPoolInitialized();

BinHooks.beforeMint(key, params, hookData);

bytes32 feeForProtocol;
bytes32 compositionFee;
(delta, feeForProtocol, mintArray, compositionFee) = pools[id].mint(
(delta, feeForProtocol, mintArray, compositionFee) = pool.mint(
BinPool.MintParams({
to: msg.sender,
liquidityConfigs: params.liquidityConfigs,
Expand Down Expand Up @@ -273,13 +277,14 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
returns (BalanceDelta delta)
{
PoolId id = key.toId();
_checkPoolInitialized(id);
BinPool.State storage pool = pools[id];
pool.checkPoolInitialized();

BinHooks.beforeBurn(key, params, hookData);

uint256[] memory binIds;
bytes32[] memory amountRemoved;
(delta, binIds, amountRemoved) = pools[id].burn(
(delta, binIds, amountRemoved) = pool.burn(
BinPool.BurnParams({
from: msg.sender,
ids: params.ids,
Expand Down Expand Up @@ -307,11 +312,12 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
returns (BalanceDelta delta, uint24 binId)
{
PoolId id = key.toId();
_checkPoolInitialized(id);
BinPool.State storage pool = pools[id];
pool.checkPoolInitialized();

BinHooks.beforeDonate(key, amount0, amount1, hookData);

(delta, binId) = pools[id].donate(key.parameters.getBinStep(), amount0, amount1);
(delta, binId) = pool.donate(key.parameters.getBinStep(), amount0, amount1);

vault.accountAppBalanceDelta(key, delta, msg.sender);

Expand Down Expand Up @@ -342,8 +348,4 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
function _setProtocolFee(PoolId id, uint24 newProtocolFee) internal override {
pools[id].setProtocolFee(newProtocolFee);
}

function _checkPoolInitialized(PoolId id) internal view {
if (pools[id].isNotInitialized()) revert PoolNotInitialized();
}
}
14 changes: 10 additions & 4 deletions src/pool-bin/libraries/BinPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ library BinPool {
}

function setProtocolFee(State storage self, uint24 protocolFee) internal {
if (self.isNotInitialized()) revert PoolNotInitialized();
self.checkPoolInitialized();
self.slot0.protocolFee = protocolFee;
}

/// @notice Only dynamic fee pools may update the swap fee.
function setLPFee(State storage self, uint24 lpFee) internal {
if (self.isNotInitialized()) revert PoolNotInitialized();
self.checkPoolInitialized();

self.slot0.lpFee = lpFee;
}
Expand Down Expand Up @@ -524,7 +524,13 @@ library BinPool {
(, self.level0) = TreeMath.remove(self.level0, self.level1, self.level2, binId);
}

function isNotInitialized(State storage self) internal view returns (bool) {
return self.slot0.activeId == 0;
function checkPoolInitialized(State storage self) internal view {
if (self.slot0.activeId == 0) {
// revert PoolNotInitialized();
assembly ("memory-safe") {
mstore(0x00, 0x486aa307)
revert(0x1c, 0x04)
}
}
}
}
19 changes: 9 additions & 10 deletions src/pool-cl/CLPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ contract CLPoolManager is ICLPoolManager, ProtocolFees, Extsload {
if (paused() && params.liquidityDelta > 0) revert PoolPaused();

PoolId id = key.toId();
_checkPoolInitialized(id);
CLPool.State storage pool = pools[id];
pool.checkPoolInitialized();

CLHooks.beforeModifyLiquidity(key, params, hookData);

(delta, feeDelta) = pools[id].modifyLiquidity(
(delta, feeDelta) = pool.modifyLiquidity(
CLPool.ModifyLiquidityParams({
owner: msg.sender,
tickLower: params.tickLower,
Expand Down Expand Up @@ -167,12 +168,13 @@ contract CLPoolManager is ICLPoolManager, ProtocolFees, Extsload {
if (params.amountSpecified == 0) revert SwapAmountCannotBeZero();

PoolId id = key.toId();
_checkPoolInitialized(id);
CLPool.State storage pool = pools[id];
pool.checkPoolInitialized();

(int256 amountToSwap, BeforeSwapDelta beforeSwapDelta, uint24 lpFeeOverride) =
CLHooks.beforeSwap(key, params, hookData);
CLPool.SwapState memory state;
(delta, state) = pools[id].swap(
(delta, state) = pool.swap(
CLPool.SwapParams({
tickSpacing: key.parameters.getTickSpacing(),
zeroForOne: params.zeroForOne,
Expand Down Expand Up @@ -221,12 +223,13 @@ contract CLPoolManager is ICLPoolManager, ProtocolFees, Extsload {
returns (BalanceDelta delta)
{
PoolId id = key.toId();
_checkPoolInitialized(id);
CLPool.State storage pool = pools[id];
pool.checkPoolInitialized();

CLHooks.beforeDonate(key, amount0, amount1, hookData);

int24 tick;
(delta, tick) = pools[id].donate(amount0, amount1);
(delta, tick) = pool.donate(amount0, amount1);
vault.accountAppBalanceDelta(key, delta, msg.sender);

/// @notice Make sure the first event is noted, so that later events from afterHook won't get mixed up with this one
Expand Down Expand Up @@ -265,10 +268,6 @@ contract CLPoolManager is ICLPoolManager, ProtocolFees, Extsload {
pools[id].setProtocolFee(newProtocolFee);
}

function _checkPoolInitialized(PoolId id) internal view {
if (pools[id].isNotInitialized()) revert PoolNotInitialized();
}

/// @notice not accept ether
// receive() external payable {}
// fallback() external payable {}
Expand Down
Loading

0 comments on commit f051737

Please sign in to comment.