Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas Optimizations #2096

Open
codehawks-bot opened this issue Aug 8, 2023 · 0 comments
Open

Gas Optimizations #2096

codehawks-bot opened this issue Aug 8, 2023 · 0 comments

Comments

@codehawks-bot
Copy link

Gas Optimizations

Severity

Gas Optimization / Informational

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants