Optimize nested mapping access in ERC721Enumerable (#4545)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
pull/4905/head
Vladislav Volosnikov 11 months ago committed by GitHub
parent 159fc11d81
commit ad27fb654a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 8
      contracts/token/ERC721/extensions/ERC721Enumerable.sol

@ -122,17 +122,19 @@ abstract contract ERC721Enumerable is ERC721, IERC721Enumerable {
uint256 lastTokenIndex = balanceOf(from);
uint256 tokenIndex = _ownedTokensIndex[tokenId];
mapping(uint256 index => uint256) storage _ownedTokensByOwner = _ownedTokens[from];
// When the token to delete is the last token, the swap operation is unnecessary
if (tokenIndex != lastTokenIndex) {
uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];
uint256 lastTokenId = _ownedTokensByOwner[lastTokenIndex];
_ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
_ownedTokensByOwner[tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
_ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
}
// This also deletes the contents at the last position of the array
delete _ownedTokensIndex[tokenId];
delete _ownedTokens[from][lastTokenIndex];
delete _ownedTokensByOwner[lastTokenIndex];
}
/**

Loading…
Cancel
Save