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

Optimizing ScrollChain._popL1Messages #71

Open
barakman opened this issue Dec 31, 2024 · 0 comments
Open

Optimizing ScrollChain._popL1Messages #71

barakman opened this issue Dec 31, 2024 · 0 comments

Comments

@barakman
Copy link

barakman commented Dec 31, 2024

On the one hand, this function looks as if it was written with gas-optimization in mind.
On the other hand, it looks like the optimization was rather poorly applied.

First off all, instead of checking the value of isCalldata on every iteration, it makes since to split this function into two functions, one for the case of isCalldata = 1 and another for all other cases.
Alternatively, you could run that check outside the loop, and then implement two separate loops (one for each case).

Second, the logic around the calculation of _count seems like something which can be done once instead of on every iteration.
You can do this by splitting the loop into two parts:

  1. A loop over all 256-length chunks
  2. A final iteration for the last chunk (if larger than 0)

For example:

unchecked {
    uint256 numOfChunks = totalL1MessagesPoppedInBatch / 256;
    for (uint256 i = 0; i < numOfChunks; ++i) {
        IL1MessageQueue(messageQueue).popCrossDomainMessage(startIndex, 256, bitmap);
        startIndex += 256;
    }
    uint256 _lastChunkSize = totalL1MessagesPoppedInBatch % 256;
    if (_lastChunkSize > 0) {
        IL1MessageQueue(messageQueue).popCrossDomainMessage(startIndex, _lastChunkSize, bitmap);
    }
}
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

No branches or pull requests

1 participant