Closed jayden-sudo closed 9 months ago
This is ok for me, just a question for the switch case intead of nested if, did u test a positive impact of the gas cost ? I remember avoiding them due to lesser efficiency?
This is ok for me, just a question for the switch case intead of nested if, did u test a positive impact of the gas cost ? I remember avoiding them due to lesser efficiency?
Using switch
may lead to higher gas consumption, but it's not entirely accurate. For example, consider the following program:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.21;
contract Test {
function testSwitch(uint256 i) public view returns (uint256 res){
uint256 gas = gasleft();
assembly{
switch i
case 1 {
i := add(i,2)
}
case 2 {
i := add(i,3)
}
case 3 {
i := add(i,4)
}
}
return gas - gasleft();
}
function testIF(uint256 i) public view returns (uint256 res){
uint256 gas = gasleft();
assembly{
if eq(i,1){
i := add(i,2)
}
if eq(i,2){
i := add(i,3)
}
if eq(i,3){
i := add(i,4)
}
}
return gas - gasleft();
}
}
When viaIR
is false
and optimizer
is false
:
i = 1
, testSwitch = 61
gas, testIf = 104
gasi = 2
, testSwitch = 83
gas, testIf = 90
gasi = 3
, testSwitch = 94
gas, testIf = 90
gasWhen viaIR
is true
and optimizer
is false
:
i = 1
, testSwitch = 53
gas, testIf = 112
gasi = 2
, testSwitch = 75
gas, testIf = 94
gasi = 3
, testSwitch = 92
gas, testIf = 94
gasEspecially when viaIR
is true
, using switch
often results in higher gas efficiency. In fact, the gas efficiency improvement in this PR mainly relies on this change.
But this is indeed confusing. If the first if
condition can be satisfied, then using switch
is clearly superior. If there are many branches in the switch
, and often the if
conditions are only satisfied later, using if
might be better (of course, in such cases, we can optimize by reorder the conditions to prioritize the most frequently hit one at the top of the switch
).
Please provide two distincts PR, with and without the switch case. view one is ok, switch will depend on the concrete bench (not toy example).
Please provide two distincts PR, with and without the switch case. view one is ok, switch will depend on the concrete bench (not toy example).
Okay, I'll submit it soon 🫡
Please provide two distincts PR, with and without the switch case. view one is ok, switch will depend on the concrete bench (not toy example).
without 'switch': #22 , only 0.3kgas saved. (hope this PR helps you complete the gas efficiency comparison)
Closed with adoption of same contributor no switch PR version. (switch case only PR can be submitted now).
Closed with adoption of same contributor no switch PR version. (switch case only PR can be submitted now).
Thank you. Honestly, changing "if" to "switch" and saving 2K gas perplexed me as well. Before submitting the PR( if -> switch), I'll examine the compiled OP code, identify the reasons for saving 2K gas, ensure that this gas efficiency improvement is sustainable, and provide detailed explanations for the gas efficiency improvements in the PR.
With the code equivalent, executing
FCL_Elliptic_ZZ.ecdsa_verify
reduced gas cost by 2600 gas:Gas Optimization Records for ecdsa_verify:
Changed from calling ModExp to staticcall ModExp, reducing the number of parameters.
view
.Replaced
uint256[6] memory pointer
withpointer := mload(0x40)
, reducing calls likeMSTORE(0x40)
.Performed assembly optimizations.
Calldata cache, avoid frequent call of calldataload