Open BurtonQin opened 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
https://github.com/Uniswap/v3-periphery/issues/351#issue-1781644099
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
with
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