-
-
Notifications
You must be signed in to change notification settings - Fork 800
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
Optimize multiplication operation on Vyper #965
Comments
Wow good catch picking that up from SafeMath! 😄 |
I'm missing something about this comment in context of the code. How does checking if and only if |
Oops. I passed out 3 hours ago and checked this. I will implement it right. |
@fubuloubu so basically you save gas because you immediately return 0 for |
Okay, that makes way more sense now lol |
Simple Summary
Optimize multiplication check as recent openzeppelin safemath implementation.
Abstract
One of the things I like Vyper is that it supports safemath from the language.
Its logic is on expr.py.
However, I found a typo in the comment:
from
# Checks that: a == 0 || a / b == b
to
# Checks that: a == 0 || c(which is a*b) / a == b
and a space for gas optimization which was approved in Openzeppelin's SafeMath implementation.
Motivation
If this change is not approved, comment typo is not fixed and vyper uses more gas than solidity with safemath contract.
Specification
Here is the difference of Opcode to be pushed to LLL parse tree:
Before
assert(a==0 || (a*b / a) == b)
After
if (a == 0) { return 0; } // Without verifying a*b / a == b
assert(a*b/a==b);
Reasons and verification is described on the link here
Backwards Compatibility
This version is compatible with the others but it uses less gas for an arithmetic operation.
Copyright
Copyright and related rights waived via CC0
The text was updated successfully, but these errors were encountered: