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

Refactor and optimizations #34

Merged
merged 17 commits into from
May 1, 2024
Merged

Conversation

shuhuiluo
Copy link
Contributor

No description provided.

@shuhuiluo shuhuiluo marked this pull request as draft April 8, 2024 02:32
@shuhuiluo shuhuiluo changed the title Refactor for readability Refactor and optimizations Apr 9, 2024
@shuhuiluo shuhuiluo marked this pull request as ready for review April 9, 2024 04:00
shuhuiluo added 16 commits May 1, 2024 14:35
`mulDiv96` method was renamed to `mulDivQ96` in multiple files to improve code clarity. The updated method naming now provides a more accurate representation of its functionality and usage within the mathematics performing code.
A new function mulDivQ128 was added to FullMath.sol to calculate a * b / 2^128 with full precision. Corresponding test cases have been added to FullMath.t.sol. The package version in package.json has been updated from 2.0.2 to 2.1.0 to reflect this enhancement. Additionally, .gas-snapshot has been adjusted accordingly.
This commit refactors the `TickBitmap` code to enhance readability. It includes wrapping some lines into `unchecked` blocks and changing the calculation of the `initialized` variable to a simple non-zero check. These changes don't affect functionality, but they make the code cleaner and easier to comprehend.
Two new functions, `absDiff` and `absDiffU160`, have been added to `TernaryLib.sol`, both of which perform absolute difference operations. The `sort2` function was also added and the `sort2` function used for sorting `uint160`s has been renamed to `sort2U160` for better distinction. All calls to `sort2` in `SqrtPriceMath.sol` have been updated to use `sort2U160` instead.
The method `absDiffU160` from the `TernaryLib` is now used in the `getAmount1Delta` function inside `SqrtPriceMath.sol`. This change has streamlined the subtraction operation that was previously done using `sort2U160` and `sub` methods. Accordingly, the gas usage for several methods in the `.gas-snapshot` file has also been updated.
Introduced new functions for calculating the amount of liquidity received for a given amount of tokens and price range. These functions, getLiquidityForAmount0Sorted and getLiquidityForAmount1Sorted, work with sorted boundaries. This change simplifies and improves the efficiency of the code by eliminating the need for boundary sort in multiple places. Updated method references in current pool price computations accordingly.
Refactor the `computeSwapStep` method in 'SwapMath.sol' to remove assembly blocks for computing 'amountRemainingAbs'. Implemented using plain Solidity for better readability, maintainability, and security.
The overflow/underflow prevention comment within the `TickBitmap.sol` file was moved for better clarity and accuracy. In `PoolCaller.sol`, a new TODO comment was added to suggest the use of `mcopy` for future data copying tasks, aiming for code optimization and efficiency improvement.
…napshot

The code was adjusted by eliminating an unnecessary sign extension inside SqrtPriceMath.sol. This results in cleaner assembly with less operations. In addition, the gas snapshot file has been updated to reflect alterations in gas usage metrics across multiple areas, showcasing the effects of the modification.
The assembly code used for handling integer sign has been updated for optimization. Changes involve replacing subtraction operations with signed bit shift for better computational performance. This alteration significantly improves the efficiency of absolute value and absolute difference operations in the Solidity library.
The process of determining the sign of 'liquidity' in the SqrtPriceMath.sol file has been simplified by directly using a mask. This modification has led to small improvements in gas consumption as reflected in the .gas-snapshot file. The performance tests showed minor variations in runtimes.
The absDiff function in TernaryLib.sol has been updated to prevent overflow of int256 when computing the difference between two uint256 values. This is achieved by handling the subtraction operation differently based on whether 'a' is greater than 'b'. Additionally, the snapshot and snapshot:diff command in package.json was simplified by removing unnecessary options (--via-ir and --evm-version cancun).
@gnarlycow gnarlycow merged commit 7f3acef into Aperture-Finance:main May 1, 2024
1 of 2 checks passed
@shuhuiluo shuhuiluo deleted the refactor branch May 1, 2024 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants