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

Gas Optimizations

Using storage instead of memory for structs/arrays saves gas

When fetching data from a storage location, assigning the data to a memory variable causes all fields of the struct/array to be read from storage, which incurs a Gcoldsload (2100 gas) for each field of the struct/array. If the fields are read from the new memory variable, they incur an additional MLOAD rather than a cheap stack read. Instead of declaring the variable with the memory keyword, declaring the variable with the storage keyword and caching any fields that need to be re-read in stack variables, will be much cheaper, only incurring the Gcoldsload for the fields actually read.

2 instances - 1 file:

File: src/Lender.sol
117: Loan memory loan = loans[loanId];
238: Pool memory pool = pools[poolId];
296: Loan memory loan = loans[loanId];
363: Loan memory loan = loans[loanId];
367: Pool memory pool = pools[poolId];
441: Loan memory loan = loans[loanId];
467: Loan memory loan = loans[loanId];
552: Loan memory loan = loans[loanId];
606: Loan memory loan = loans[loanId];
611: Pool memory pool = pools[poolId];
720: function _calculateInterest(
721: Loan memory l
722: ) internal view returns (uint256 interest, uint256 fees) {

117
238
296
363
367
441
467
552
606
611
720-722

Recommended Changes : use storage instead of memory and cache any fields that need to be re-read in stack variables.

Support

FAQs

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