Uniswap / v3-periphery

🦄 🦄 🦄 Peripheral smart contracts for interacting with Uniswap v3
https://uniswap.org
GNU General Public License v2.0
1.15k stars 1.06k forks source link

contracts/base and libraries: replace "if (A && B)" to save gas fees #351

Open BurtonQin opened 1 year ago

BurtonQin commented 1 year ago

The solidity compiler generates more code for “if (A && B) {}” than for “if (A) {if (B) {} }”. Our experiment shows that switching from “if (A && B) {}” to “if (A) {if (B) {} }” can save gas fee.

There are five such cases:

https://github.com/Uniswap/v3-periphery/blob/6cce88e63e176af1ddb6cc56e029110289622317/contracts/base/PeripheryPayments.sol#L58

Replacing

if (token == WETH9 && address(this).balance >= value) {

with

if (token == WETH9) {
    if (address(this).balance >= value) {

can save gas fees and does not hurt code readability.

Similarly, the following code snippets can be improved:

https://github.com/Uniswap/v3-periphery/blob/6cce88e63e176af1ddb6cc56e029110289622317/contracts/libraries/NFTDescriptor.sol#L224

https://github.com/Uniswap/v3-periphery/blob/6cce88e63e176af1ddb6cc56e029110289622317/contracts/libraries/NFTDescriptor.sol#L277

https://github.com/Uniswap/v3-periphery/blob/6cce88e63e176af1ddb6cc56e029110289622317/contracts/V3Migrator.sol#L78

https://github.com/Uniswap/v3-periphery/blob/6cce88e63e176af1ddb6cc56e029110289622317/contracts/V3Migrator.sol#L91

AbulKhayer2244 commented 1 year ago

https://github.com/Uniswap/v3-periphery/issues/351#issue-1781644099