Skip to content

Commit

Permalink
Optimize sqrtPriceX96 rounding in TickMath.getSqrtPriceAtTick (#665)
Browse files Browse the repository at this point in the history
* Refactor `price` calculation in `TickMath`

TickMath's price calculation has been updated for scenarios where tick is greater than zero. This change was made by replacing the earlier calculation with an assembly block to improve efficiency and performance. Additionally, a rounding operation was maintained to ensure conformity with the 160 bit input constraint.

* Optimize `sqrtPriceX96` rounding in `TickMath.getSqrtPriceAtTick`

In the TickMath.sol file, the square root price calculation has been optimized using assembly for better performance. The new implementation improves computational efficiency by replacing the standard division operation with bit shifting and bitwise operations.

* Combine assembly blocks
  • Loading branch information
shuhuiluo authored May 20, 2024
1 parent 48c8986 commit e3bef1e
Show file tree
Hide file tree
Showing 32 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/TickMathGetSqrtPriceAtTick.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
87496
80082
2 changes: 1 addition & 1 deletion .forge-snapshots/TickMathGetTickAtSqrtPrice.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
233945
227576
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153643
153494
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
331309
331160
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 @@
286302
286153
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 @@
143853
143704
Original file line number Diff line number Diff line change
@@ -1 +1 @@
301821
301672
2 changes: 1 addition & 1 deletion .forge-snapshots/initialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
61862
61811
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21709
21667
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
186775
186626
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 @@
123143
122994
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 @@
119930
119781
Original file line number Diff line number Diff line change
@@ -1 +1 @@
104932
104808
2 changes: 1 addition & 1 deletion .forge-snapshots/simple addLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
167424
167300
Original file line number Diff line number Diff line change
@@ -1 +1 @@
98529
98405
2 changes: 1 addition & 1 deletion .forge-snapshots/simple removeLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
90569
90445
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 @@
116827
116663
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131988
131824
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 @@
183126
182962
Original file line number Diff line number Diff line change
@@ -1 +1 @@
112548
112435
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123891
123778
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 @@
135923
135785
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 @@
125140
125038
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 @@
147180
147042
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 @@
163842
163678
Original file line number Diff line number Diff line change
@@ -1 +1 @@
222199
221922
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 @@
148043
147879
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123903
123790
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 @@
180223
180059
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 @@
155902
155738
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 @@
158505
158341
13 changes: 8 additions & 5 deletions src/libraries/TickMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,15 @@ library TickMath {
if (absTick & 0x40000 != 0) price = (price * 0x2216e584f5fa1ea926041bedfe98) >> 128;
if (absTick & 0x80000 != 0) price = (price * 0x48a170391f7dc42444e8fa2) >> 128;

if (tick > 0) price = type(uint256).max / price;
assembly {
// if (tick > 0) price = type(uint256).max / price;
if sgt(tick, 0) { price := div(not(0), price) }

// this divides by 1<<32 rounding up to go from a Q128.128 to a Q128.96.
// we then downcast because we know the result always fits within 160 bits due to our tick input constraint
// we round up in the division so getTickAtSqrtPrice of the output price is always consistent
sqrtPriceX96 = uint160((price >> 32) + (price & ((1 << 32) - 1) == 0 ? 0 : 1));
// this divides by 1<<32 rounding up to go from a Q128.128 to a Q128.96.
// we then downcast because we know the result always fits within 160 bits due to our tick input constraint
// we round up in the division so getTickAtSqrtPrice of the output price is always consistent
sqrtPriceX96 := shr(32, add(price, sub(shl(32, 1), 1)))
}
}
}

Expand Down

0 comments on commit e3bef1e

Please sign in to comment.