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

Update mask calculation on TickBitmap::nextInitializedTickWithinOneWord #978

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ParsaAminpour
Copy link

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
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.

I calculated mask opcode by opcode and realized that calculating mask in this way is slightly gas-efficient.
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.

None yet

2 participants