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

Optimize SqrtPriceMath #258

Closed
wants to merge 41 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
677b62f
Add `SqrtPriceMath` forge tests and snapshot
shuhuiluo Jun 16, 2023
5d4f82f
Optimize `getNextSqrtPriceFromAmount0RoundingUp`
shuhuiluo Jun 16, 2023
072a4c2
Optimize `getNextSqrtPriceFromAmount1RoundingDown`
shuhuiluo Jun 16, 2023
d8af24f
Optimize `getNextSqrtPriceFromInput` and `getNextSqrtPriceFromOutput`
shuhuiluo Jun 16, 2023
c66b805
Optimize `getAmount0Delta` by introducing `sort2` and removing branching
shuhuiluo Jun 16, 2023
5902613
Optimize `getAmount1Delta` by introducing `mulDiv96`
shuhuiluo Jun 16, 2023
ced1a76
Optimize signed `getAmount0Delta` and `getAmount1Delta` using bit ops
shuhuiluo Jun 16, 2023
3c68aee
Merge branch 'refs/heads/main' into optimize-sqrtpricemath
shuhuiluo Apr 4, 2024
f2c7ea3
Fix `getNextSqrtPriceFromAmount0RoundingUp`
shuhuiluo Apr 4, 2024
9713432
Refactor `FullMath.mulDiv96` and add tests
shuhuiluo Apr 4, 2024
cc0623d
Revert changes to `SqrtPriceMathTest`
shuhuiluo Apr 4, 2024
5a1d3da
Merge branch 'refs/heads/main' into optimize-sqrtpricemath
shuhuiluo Apr 6, 2024
5232cda
Update method name and snapshot values
shuhuiluo Apr 6, 2024
d829c2b
Refactor function & variable names for clarity
shuhuiluo Apr 6, 2024
4d7f5fe
Optimize comparison to `type(uint160).max` in `getNextSqrtPriceFromAm…
shuhuiluo Apr 6, 2024
5b183ba
Update variable names in comments for `mulDivQ96`
shuhuiluo Apr 7, 2024
45f0f2a
Implement 'absDiff' function to improve calculation in `getAmount1Delta`
shuhuiluo Apr 8, 2024
fd9eeab
Merge and update snapshots
shuhuiluo Apr 21, 2024
f82203a
Avoid casting and optimize further
shuhuiluo Apr 21, 2024
3636004
Merge and update snapshots
shuhuiluo Apr 24, 2024
f3400fb
Refactor sign and mask calculation in `SqrtPriceMath`
shuhuiluo Apr 26, 2024
14f59d1
Merge and update snapshots
shuhuiluo May 11, 2024
9eb9020
Switch if else branches
shuhuiluo May 11, 2024
8a685ba
Merge and update snapshots
shuhuiluo May 18, 2024
435a4f3
Remove unused functions from `UnsafeMath`
shuhuiluo May 18, 2024
bd96af9
Add `UnsafeMath.div` and optimize `SqrtPriceMath.getAmount0Delta`
shuhuiluo May 19, 2024
04d35ab
Add `assign` function for direct uint conversion in `SqrtPriceMath`
shuhuiluo May 19, 2024
789b88d
Add natspec on `UnsafeMath.div`
shuhuiluo May 19, 2024
a2c4b88
Optimize signed variants of `getAmount0Delta` and `getAmount1Delta`
shuhuiluo May 19, 2024
4e37569
Merge and update snapshots
shuhuiluo May 22, 2024
99104cb
Cast variables in assembly and use `UnsafeMath.div`
shuhuiluo May 23, 2024
df3c698
Merge and update snapshots
shuhuiluo May 23, 2024
d4aaf2f
Merge and update snapshots
shuhuiluo May 23, 2024
a1a6bc7
Merge and update snapshots
shuhuiluo May 23, 2024
e581828
Merge and update snapshots
shuhuiluo May 23, 2024
d381756
Merge and update snapshots
shuhuiluo May 26, 2024
d275b5b
Merge and update snapshots
shuhuiluo May 26, 2024
efd2450
Merge and update snapshots
shuhuiluo May 26, 2024
7047ddf
Cast variables in assembly and use `UnsafeMath.div`
shuhuiluo May 26, 2024
69ea63e
Optimize signed variants of `getAmount0Delta` and `getAmount1Delta`
shuhuiluo May 26, 2024
a8c1cc2
Use `FullMath.mulDivQ96` in `SqrtPriceMath`
shuhuiluo May 26, 2024
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
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_oneForZero_exactInCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1947
1853
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_oneForZero_exactInPartial.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2390
2199
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_oneForZero_exactOutCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1686
1592
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2831
2636
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_zeroForOne_exactInCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2059
1964
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_zeroForOne_exactInPartial.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2941
2724
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_zeroForOne_exactOutCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1798
1703
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2458
2311
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149323
149208
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
326027
325912
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
280141
280026
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
139529
139414
Original file line number Diff line number Diff line change
@@ -1 +1 @@
297501
297386
Original file line number Diff line number Diff line change
@@ -1 +1 @@
502
429
Original file line number Diff line number Diff line change
@@ -1 +1 @@
638
564
Original file line number Diff line number Diff line change
@@ -1 +1 @@
369
339
Original file line number Diff line number Diff line change
@@ -1 +1 @@
370
340
Original file line number Diff line number Diff line change
@@ -1 +1 @@
520
441
Original file line number Diff line number Diff line change
@@ -1 +1 @@
711
654
Original file line number Diff line number Diff line change
@@ -1 +1 @@
746
709
Original file line number Diff line number Diff line change
@@ -1 +1 @@
398
364
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19986
19749
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
181644
181554
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
135933
135843
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115761
115671
Original file line number Diff line number Diff line change
@@ -1 +1 @@
102765
102698
2 changes: 1 addition & 1 deletion .forge-snapshots/simple addLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
165257
165190
Original file line number Diff line number Diff line change
@@ -1 +1 @@
96504
96449
2 changes: 1 addition & 1 deletion .forge-snapshots/simple removeLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
88544
88489
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap with native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
114924
114774
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130089
129939
2 changes: 1 addition & 1 deletion .forge-snapshots/swap CA fee on unspecified.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179527
179377
Original file line number Diff line number Diff line change
@@ -1 +1 @@
110279
110170
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121626
121517
2 changes: 1 addition & 1 deletion .forge-snapshots/swap burn 6909 for input.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133455
133368
2 changes: 1 addition & 1 deletion .forge-snapshots/swap burn native 6909 for input.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
122743
122638
2 changes: 1 addition & 1 deletion .forge-snapshots/swap mint native output as 6909.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
144531
144382
2 changes: 1 addition & 1 deletion .forge-snapshots/swap mint output as 6909.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
161161
161011
Original file line number Diff line number Diff line change
@@ -1 +1 @@
217571
217312
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with dynamic fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
145362
145212
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
138626
138517
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with lp fee and protocol fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
176624
176478
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with return dynamic fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
152398
152248
2 changes: 1 addition & 1 deletion .forge-snapshots/update dynamic fee in before swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
155040
154890
27 changes: 27 additions & 0 deletions src/libraries/FullMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,33 @@ library FullMath {
}
}

/// @notice Calculates a * b / 2^96 with full precision.
/// @param a The multiplicand
/// @param b The multiplier
/// @return result The 256-bit result
function mulDivQ96(uint256 a, uint256 b) internal pure returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
// 512-bit multiply `[prod1 prod0] = a * b`.
// Compute the product mod `2**256` and mod `2**256 - 1`
// then use the Chinese Remainder Theorem to reconstruct
// the 512 bit result. The result is stored in two 256
// variables such that `product = prod1 * 2**256 + prod0`.

// Least significant 256 bits of the product.
let prod0 := mul(a, b)
let mm := mulmod(a, b, not(0))
// Most significant 256 bits of the product.
let prod1 := sub(mm, add(prod0, lt(mm, prod0)))

// Make sure the result is less than `2**256`.
if shr(96, prod1) { revert(0, 0) }

// Divide [prod1 prod0] by 2^96.
result := or(shr(96, prod0), shl(160, prod1))
}
}

/// @notice Calculates ceil(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
/// @param a The multiplicand
/// @param b The multiplier
Expand Down
Loading
Loading