From df39e3ec58859e82f8603c6c2f81543ae60140dd Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Tue, 31 Oct 2023 10:25:52 +0300 Subject: [PATCH] perf: remove the need of via ir --- foundry.toml | 2 +- src/SpeedJumpIrm.sol | 18 +++++++++++------- test/SpeedJumpIrmTest.sol | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/foundry.toml b/foundry.toml index b4d0a92c..c436d1d7 100644 --- a/foundry.toml +++ b/foundry.toml @@ -3,7 +3,7 @@ src = "src" out = "out" test = "test" libs = ["lib"] -via-ir = true +via-ir = false [profile.default.fuzz] runs = 4096 diff --git a/src/SpeedJumpIrm.sol b/src/SpeedJumpIrm.sol index 6597f5b5..8e4120d6 100644 --- a/src/SpeedJumpIrm.sol +++ b/src/SpeedJumpIrm.sol @@ -112,13 +112,17 @@ contract AdaptativeCurveIRM is IIrm { // Safe "unchecked" int256 casts because utilization <= WAD, TARGET_UTILIZATION < WAD and errNormFactor <= WAD. int256 err = (int256(utilization) - int256(TARGET_UTILIZATION)).wDivDown(int256(errNormFactor)); - // Safe "unchecked" cast because ADJUSTMENT_SPEED <= type(int256).max. - int256 speed = int256(ADJUSTMENT_SPEED).wMulDown(err); - uint256 elapsed = (baseRate[id] > 0) ? block.timestamp - market.lastUpdate : 0; - // Safe "unchecked" cast because elapsed <= block.timestamp. - int256 linearVariation = speed * int256(elapsed); - uint256 variationMultiplier = MathLib.wExp(linearVariation); - uint256 newBaseRate = (baseRate[id] > 0) ? baseRate[id].wMulDown(variationMultiplier) : INITIAL_BASE_RATE; + int256 linearVariation; + { + // Safe "unchecked" cast because ADJUSTMENT_SPEED <= type(int256).max. + int256 speed = int256(ADJUSTMENT_SPEED).wMulDown(err); + uint256 elapsed = (baseRate[id] > 0) ? block.timestamp - market.lastUpdate : 0; + // Safe "unchecked" cast because elapsed <= block.timestamp. + linearVariation = speed * int256(elapsed); + } + + uint256 newBaseRate = + (baseRate[id] > 0) ? baseRate[id].wMulDown(MathLib.wExp(linearVariation)) : INITIAL_BASE_RATE; uint256 newBorrowRate = _curve(newBaseRate, err); // Then we compute the average rate over the period (this is what Morpho needs to accrue the interest). diff --git a/test/SpeedJumpIrmTest.sol b/test/SpeedJumpIrmTest.sol index d2aa65d3..3aed5a1c 100644 --- a/test/SpeedJumpIrmTest.sol +++ b/test/SpeedJumpIrmTest.sol @@ -164,7 +164,7 @@ contract AdaptativeCurveIRMTest is Test { return avgBorrowRate; } - function _curve(uint256 baseRate, int256 err) internal view returns (uint256) { + function _curve(uint256 baseRate, int256 err) internal pure returns (uint256) { // Safe "unchecked" cast because err >= -1 (in WAD). if (err < 0) { return uint256((WAD_INT - WAD_INT.wDivDown(int256(CURVE_STEEPNESS))).wMulDown(err) + WAD_INT).wMulDown(