Skip to content
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

Optimization/gas tweaks part2 #76

Merged
merged 4 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1 +1 @@
354468
354066
Original file line number Diff line number Diff line change
@@ -1 +1 @@
169905
169503
Original file line number Diff line number Diff line change
@@ -1 +1 @@
240616
240214
Original file line number Diff line number Diff line change
@@ -1 +1 @@
117090
116696
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133844
133828
Original file line number Diff line number Diff line change
@@ -1 +1 @@
166609
166601
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153974
153966
Original file line number Diff line number Diff line change
@@ -1 +1 @@
146019
146003
Original file line number Diff line number Diff line change
@@ -1 +1 @@
380
372
Original file line number Diff line number Diff line change
@@ -1 +1 @@
571
563
Original file line number Diff line number Diff line change
@@ -1 +1 @@
582
574
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1296
1288
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1133
1125
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1301
1293
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2118
2110
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1130
1122
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2118
2110
Original file line number Diff line number Diff line change
@@ -1 +1 @@
563
29
2 changes: 1 addition & 1 deletion .forge-snapshots/TickTest#update.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133918
133389
5 changes: 2 additions & 3 deletions src/pool-cl/libraries/FullMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,8 @@ library FullMath {
function mulDivRoundingUp(uint256 a, uint256 b, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
result = mulDiv(a, b, denominator);
if (mulmod(a, b, denominator) > 0) {
require(result < type(uint256).max);
result++;
if (mulmod(a, b, denominator) != 0) {
require(++result > 0);
}
}
}
Expand Down
22 changes: 16 additions & 6 deletions src/pool-cl/libraries/Tick.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,22 @@ library Tick {
/// @dev Executed within the pool constructor
/// @param tickSpacing The amount of required tick separation, realized in multiples of `tickSpacing`
/// e.g., a tickSpacing of 3 requires ticks to be initialized every 3rd tick i.e., ..., -6, -3, 0, 3, 6, ...
/// @return The max liquidity per tick
function tickSpacingToMaxLiquidityPerTick(int24 tickSpacing) internal pure returns (uint128) {
int24 minTick = (TickMath.MIN_TICK / tickSpacing) * tickSpacing;
int24 maxTick = (TickMath.MAX_TICK / tickSpacing) * tickSpacing;
uint24 numTicks = uint24((maxTick - minTick) / tickSpacing) + 1;
return type(uint128).max / numTicks;
/// @return result The max liquidity per tick
function tickSpacingToMaxLiquidityPerTick(int24 tickSpacing) internal pure returns (uint128 result) {
// Equivalent to v3 but in assembly for gas efficiency:
// int24 minTick = (TickMath.MIN_TICK / tickSpacing) * tickSpacing;
// int24 maxTick = (TickMath.MAX_TICK / tickSpacing) * tickSpacing;
// uint24 numTicks = uint24((maxTick - minTick) / tickSpacing) + 1;
// return type(uint128).max / numTicks;
int24 MAX_TICK = TickMath.MAX_TICK;
int24 MIN_TICK = TickMath.MIN_TICK;
// tick spacing will never be 0 since TickMath.MIN_TICK_SPACING is 1
assembly {
let minTick := mul(sdiv(MIN_TICK, tickSpacing), tickSpacing)
let maxTick := mul(sdiv(MAX_TICK, tickSpacing), tickSpacing)
let numTicks := add(sdiv(sub(maxTick, minTick), tickSpacing), 1)
result := div(sub(shl(128, 1), 1), numTicks)
}
}

/// @notice Retrieves fee growth data
Expand Down
72 changes: 0 additions & 72 deletions test/pool-cl/helpers/PoolModifyPositionTest.sol

This file was deleted.

10 changes: 7 additions & 3 deletions test/pool-cl/libraries/FullMath.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contract FullMathTest is Test {
function test_mulDiv_fuzz(uint256 x, uint256 y, uint256 d) public {
vm.assume(d != 0);
vm.assume(y != 0);
vm.assume(x <= type(uint256).max / y);
x = bound(x, 0, type(uint256).max / y);
assertEq(FullMath.mulDiv(x, y, d), x * y / d);
}

Expand Down Expand Up @@ -97,10 +97,14 @@ contract FullMathTest is Test {
function test_mulDivRoundingUp_fuzz(uint256 x, uint256 y, uint256 d) public {
vm.assume(d != 0);
vm.assume(y != 0);
vm.assume(x <= type(uint256).max / y);
x = bound(x, 0, type(uint256).max / y);
uint256 numerator = x * y;
uint256 result = FullMath.mulDivRoundingUp(x, y, d);
assertTrue(result == numerator / d || result == numerator / d + 1);
if (mulmod(x, y, d) > 0) {
assertEq(result, numerator / d + 1);
} else {
assertEq(result, numerator / d);
}
}

function test_mulDivRounding(uint256 x, uint256 y, uint256 d) public {
Expand Down
10 changes: 10 additions & 0 deletions test/pool-cl/libraries/Tick.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -542,4 +542,14 @@ contract TickTest is Test, GasSnapshot {
// max liquidity at every tick is less than the cap
assertGe(type(uint128).max, uint256(maxLiquidityPerTick) * numTicks);
}

function test_fuzz_tickSpacingToMaxLiquidityPerTick(int24 tickSpacing) public pure {
vm.assume(tickSpacing > 0);
// v3 math
int24 minTick = (TickMath.MIN_TICK / tickSpacing) * tickSpacing;
int24 maxTick = (TickMath.MAX_TICK / tickSpacing) * tickSpacing;
uint24 numTicks = uint24((maxTick - minTick) / tickSpacing) + 1;
// assert that the result is the same as the v3 math
assertEq(type(uint128).max / numTicks, Tick.tickSpacingToMaxLiquidityPerTick(tickSpacing));
}
}
Loading