20,000 USDC
View results
Submission Details
Severity: gas

Using `calldata` instead of `memory` for `read-only` arguments in `external` functions saves gas

Summary

Using calldata instead of memory for read-only arguments in external functions saves gas

Vulnerability Details

By declaring loan as calldata, the Solidity compiler will optimize the function to directly read the Loan data from the calldata, avoiding unnecessary data copying. This can lead to gas savings, especially when the Loan struct is large, as it reduces the cost of reading data from storage.

The only time it makes sense to read the whole struct/array into a memory variable, is if the full struct/array is being returned by the function, is being passed to a function that requires memory, or if the array/struct is being read from another memory array/struct.

Instances:

  1. Lender.sol: Line 117

Impact

Huge savings in gas.

Tools Used

Manual audit

Recommendations

Declare the variable as calldata.

Support

FAQs

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