-
Notifications
You must be signed in to change notification settings - Fork 842
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
Some minor optimizations and refactors #337
Conversation
Will take one more pass in the AM tomorrow |
Can you update the PR summary to include the changes you've made and the motivation for doing so? |
contracts/ERC721A.sol
Outdated
/** | ||
* @dev Casts the boolean to uint256 without branching. | ||
*/ | ||
function _toUint256(bool value) private pure returns (uint256 result) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why rename the function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shorter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think we should keep the original name. When reading the calling code, you have to double take to make sure you're converting a bool to int
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh… ok then.
} | ||
if (approvalCheck) | ||
if (!_isOwnerOrApproved(approvedAddress, from, _msgSenderERC721A())) | ||
if (!isApprovedForAll(from, _msgSenderERC721A())) revert TransferCallerNotOwnerNorApproved(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you combine this with the if-statement on 706? The formatting looks weird right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is intentional for performance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are the gains?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
26
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment here then? I know people are going to ask about this otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, added
This will make using assembly more worth it imo.
The happy case may cost at most 3 gas more, but when there are approvals, we can save tens to hundreds of gas.
https://www.diffchecker.com/jZfRzVAz
@cygaar
Changes:
_transfer
, intotransferFrom
to save around 30 gas.Renamed the private function,_boolToUint256
, to_toUint256
for conciseness. Line lengths are getting a bit long, and it is obvious from the context that the input is a bool.approvedAddress
and it’s corresponding slot. The slot allows us to delete it later without recomputing the keccak256, be it via assembly or implicitly with plain Solidity._deleteApprovedAddress
, is replaced with the above functions.