Skip to content

Update mask calculation on TickBitmap::nextInitializedTickWithinOneWord #978

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

Closed
wants to merge 1 commit into 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.
Copy link

stale bot commented Jul 15, 2024

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.

@stale stale bot added the wontfix label Jul 15, 2024
@stale stale bot closed this Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants