<!-- canonical: https://0xsimao.com/findings/maple-finance-iii-optimizations-load-storage-unimpaired -->

# LoanManager code optimizations: no need to load payment struct from storage if loan is unimpaired.

Low/Info · Three Sigma · Institutional lending · 10th April, 2024

Finding 3S-MAPLE-N06 of the Maple Finance security review.

- Protocol: https://maple.finance/
- Report: /reports/maple-finance-iii-2024
- Source: https://cdn.sanity.io/files/qoqld077/production/36dbe5ca76da3d2392bcee581548067705b8bd36.pdf

---

### Description

In the LoanManager contract, two changes can be made to reduce gas usage and improve
readability:
In function _accountForLoanImpairmentRemoval(): loading the payment struct from
storage should only be performed after the check to see if the loan is impaired, since the
vast majority of the time, a payment will be made to an unimpaired loan, so there is no
need to load this struct from storage as it will not be used. This change would result in a
makePayment() call cheaper by around 3000 gas.
An identical issue can be found in function _accountForLoanImpairment().

### Recommendation

Only load the payment struct from storage after the line which returns if the loan is
impaired.

### Status

Addressed in the following PR:
https://github.com/maple-labs/open-term-loan-manager-private/pull/52

---

Related findings:

- [`FlashRolloverLoan_G5` will fail for `LenderCommitmentGroup_Smart` due to `CollateralManager` pulling collateral from `FlashRolloverLoan_G5`](https://0xsimao.com/findings/teller-finance-flash-rollover-collateral-pulling): Teller Finance
