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

0xInu~ _packedOwnershipOf optimization, saving 2143 gas on transfers #433

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions contracts/ERC721A.sol
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,23 @@ contract ERC721A is IERC721A {
/**
* Returns the packed ownership data of `tokenId`.
*/
function _packedOwnershipOf(uint256 tokenId) private view returns (uint256) {
function _packedOwnershipOf(uint256 tokenId) private view returns (uint256) {
Vectorized marked this conversation as resolved.
Show resolved Hide resolved
uint256 curr = tokenId;

unchecked {
if (_startTokenId() <= curr)
if (_startTokenId() <= curr) {

// load the packed ownership of the current tokenId
uint256 packed = _packedOwnerships[curr];

// If the data exists, and it's not burned, return it and skip tracing
// This is possible because we have already achieved the target condition
// This saves 2143 gas on transfers of initialized tokens.
if (packed != 0 && packed & _BITMASK_BURNED == 0) return packed;

// If the tokenId is within index-bounds
if (curr < _currentIndex) {
uint256 packed = _packedOwnerships[curr];

// If not burned.
if (packed & _BITMASK_BURNED == 0) {
// Invariant:
Expand All @@ -367,6 +377,7 @@ contract ERC721A is IERC721A {
return packed;
}
}
}
}
revert OwnerQueryForNonexistentToken();
}
Expand Down