Skip to content

gas: add unchecked for ConduitController#updateChannel #440

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

Conversation

mingwatch
Copy link
Contributor

Motivation

For the arithmetic operations that will never over/underflow, using the unchecked directive (Solidity v0.8 has default overflow/underflow checks) can save some gas from the unnecessary internal over/underflow checks.

In ConduitController.sol#updateChannel(), when channelPreviouslyOpen is true means that channelIndexPlusOne != 0, and channelIndexPlusOne is uint256, so channelIndexPlusOne must >= 1, then channelIndexPlusOne - 1 will never underflow.

Solution

add unchecked for that case

`channelPreviouslyOpen` is `true` means that `channelIndexPlusOne != 0`, and `channelIndexPlusOne` is `uint256`, then `channelIndexPlusOne - 1` will never underflow.
uint256 removedChannelIndex;
// channelPreviouslyOpen already ensures channelIndexPlusOne > 0
unchecked {
removedChannelIndex = channelIndexPlusOne - 1;

Choose a reason for hiding this comment

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

This looks safe

Choose a reason for hiding this comment

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

To clarify: this is safe.
In this branch, channelPreviouslyOpen must be true, which means that channelIndexPlusOne != 0. Since it has type uint, it must be > 0 therefore safe.

@0age 0age changed the base branch from main to mingwatch-gas-optimization-1 June 8, 2022 20:39
@0age 0age merged commit 43467e5 into ProjectOpenSea:mingwatch-gas-optimization-1 Jun 8, 2022
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.

4 participants