Platform: Code4rena
Start Date: 12/07/2022
Pot Size: $35,000 USDC
Total HM: 13
Participants: 78
Period: 3 days
Judge: 0xean
Total Solo HM: 6
Id: 135
League: ETH
Rank: 77/78
Findings: 1
Award: $25.71
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 Selected for report: joestakey
Also found by: 0x040, 0x1f8b, 0xDjango, 0xNazgul, 0xsam, Avci, Aymen0909, Bnke0x0, CRYP70, ElKu, Fitraldys, Funen, JC, Kaiziron, MadWookie, Meera, ReyAdmirado, Sm4rty, Soosh, TomJ, Waze, _Adam, __141345__, ajtra, benbaessler, c3phas, csanuragjain, durianSausage, exd0tpy, fatherOfBlocks, hake, ignacio, karanctf, kyteg, m_Rassska, oyc_109, rbserver, robee, rokinot, samruna, sashik_eth, simon135, slywaters
25.7062 USDC - $25.71
Hello! CRYP70 here and I appreciate the opportunity to be apart of this competition. I believe I have found something which may save you some gas over large arrays. See the details of my findings.
++i
generally costs less gas than i++
or i = i + 1
(about 5 units per increment) because i++
must increment a value and then "return" the old value which means the program may need to hold two numbers in memory. When ++i
is used, it will only ever use one number in memory.
See the example below for an simplified illustration:
pragma solidity ^0.8.13; contract MyFavouriteCounter { uint public count; function incrementPrefixCount() public returns (uint) { count = 1; return (++count); // returns 2 } function incrementPostfixCount() public returns (uint) { count = 1; return (count++); // returns 1 } }
I was able to identify this in the following locations:
Swivel.sol:100:initiate()
- unchecked {i++;}
Swivel.sol:269:exit()
- unchecked {i++;}
Swivel.sol:418:cancel()
- i++;
Swivel.sol:564:approveUnderlying()
- i++;
Remediation:
Simply using ++i
can save some gas.
Thanks again!
#0 - robrobbins
2022-08-31T20:11:38Z
yep, but there are literally dozens pointing out this same thing so...
el dupo