Skip to content

Commit

Permalink
optimize UnsafeMath#divRoundingUp (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed Mar 31, 2021
1 parent 2dc1eb9 commit 705be1e
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 73 deletions.
2 changes: 2 additions & 0 deletions contracts/libraries/SqrtPriceMath.sol
Expand Up @@ -161,6 +161,8 @@ library SqrtPriceMath {
uint256 numerator1 = uint256(liquidity) << FixedPoint96.RESOLUTION;
uint256 numerator2 = sqrtRatioBX96 - sqrtRatioAX96;

require(sqrtRatioAX96 > 0);

return
roundUp
? UnsafeMath.divRoundingUp(
Expand Down
7 changes: 4 additions & 3 deletions contracts/libraries/UnsafeMath.sol
Expand Up @@ -5,12 +5,13 @@ pragma solidity >=0.5.0;
/// @notice Contains methods that perform common math functions but do not do any overflow or underflow checks
library UnsafeMath {
/// @notice Returns ceil(x / y)
/// @dev panics if y == 0
/// @dev division by 0 has unspecified behavior, and must be checked externally
/// @param x The dividend
/// @param y The divisor
/// @return z The quotient, ceil(x / y)
function divRoundingUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
// addition is safe because (type(uint256).max / 1) + (type(uint256).max % 1 > 0 ? 1 : 0) == type(uint256).max
z = (x / y) + (x % y > 0 ? 1 : 0);
assembly {
z := add(div(x, y), gt(mod(x, y), 0))
}
}
}
10 changes: 5 additions & 5 deletions test/__snapshots__/SqrtPriceMath.spec.ts.snap
@@ -1,17 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`SqrtPriceMath #getAmount0Delta gas cost for amount0 where roundUp = true 1`] = `657`;
exports[`SqrtPriceMath #getAmount0Delta gas cost for amount0 where roundUp = true 1`] = `610`;

exports[`SqrtPriceMath #getAmount0Delta gas cost for amount0 where roundUp = true 2`] = `452`;
exports[`SqrtPriceMath #getAmount0Delta gas cost for amount0 where roundUp = true 2`] = `478`;

exports[`SqrtPriceMath #getAmount1Delta gas cost for amount0 where roundUp = false 1`] = `452`;
exports[`SqrtPriceMath #getAmount1Delta gas cost for amount0 where roundUp = false 1`] = `478`;

exports[`SqrtPriceMath #getAmount1Delta gas cost for amount0 where roundUp = true 1`] = `657`;
exports[`SqrtPriceMath #getAmount1Delta gas cost for amount0 where roundUp = true 1`] = `610`;

exports[`SqrtPriceMath #getNextSqrtPriceFromInput zeroForOne = false gas 1`] = `536`;

exports[`SqrtPriceMath #getNextSqrtPriceFromInput zeroForOne = true gas 1`] = `753`;

exports[`SqrtPriceMath #getNextSqrtPriceFromOutput zeroForOne = false gas 1`] = `848`;

exports[`SqrtPriceMath #getNextSqrtPriceFromOutput zeroForOne = true gas 1`] = `514`;
exports[`SqrtPriceMath #getNextSqrtPriceFromOutput zeroForOne = true gas 1`] = `441`;
16 changes: 8 additions & 8 deletions test/__snapshots__/SwapMath.spec.ts.snap
@@ -1,17 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`SwapMath #computeSwapStep gas swap one for zero exact in capped 1`] = `2077`;
exports[`SwapMath #computeSwapStep gas swap one for zero exact in capped 1`] = `2103`;

exports[`SwapMath #computeSwapStep gas swap one for zero exact in partial 1`] = `2776`;
exports[`SwapMath #computeSwapStep gas swap one for zero exact in partial 1`] = `2802`;

exports[`SwapMath #computeSwapStep gas swap one for zero exact out capped 1`] = `1829`;
exports[`SwapMath #computeSwapStep gas swap one for zero exact out capped 1`] = `1855`;

exports[`SwapMath #computeSwapStep gas swap one for zero exact out partial 1`] = `2776`;
exports[`SwapMath #computeSwapStep gas swap one for zero exact out partial 1`] = `2802`;

exports[`SwapMath #computeSwapStep gas swap zero for one exact in capped 1`] = `2151`;
exports[`SwapMath #computeSwapStep gas swap zero for one exact in capped 1`] = `2104`;

exports[`SwapMath #computeSwapStep gas swap zero for one exact in partial 1`] = `3200`;
exports[`SwapMath #computeSwapStep gas swap zero for one exact in partial 1`] = `3106`;

exports[`SwapMath #computeSwapStep gas swap zero for one exact out capped 1`] = `1903`;
exports[`SwapMath #computeSwapStep gas swap zero for one exact out capped 1`] = `1856`;

exports[`SwapMath #computeSwapStep gas swap zero for one exact out partial 1`] = `3200`;
exports[`SwapMath #computeSwapStep gas swap zero for one exact out partial 1`] = `3106`;
6 changes: 3 additions & 3 deletions test/__snapshots__/UniswapV3Factory.spec.ts.snap
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`UniswapV3Factory #createPool gas 1`] = `4515505`;
exports[`UniswapV3Factory #createPool gas 1`] = `4510482`;

exports[`UniswapV3Factory factory bytecode size 1`] = `24345`;
exports[`UniswapV3Factory factory bytecode size 1`] = `24320`;

exports[`UniswapV3Factory pool bytecode size 1`] = `21922`;
exports[`UniswapV3Factory pool bytecode size 1`] = `21897`;
108 changes: 54 additions & 54 deletions test/__snapshots__/UniswapV3Pool.gas.spec.ts.snap
@@ -1,20 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`UniswapV3Pool gas tests fee is off #burn above current price burn entire position after some time passes 1`] = `56987`;
exports[`UniswapV3Pool gas tests fee is off #burn above current price burn entire position after some time passes 1`] = `57000`;

exports[`UniswapV3Pool gas tests fee is off #burn above current price burn when only position using ticks 1`] = `56987`;
exports[`UniswapV3Pool gas tests fee is off #burn above current price burn when only position using ticks 1`] = `57000`;

exports[`UniswapV3Pool gas tests fee is off #burn above current price entire position burn but other positions are using the ticks 1`] = `77279`;
exports[`UniswapV3Pool gas tests fee is off #burn above current price entire position burn but other positions are using the ticks 1`] = `77305`;

exports[`UniswapV3Pool gas tests fee is off #burn above current price partial position burn 1`] = `92279`;
exports[`UniswapV3Pool gas tests fee is off #burn above current price partial position burn 1`] = `92305`;

exports[`UniswapV3Pool gas tests fee is off #burn around current price burn entire position after some time passes 1`] = `75499`;
exports[`UniswapV3Pool gas tests fee is off #burn around current price burn entire position after some time passes 1`] = `75512`;

exports[`UniswapV3Pool gas tests fee is off #burn around current price burn when only position using ticks 1`] = `72195`;
exports[`UniswapV3Pool gas tests fee is off #burn around current price burn when only position using ticks 1`] = `72208`;

exports[`UniswapV3Pool gas tests fee is off #burn around current price entire position burn but other positions are using the ticks 1`] = `88892`;
exports[`UniswapV3Pool gas tests fee is off #burn around current price entire position burn but other positions are using the ticks 1`] = `88918`;

exports[`UniswapV3Pool gas tests fee is off #burn around current price partial position burn 1`] = `103892`;
exports[`UniswapV3Pool gas tests fee is off #burn around current price partial position burn 1`] = `103918`;

exports[`UniswapV3Pool gas tests fee is off #burn below current price burn entire position after some time passes 1`] = `69823`;

Expand All @@ -30,21 +30,21 @@ exports[`UniswapV3Pool gas tests fee is off #increaseObservationCardinalityNext

exports[`UniswapV3Pool gas tests fee is off #increaseObservationCardinalityNext no op 1`] = `28277`;

exports[`UniswapV3Pool gas tests fee is off #mint above current price add to position after some time passes 1`] = `108096`;
exports[`UniswapV3Pool gas tests fee is off #mint above current price add to position after some time passes 1`] = `108049`;

exports[`UniswapV3Pool gas tests fee is off #mint above current price add to position existing 1`] = `108096`;
exports[`UniswapV3Pool gas tests fee is off #mint above current price add to position existing 1`] = `108049`;

exports[`UniswapV3Pool gas tests fee is off #mint above current price new position mint first in range 1`] = `176478`;
exports[`UniswapV3Pool gas tests fee is off #mint above current price new position mint first in range 1`] = `176431`;

exports[`UniswapV3Pool gas tests fee is off #mint above current price second position in same range 1`] = `123096`;
exports[`UniswapV3Pool gas tests fee is off #mint above current price second position in same range 1`] = `123049`;

exports[`UniswapV3Pool gas tests fee is off #mint around current price add to position after some time passes 1`] = `159795`;
exports[`UniswapV3Pool gas tests fee is off #mint around current price add to position after some time passes 1`] = `159748`;

exports[`UniswapV3Pool gas tests fee is off #mint around current price add to position existing 1`] = `148987`;
exports[`UniswapV3Pool gas tests fee is off #mint around current price add to position existing 1`] = `148940`;

exports[`UniswapV3Pool gas tests fee is off #mint around current price new position mint first in range 1`] = `308610`;
exports[`UniswapV3Pool gas tests fee is off #mint around current price new position mint first in range 1`] = `308563`;

exports[`UniswapV3Pool gas tests fee is off #mint around current price second position in same range 1`] = `163987`;
exports[`UniswapV3Pool gas tests fee is off #mint around current price second position in same range 1`] = `163940`;

exports[`UniswapV3Pool gas tests fee is off #mint below current price add to position after some time passes 1`] = `108641`;

Expand All @@ -56,43 +56,43 @@ exports[`UniswapV3Pool gas tests fee is off #mint below current price second pos

exports[`UniswapV3Pool gas tests fee is off #poke best case 1`] = `42584`;

exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 first swap in block moves tick, no initialized crossings 1`] = `118475`;
exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 first swap in block moves tick, no initialized crossings 1`] = `118334`;

exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 first swap in block with no tick movement 1`] = `104442`;
exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 first swap in block with no tick movement 1`] = `104348`;

exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 first swap in block, large swap crossing a single initialized tick 1`] = `120839`;
exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 first swap in block, large swap crossing a single initialized tick 1`] = `120651`;

exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 first swap in block, large swap crossing several initialized ticks 1`] = `136510`;
exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 first swap in block, large swap crossing several initialized ticks 1`] = `136181`;

exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 first swap in block, large swap, no initialized crossings 1`] = `130580`;
exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 first swap in block, large swap, no initialized crossings 1`] = `130345`;

exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 large swap crossing several initialized ticks after some time passes (seconds outside is set) 1`] = `151510`;
exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 large swap crossing several initialized ticks after some time passes (seconds outside is set) 1`] = `151181`;

exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 large swap crossing several initialized ticks second time after some time passes 1`] = `211510`;
exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 large swap crossing several initialized ticks second time after some time passes 1`] = `211181`;

exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 second swap in block moves tick, no initialized crossings 1`] = `118475`;
exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 second swap in block moves tick, no initialized crossings 1`] = `118334`;

exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 second swap in block with no tick movement 1`] = `104553`;
exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 second swap in block with no tick movement 1`] = `104459`;

exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 second swap in block, large swap crossing a single initialized tick 1`] = `108447`;
exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 second swap in block, large swap crossing a single initialized tick 1`] = `108306`;

exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 second swap in block, large swap crossing several initialized ticks 1`] = `124097`;
exports[`UniswapV3Pool gas tests fee is off #swapExact0For1 second swap in block, large swap crossing several initialized ticks 1`] = `123815`;

exports[`UniswapV3Pool gas tests fee is on #burn above current price burn entire position after some time passes 1`] = `56987`;
exports[`UniswapV3Pool gas tests fee is on #burn above current price burn entire position after some time passes 1`] = `57000`;

exports[`UniswapV3Pool gas tests fee is on #burn above current price burn when only position using ticks 1`] = `56987`;
exports[`UniswapV3Pool gas tests fee is on #burn above current price burn when only position using ticks 1`] = `57000`;

exports[`UniswapV3Pool gas tests fee is on #burn above current price entire position burn but other positions are using the ticks 1`] = `77279`;
exports[`UniswapV3Pool gas tests fee is on #burn above current price entire position burn but other positions are using the ticks 1`] = `77305`;

exports[`UniswapV3Pool gas tests fee is on #burn above current price partial position burn 1`] = `92279`;
exports[`UniswapV3Pool gas tests fee is on #burn above current price partial position burn 1`] = `92305`;

exports[`UniswapV3Pool gas tests fee is on #burn around current price burn entire position after some time passes 1`] = `75499`;
exports[`UniswapV3Pool gas tests fee is on #burn around current price burn entire position after some time passes 1`] = `75512`;

exports[`UniswapV3Pool gas tests fee is on #burn around current price burn when only position using ticks 1`] = `72195`;
exports[`UniswapV3Pool gas tests fee is on #burn around current price burn when only position using ticks 1`] = `72208`;

exports[`UniswapV3Pool gas tests fee is on #burn around current price entire position burn but other positions are using the ticks 1`] = `88892`;
exports[`UniswapV3Pool gas tests fee is on #burn around current price entire position burn but other positions are using the ticks 1`] = `88918`;

exports[`UniswapV3Pool gas tests fee is on #burn around current price partial position burn 1`] = `103892`;
exports[`UniswapV3Pool gas tests fee is on #burn around current price partial position burn 1`] = `103918`;

exports[`UniswapV3Pool gas tests fee is on #burn below current price burn entire position after some time passes 1`] = `69823`;

Expand All @@ -108,21 +108,21 @@ exports[`UniswapV3Pool gas tests fee is on #increaseObservationCardinalityNext g

exports[`UniswapV3Pool gas tests fee is on #increaseObservationCardinalityNext no op 1`] = `28277`;

exports[`UniswapV3Pool gas tests fee is on #mint above current price add to position after some time passes 1`] = `108096`;
exports[`UniswapV3Pool gas tests fee is on #mint above current price add to position after some time passes 1`] = `108049`;

exports[`UniswapV3Pool gas tests fee is on #mint above current price add to position existing 1`] = `108096`;
exports[`UniswapV3Pool gas tests fee is on #mint above current price add to position existing 1`] = `108049`;

exports[`UniswapV3Pool gas tests fee is on #mint above current price new position mint first in range 1`] = `176478`;
exports[`UniswapV3Pool gas tests fee is on #mint above current price new position mint first in range 1`] = `176431`;

exports[`UniswapV3Pool gas tests fee is on #mint above current price second position in same range 1`] = `123096`;
exports[`UniswapV3Pool gas tests fee is on #mint above current price second position in same range 1`] = `123049`;

exports[`UniswapV3Pool gas tests fee is on #mint around current price add to position after some time passes 1`] = `159795`;
exports[`UniswapV3Pool gas tests fee is on #mint around current price add to position after some time passes 1`] = `159748`;

exports[`UniswapV3Pool gas tests fee is on #mint around current price add to position existing 1`] = `148987`;
exports[`UniswapV3Pool gas tests fee is on #mint around current price add to position existing 1`] = `148940`;

exports[`UniswapV3Pool gas tests fee is on #mint around current price new position mint first in range 1`] = `308610`;
exports[`UniswapV3Pool gas tests fee is on #mint around current price new position mint first in range 1`] = `308563`;

exports[`UniswapV3Pool gas tests fee is on #mint around current price second position in same range 1`] = `163987`;
exports[`UniswapV3Pool gas tests fee is on #mint around current price second position in same range 1`] = `163940`;

exports[`UniswapV3Pool gas tests fee is on #mint below current price add to position after some time passes 1`] = `108641`;

Expand All @@ -134,24 +134,24 @@ exports[`UniswapV3Pool gas tests fee is on #mint below current price second posi

exports[`UniswapV3Pool gas tests fee is on #poke best case 1`] = `42584`;

exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block moves tick, no initialized crossings 1`] = `124662`;
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block moves tick, no initialized crossings 1`] = `124521`;

exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block with no tick movement 1`] = `110482`;
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block with no tick movement 1`] = `110388`;

exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block, large swap crossing a single initialized tick 1`] = `127173`;
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block, large swap crossing a single initialized tick 1`] = `126985`;

exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block, large swap crossing several initialized ticks 1`] = `143285`;
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block, large swap crossing several initialized ticks 1`] = `142956`;

exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block, large swap, no initialized crossings 1`] = `137061`;
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block, large swap, no initialized crossings 1`] = `136826`;

exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 large swap crossing several initialized ticks after some time passes (seconds outside is set) 1`] = `158285`;
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 large swap crossing several initialized ticks after some time passes (seconds outside is set) 1`] = `157956`;

exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 large swap crossing several initialized ticks second time after some time passes 1`] = `218285`;
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 large swap crossing several initialized ticks second time after some time passes 1`] = `217956`;

exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 second swap in block moves tick, no initialized crossings 1`] = `124662`;
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 second swap in block moves tick, no initialized crossings 1`] = `124521`;

exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 second swap in block with no tick movement 1`] = `110593`;
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 second swap in block with no tick movement 1`] = `110499`;

exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 second swap in block, large swap crossing a single initialized tick 1`] = `114634`;
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 second swap in block, large swap crossing a single initialized tick 1`] = `114493`;

exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 second swap in block, large swap crossing several initialized ticks 1`] = `130725`;
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 second swap in block, large swap crossing several initialized ticks 1`] = `130443`;

0 comments on commit 705be1e

Please sign in to comment.