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

Gas - Use simple comparison #23

Open
kitty-the-kat opened this issue Jan 11, 2023 · 1 comment
Open

Gas - Use simple comparison #23

kitty-the-kat opened this issue Jan 11, 2023 · 1 comment

Comments

@kitty-the-kat
Copy link
Contributor

Using a compound comparison such as ≥ or ≤ uses more gas than a simple comparison check like >, <, or ==. Compound comparison operators can be replaced with simple ones for gas savings.

Technical Details

The withdraw() function in ConvexStrategy provides one example of this, but there are other instances in the contracts:

if (withdrawnAssets <= _amount) {
	loss += _amount - withdrawnAssets;
} else {
	if (loss > withdrawnAssets - _amount) {
		loss -= withdrawnAssets - _amount;
	} else {
		loss = 0;
	}
}

By switching around the if/else clauses, we can replace the compound operator with a simple one

if (withdrawnAssets > _amount) {
	if (loss > withdrawnAssets - _amount) {
		loss -= withdrawnAssets - _amount;
	} else {
		loss = 0;
	}
} else {
	loss += _amount - withdrawnAssets;
}

Impact

Gas savings.

Recommendation

Replace compound comparison operators with simple ones for gas savings.

@kitty-the-kat
Copy link
Contributor Author

acknowledged - will add in future update

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

When branches are created from issues, their pull requests are automatically linked.

1 participant