Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Represent present value with 256 bits instead of 104 #454

Merged
merged 3 commits into from Jul 18, 2022

Conversation

jflatow
Copy link
Contributor

@jflatow jflatow commented Jul 1, 2022

This internally uses 256 bits for present values instead of artificially constraining them to 104 bits. This avoids an unsafe cast we were doing previously to save gas, and also opens up some other gas optimizations / contract size reductions. However, this also removes some previously unchecked math, which makes it a slight tradeoff in terms of gas.

@jflatow
Copy link
Contributor Author

jflatow commented Jul 1, 2022

Just measured, and it seems to save ~100 bytes contract size and a few hundred gas on supply, transfer, and withdraw, in addition to avoiding the unsafe cast.

Copy link
Contributor

@kevincheng96 kevincheng96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's nice that this shaves off some gas and contract size. However, other than the unchecked change, I don't think these changes are actually adding extra safety right?

I've always been supportive of using uint256/int256 for intermediate variables, but this is a pretty large change late in the cycle so I'll want to spend some more time to review this. But I definitely think this is the right change to make. 👍

contracts/Comet.sol Outdated Show resolved Hide resolved
contracts/Comet.sol Outdated Show resolved Hide resolved
contracts/CometCore.sol Show resolved Hide resolved
@jflatow
Copy link
Contributor Author

jflatow commented Jul 1, 2022

It's nice that this shaves off some gas and contract size. However, other than the unchecked change, I don't think these changes are actually adding extra safety right?

I've always been supportive of using uint256/int256 for intermediate variables, but this is a pretty large change late in the cycle so I'll want to spend some more time to review this. But I definitely think this is the right change to make. 👍

Well it gets rid of the unsafe cast in presentValue which was the original reason I was looking at it again, is that what you mean by the unchecked change? There's also the mulPrice / signedMulPrice change which I wasn't wanting or planning on making, but kind of fell out of it

@kevincheng96
Copy link
Contributor

Well it gets rid of the unsafe cast in presentValue which was the original reason I was looking at it again, is that what you mean by the unchecked change? There's also the mulPrice / signedMulPrice change which I wasn't wanting or planning on making, but kind of fell out of it

Yeah, that's what I meant by "unchecked change". Just to make sure I'm not missing anything, I want to double check with you that that's the only change here that changes the safety of our code. The other changes are more gas savings/optimizations (e.g. less explicit casting) right?

@jflatow
Copy link
Contributor Author

jflatow commented Jul 1, 2022

Well it gets rid of the unsafe cast in presentValue which was the original reason I was looking at it again, is that what you mean by the unchecked change? There's also the mulPrice / signedMulPrice change which I wasn't wanting or planning on making, but kind of fell out of it

Yeah, that's what I meant by "unchecked change". Just to make sure I'm not missing anything, I want to double check with you that that's the only change here that changes the safety of our code. The other changes are more gas savings/optimizations (e.g. less explicit casting) right?

Yes, those 2 changes should be the difference

@jflatow jflatow force-pushed the jflatow/present-256 branch 2 times, most recently from 792d5b4 to 6123b1d Compare July 6, 2022 18:29
contracts/Comet.sol Outdated Show resolved Hide resolved
contracts/CometCore.sol Show resolved Hide resolved
contracts/CometCore.sol Outdated Show resolved Hide resolved
Copy link
Contributor

@kevincheng96 kevincheng96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, looks good!

contracts/Comet.sol Show resolved Hide resolved
This internally uses 256 bits for present values instead of artificially constraining them to 104 bits.
This avoids an unsafe cast we were doing previously to save gas, and also opens up some other gas optimizations / contract size reductions.
However, this also removes some previously unchecked math, which makes it a slight tradeoff in terms of gas.
@jflatow jflatow merged commit aa92a19 into main Jul 18, 2022
@jflatow jflatow deleted the jflatow/present-256 branch August 4, 2022 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants