Beginner FriendlyFoundry
100 EXP
View results
Submission Details
Severity: low
Invalid

Inefficient use of variables in loops - [`players.length`]

Description:

It was noted that the for loop in ThePredicter.sol::withdraw() is looping through players.length which is stored in storage instead of memeory which is less efficient and more gas expensive. Caching storage variables used in loops helps optimize gas usage and improve the efficiency of smart contracts.

Impact:

Accessing storage variables can be gas expensive as each read operation from storage costs significantly more than compared to when reading from memory. By repeatedly accessing a storage variable within a loop, the gas costs accumulate.

Tooling:

Gas ineficiencies can commonly be detected by static code analysis tools such as Slither.

Remediation:

Copy the players.length to a memory variable before the loop to reduce the number of expensive storage reads. Following is an example of how this can be accomplished:

+ uint256 playersLength = players.length;
+ for (uint256 i = 0; i < playersLength; ++i) {
- for (uint256 i = 0; i < players.length; ++i) {
int8 cScore = scoreBoard.getPlayerScore(players[i]);
if (cScore > maxScore) maxScore = cScore;
if (cScore > 0) totalPositivePoints += cScore;
}
Updates

Lead Judging Commences

NightHawK Lead Judge about 1 year ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.