Skip to content

Commit ad27fb6

Browse files
0xVolosnikovAmxx
andauthored
Optimize nested mapping access in ERC721Enumerable (#4545)
Co-authored-by: Hadrien Croubois <[email protected]>
1 parent 159fc11 commit ad27fb6

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

contracts/token/ERC721/extensions/ERC721Enumerable.sol

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,19 @@ abstract contract ERC721Enumerable is ERC721, IERC721Enumerable {
122122
uint256 lastTokenIndex = balanceOf(from);
123123
uint256 tokenIndex = _ownedTokensIndex[tokenId];
124124

125+
mapping(uint256 index => uint256) storage _ownedTokensByOwner = _ownedTokens[from];
126+
125127
// When the token to delete is the last token, the swap operation is unnecessary
126128
if (tokenIndex != lastTokenIndex) {
127-
uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];
129+
uint256 lastTokenId = _ownedTokensByOwner[lastTokenIndex];
128130

129-
_ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
131+
_ownedTokensByOwner[tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
130132
_ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
131133
}
132134

133135
// This also deletes the contents at the last position of the array
134136
delete _ownedTokensIndex[tokenId];
135-
delete _ownedTokens[from][lastTokenIndex];
137+
delete _ownedTokensByOwner[lastTokenIndex];
136138
}
137139

138140
/**

0 commit comments

Comments
 (0)