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

TickBitmap, SwapMath, Oracle: replace "if (A && B)" to save gas fees #656

Open
BurtonQin opened this issue Jun 30, 2023 · 0 comments
Open

Comments

@BurtonQin
Copy link

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 six such cases:

if (tick < 0 && tick % tickSpacing != 0) compressed--; // round towards negative infinity

if (!exactIn && amountOut > uint256(-amountRemaining)) {

if (exactIn && sqrtRatioNextX96 != sqrtRatioTargetX96) {

if (cardinalityNext > cardinality && index == (cardinality - 1)) {

if (a <= time && b <= time) return a <= b;

if (targetAtOrAfter && lte(time, target, atOrAfter.blockTimestamp)) break;

Replacing

if (targetAtOrAfter && lte(time, target, atOrAfter.blockTimestamp)) break;

with

if (targetAtOrAfter) {
    if (lte(time, target, atOrAfter.blockTimestamp)) {
         break;
    }
}

can save gas fees and does not hurt readability.

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