Uniswap / v3-core

🦄 🦄 🦄 Core smart contracts of Uniswap v3
https://uniswap.org
Other
4.38k stars 2.68k forks source link

Update mask calculation on TickBitmap::nextInitializedTickWithinOneWord #978

Closed ParsaAminpour closed 2 months ago

ParsaAminpour commented 6 months ago

I calculated mask opcode by opcode and realized that calculating mask in this way is slightly gas-efficient. Based on these test cases

function testMaskAreEqual() public view {
    uint8 bitPos = 8;
    // mask1 function is the calculation that you are currently using.
    uint256 mask1_calculated = mask_cal.mask1(bitPos);
    // mask2 function is the efficient way.
    uint256 mask2_calculated = mask_cal.mask2(bitPos);
    assertEq(mask1_calculated, mask2_calculated);
}

function testMask1() public view {
    uint8 bitPos = 8;
    uint256 mask = mask_cal.mask1(bitPos);
    console.log(mask);
}

function testMask2() public view {
    uint8 bitPos = 8;
    uint256 mask = mask_cal.mask2(bitPos);
    console.log(mask);
}
The actual functions' code ```solidity function mask1(uint8 bitPos) external pure returns(uint256 mask) { mask = (1 << bitPos) - 1 + (1 << bitPos); } function mask2(uint8 bitPos) external pure returns(uint256 mask) { mask = (1 << (bitPos+1)) - 1; } ```

And the result is (via foundry):

result

As you can see, there is a small difference in gas consumption between these two functions.

stale[bot] commented 2 months ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.