Reduce gas costs in CToken#redeemFresh

I’ve made small changes to Comptroller and CToken to reduce gas costs for CToken#redeemFresh by 3666.

The call to Comptroller#redeemVerify was commented out and its code was inlined into redeemFresh. The sanity check that was inlined isn’t specific to the Comptroller, so it seems like being in redeemFresh is a better alternative.

Pull request: https://github.com/compound-finance/compound-protocol/pull/140/commits/9cd1a944907b3b28c437c675390291e04026e64f

Gas report:

BEFORE

first mint - 126183 gas
second mint - 86711 gas
second mint, no interest accrued - 62679 gas
redeem - 96782 gas
unitroller-g6 second mint with comp accrued - 140079 gas
unitroller-g6 claim comp - 142304 gas
unitroller second mint with comp accrued - 119362 gas
unitroller claim comp - 144374 gas

AFTER

first mint - 126183 gas
second mint - 86711 gas
second mint, no interest accrued - 62679 gas
redeem - 93116 gas
unitroller-g6 second mint with comp accrued - 140079 gas
unitroller-g6 claim comp - 142304 gas
unitroller second mint with comp accrued - 119362 gas
unitroller claim comp - 144374 gas
6 Likes

@TylerEther this is a simple, useful gas-saving change for a core cToken function; I’ll try to get other members of the community to double-check this change, but it should hopefully be a no-brainer to include in the next the next cToken implementation.

2 Likes

Awesome job @TylerEther. Since they have the potential to impact many users at once, gas-savings are incredibly important changes that the community could be making.

Compound Grants would like to support your work with a grant! For all others reading, we have outstanding RFP’s for improving the gas efficiency of Compound. If you can make Compound cheaper and more efficient for everyone, you’re eligible for what can be a (very) hefty grant.

2 Likes

Thanks, Larry! I’ll apply for a grant within the next day or so. :slight_smile:

2 Likes

I’ve made some additional changes to Comptroller to reduce gas costs for COMP accrual functions by a total of 1963.

Second mint with comp accrued: reduced by 695.
Claim comp: reduced by 1268.

Pull request: https://github.com/compound-finance/compound-protocol/pull/143

Gas report:

BEFORE

first mint - 126183 gas
second mint - 86711 gas
second mint, no interest accrued - 62679 gas
redeem - 96782 gas
unitroller-g6 second mint with comp accrued - 140079 gas
unitroller-g6 claim comp - 142304 gas
unitroller second mint with comp accrued - 119362 gas
unitroller claim comp - 144374 gas

AFTER comp accrual optimizations

first mint - 126183 gas
second mint - 86711 gas
second mint, no interest accrued - 62679 gas
redeem - 96782 gas
unitroller-g6 second mint with comp accrued - 140079 gas
unitroller-g6 claim comp - 142304 gas
unitroller second mint with comp accrued - 118667 gas
unitroller claim comp - 143106 gas

There’s one further gas cost optimization I found which is quite trivial: setting the optimization runs to 40000 instead of the default 200.

Gas report:

BEFORE

first mint - 126183 gas
second mint - 86711 gas
second mint, no interest accrued - 62679 gas
redeem - 96782 gas
unitroller-g6 second mint with comp accrued - 140079 gas
unitroller-g6 claim comp - 142304 gas
unitroller second mint with comp accrued - 119362 gas
unitroller claim comp - 144374 gas

AFTER optimization w/ 40,000 runs

first mint - 125801 gas
second mint - 86299 gas
second mint, no interest accrued - 62315 gas
redeem - 96361 gas
unitroller-g6 second mint with comp accrued - 139136 gas
unitroller-g6 claim comp - 141576 gas
unitroller second mint with comp accrued - 118666 gas
unitroller claim comp - 143623 gas

No pull request was made for this one as it’s a simple one line change and I’m unsure if this could break anything.

:partying_face:

2 Likes