wise-foundation / lending-audit

5 stars 4 forks source link

[PMR-02C] Inefficient `mapping` Lookups #45

Open vm06007 opened 1 year ago

vm06007 commented 1 year ago

PMR-02C: Inefficient mapping Lookups

Type Severity Location
Gas Optimization PoolManager.sol:L39, L69, L70

Description:

The linked statements perform key-based lookup operations on mapping declarations from storage multiple times for the same key redundantly.

Example:

algorithmData[_poolToken].increasePole = _steppingDirection;

uint256 staticMinPole = _getMinPole(
    _poolMulFactor,
    _upperBoundMaxRate
);

uint256 staticMaxPole = _getMaxPole(
    _poolMulFactor,
    _lowerBoundMaxRate
);

uint256 staticDeltaPole = _getDeltaPole(
    staticMaxPole,
    staticMinPole
);

uint256 startValuePole = _getStartValue(
    staticMaxPole,
    staticMinPole
);

borrowRatesData[_poolToken] = BorrowRatesEntry({
    pole: startValuePole,
    deltaPole: staticDeltaPole,
    minPole: staticMinPole,
    maxPole: staticMaxPole,
    multiplicativeFactor: _poolMulFactor
});

algorithmData[_poolToken].bestPole = startValuePole;
algorithmData[_poolToken].maxValue = lendingPoolData[_poolToken].totalDepositShares;

Recommendation:

As the lookups internally perform an expensive keccak256 operation, we advise the lookups to be cached wherever possible to a single local declaration that either holds the value of the mapping in case of primitive types or holds a storage pointer to the struct contained.

vm06007 commented 1 year ago

Resolved in: https://github.com/wise-foundation/lending-audit/pull/44