20,000 USDC
View results
Submission Details
Severity: gas
Valid

Caching the length in for loops

Summary

.length should not be looked up in every loop of a for-loop

Reading array length at each iteration of the loop takes 6 gas (three for mload and three to place memory_offset ) in the stack. Caching the array length in the stack saves around 3 gas per iteration. I suggest storing the array’s length in a variable before the for-loop.

Vulnerability Details

https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Lender.sol#L233

File: src/Lender.sol
233 for (uint256 i = 0; i < borrows.length; i++) {

https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Lender.sol#L293

File: src/Lender.sol
293 for (uint256 i = 0; i < loanIds.length; i++) {

https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Lender.sol#L359

File: src/Lender.sol
259 for (uint256 i = 0; i < loanIds.length; i++) {

https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Lender.sol#L438

File: src/Lender.sol
438 for (uint256 i = 0; i < loanIds.length; i++) {

https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Lender.sol#L549

File: src/Lender.sol
549 for (uint256 i = 0; i < loanIds.length; i++) {

https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Lender.sol#L592

File: src/Lender.sol
592 for (uint256 i = 0; i < refinances.length; i++) {

Support

FAQs

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