Browse Source

Optimize nested mapping access in ERC721Enumerable (#4545)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Vladislav Volosnikov 1 year ago
parent
commit
ad27fb654a
1 changed files with 5 additions and 3 deletions
  1. 5 3
      contracts/token/ERC721/extensions/ERC721Enumerable.sol

+ 5 - 3
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];
     }
 
     /**