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

Some minor optimizations and refactors #337

Merged
merged 12 commits into from
Jun 14, 2022
Merged

Conversation

Vectorized
Copy link
Collaborator

@Vectorized Vectorized commented Jun 14, 2022

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:

  • Inlined the private function, _transfer, into transferFrom 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.
  • Added a function to return the 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.
  • Note that the deletion part must be manually inlined, as the compiler won’t inline functions with conditional blocks.
  • Added a function to do branchless boolean or clause to check if the message sender is the owner or an approved address. This can’t be done easily with plain Solidity.
  • The private function, _deleteApprovedAddress, is replaced with the above functions.
  • Refactored the boolean condition for approval check to use nested if statements, which compiles to lesser opcodes.

contracts/ERC721A.sol Show resolved Hide resolved
contracts/ERC721A.sol Show resolved Hide resolved
@cygaar
Copy link
Collaborator

cygaar commented Jun 14, 2022

Will take one more pass in the AM tomorrow

@cygaar
Copy link
Collaborator

cygaar commented Jun 14, 2022

Can you update the PR summary to include the changes you've made and the motivation for doing so?

/**
* @dev Casts the boolean to uint256 without branching.
*/
function _toUint256(bool value) private pure returns (uint256 result) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why rename the function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Shorter.

Copy link
Collaborator

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

Copy link
Collaborator Author

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();
Copy link
Collaborator

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.

Copy link
Collaborator Author

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.

Copy link
Collaborator

Choose a reason for hiding this comment

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

What are the gains?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

26

Copy link
Collaborator

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, added

@Vectorized Vectorized merged commit 3fed4e9 into chiru-labs:main Jun 14, 2022
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.

2 participants