Skip to content

gas: add unchecked for ConduitController#updateChannel - #440

Merged
0age merged 2 commits into
ProjectOpenSea:mingwatch-gas-optimization-1from
mingwatch:mingwatch-gas-optimization-1
Jun 8, 2022
Merged

gas: add unchecked for ConduitController#updateChannel#440
0age merged 2 commits into
ProjectOpenSea:mingwatch-gas-optimization-1from
mingwatch:mingwatch-gas-optimization-1

Conversation

@mingwatch

Copy link
Copy Markdown
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.
Comment thread contracts/conduit/ConduitController.sol Outdated
Co-authored-by: Ryan Ghods <ralxzryan@gmail.com>
uint256 removedChannelIndex;
// channelPreviouslyOpen already ensures channelIndexPlusOne > 0
unchecked {
removedChannelIndex = channelIndexPlusOne - 1;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This looks safe

Copy link
Copy Markdown

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