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 TickBitmap.flipTick using assembly #653

Merged
merged 7 commits into from
May 18, 2024

Conversation

shuhuiluo
Copy link
Contributor

Related Issue

Which issue does this pull request resolve?

Description of changes

This pull request is a part of #560 in an effort to ease the review process. It focuses on the flipTick function in the TickBitmap library.

@shuhuiluo shuhuiluo mentioned this pull request May 15, 2024
Copy link
Contributor

@hensha256 hensha256 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it! Just a couple of things :)

src/libraries/TickBitmap.sol Show resolved Hide resolved
test/libraries/TickBitmap.t.sol Show resolved Hide resolved
vm.expectRevert(abi.encodeWithSelector(TickBitmap.TickMisaligned.selector, tick, tickSpacing));
bitmap.flipTick(tick, tickSpacing);
} else {
bool initialized = isInitialized(tick, tickSpacing);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think initialized is always going to be false because the bitmap is empty at the beginning. So this test is only testing false->true flipping.
Should be enough just to add another 2 lines where you call flip again and check it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

function isInitialized(int24 tick, int24 tickSpacing) internal view returns (bool) {
unchecked {
int24 compressed = tick / tickSpacing;
if (tick < 0 && tick % tickSpacing != 0) compressed--; // round towards negative infinity
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you have to subtract 1? why cant it just be the solidity that was previously in TickBitmap?

(int16 wordPos, uint8 bitPos) = position(tick / tickSpacing);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the first two lines of nextInitializedTickWithinOneWord, which I factored out to a compress function in #654.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but for flipTick theres a requirement that (tick % tickSpacing != 0) otherwise it reverts, so this line feels out of place here? Not sure what I'm missing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean isInitialized should return false instead of rounding if tick % tickSpacing != 0?

A new fuzz test function `test_fuzz_flipTick` has been added to the TickBitmap library for improved testing accuracy. Additionally, the `isInitialized` function has been re-implemented to enhance its precision and correctness.
A refactoring was done in the TickBitmap test file which includes changing import paths and renaming a test function. A new fuzz test case on testing the next initialized tick within one word, but on an empty bitmap, was also added. This contributes to verifying the accuracy of tick operations under more extensive scenarios.
@hensha256 hensha256 merged commit 2df9bb3 into Uniswap:main May 18, 2024
5 of 6 checks passed
@shuhuiluo shuhuiluo deleted the tickbitmap-fliptick branch May 18, 2024 22:12
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