Optimize nested mapping access in ERC721Enumerable (#4545)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
This commit is contained in:
Vladislav Volosnikov
2024-03-25 13:14:16 +01:00
committed by GitHub
parent 159fc11d81
commit ad27fb654a

View File

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