Platform: Code4rena
Start Date: 07/06/2022
Pot Size: $75,000 USDC
Total HM: 11
Participants: 77
Period: 7 days
Judge: gzeon
Total Solo HM: 7
Id: 124
League: ETH
Rank: 69/77
Findings: 1
Award: $47.60
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 Selected for report: IllIllI
Also found by: 0v3rf10w, 0x1f8b, 0x29A, 0xKitsune, 0xNazgul, 0xSolus, 0xf15ers, 0xkatana, 0xmint, 8olidity, Chom, Cityscape, DavidGialdi, Deivitto, ElKu, Fitraldys, Funen, GreyArt, Lambda, Meera, Picodes, PierrickGT, Sm4rty, Tadashi, TerrierLover, TomJ, Tomio, UnusualTurtle, Waze, _Adam, antonttc, asutorufos, berndartmueller, c3phas, catchup, csanuragjain, delfin454000, djxploit, ellahi, fatherOfBlocks, hake, hansfriese, hyh, joestakey, kaden, minhquanym, oyc_109, rfa, sach1r0, saian, samruna, simon135, slywaters, ynnad
47.6016 USDC - $47.60
Starting in L#596 in the _getFCashPositions()
, there's no need to repeat the same loop twice as it's unecessary. Instead, the second loop should be only used to allocate the correct memory space to the fCashPositions
variable for only the amount of addresses it's going to store already calculated previously with j
.
This is my proposed solution:
function _getFCashPositions(ISetToken _setToken) internal view returns(address[] memory fCashPositions) { ISetToken.Position[] memory positions = _setToken.getPositions(); uint positionsLength = positions.length; address[] memory tmp = new address[](positionsLength); uint j; for(uint256 i = 0; i < positionsLength; i++) { // Check that the given position is an equity position if(positions[i].unit > 0) { address component = positions[i].component; if(_isWrappedFCash(component)) { tmp[j] = component; j++; } } } // Write to memory with correct space fCashPositions = new address[](j); for(uint256 i = 0; i < j; i++) { fCashPositions[i] = tmp[i]; } }
#0 - ckoopmann
2022-06-17T03:00:39Z
Interesting suggestion, that I don't remember seeing in any of the other issues so far. Will try this out, and probably apply if it passes tests and provides gas savings. (which it should since it avoids calling the rather expensive _isWrappedfCash method again).
Nice catch 👍
#1 - ckoopmann
2022-06-17T03:01:01Z
I guess this could also be considered a Gas optimization report.