morpho-org / morpho-utils

Repository gathering useful libraries and contracts.
GNU Affero General Public License v3.0
65 stars 1 forks source link

Optimized min-max-zeroFloorSub functions #30

Closed AtomicAzzaz closed 1 year ago

AtomicAzzaz commented 1 year ago

Before: image After: image Same savings for zerFloorSub

github-actions[bot] commented 1 year ago

Changes to gas costs

๐Ÿงพ Summary

Contract Method Avg (+/-) %
MathFunctions zeroFloorSub
max
min
divUp
-38 โœ…
-34 โœ…
-23 โœ…
-16 โœ…
+0.18%
+0.16%
+0.11%
+0.08%

Full diff report ๐Ÿ‘‡
| Contract | Deployment Cost (+/-) | Method | Min (+/-) | % | Avg (+/-) | % | Median (+/-) | % | Max (+/-) | % | # Calls (+/-) | |:-|-:|:-|-:|-:|-:|-:|-:|-:|-:|-:|-:| | **MathFunctions** | 69,517 (-8,612) | _zeroFloorSub_
_max_
_min_
_divUp_ | 361 (-38)
320 (-34)
353 (-23)
419 (-16) | **+0.18%**
**+0.16%**
**+0.11%**
**+0.08%** | 361 (-38)
320 (-34)
353 (-23)
419 (-16) | **+0.18%**
**+0.16%**
**+0.11%**
**+0.08%** | 361 (-38)
320 (-34)
353 (-23)
419 (-16) | **+0.18%**
**+0.16%**
**+0.11%**
**+0.08%** | 361 (-38)
320 (-35)
353 (-24)
419 (-16) | **+0.18%**
**+0.17%**
**+0.12%**
**+0.08%** | 1 (0)
2 (0)
2 (0)
1 (0) |
makcandrov commented 1 year ago

The same strat can be applied to zeroFloorSub:

function zeroFloorSub(uint256 x, uint256 y) internal pure returns (uint256 z) {
          assembly {
              z := mul(gt(x, y), sub(x, y))
          }
}

from 399 to 361 gas

AtomicAzzaz commented 1 year ago

The same strat can be applied to zeroFloorSub:

function zeroFloorSub(uint256 x, uint256 y) internal pure returns (uint256 z) {
          assembly {
              z := mul(gt(x, y), sub(x, y))
          }
}

from 399 to 361 gas

Of course!

MathisGD commented 1 year ago

Of course!

@AtomicAzzaz you want to do it or should I do it ?

AtomicAzzaz commented 1 year ago

Of course!

@AtomicAzzaz you want to do it or should I do it ?

I've already done it!