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

[optimization] Gas use in removeNFToken #104

Closed
mg6maciej opened this issue Jun 24, 2018 · 9 comments
Closed

[optimization] Gas use in removeNFToken #104

mg6maciej opened this issue Jun 24, 2018 · 9 comments
Assignees

Comments

@mg6maciej
Copy link
Contributor

These two lines don't need to be executed when tokenToRemoveIndex == lastTokenIndex.

    ownerToIds[_from][tokenToRemoveIndex] = lastToken;
    ownerToIds[_from][lastTokenIndex] = 0;

Next line covers clearing it:

    ownerToIds[_from].length--;
@mg6maciej
Copy link
Contributor Author

Actually

ownerToIds[_from][lastTokenIndex] = 0;

is never necessary.

@mudgen
Copy link

mudgen commented Jun 24, 2018

I already reported about this here: #91

@MoMannn
Copy link
Collaborator

MoMannn commented Jun 24, 2018

It was reported and fixed. I believe @mg6maciej is not using the last version. Please check #91.
We will check about tokenToRemoveIndex == lastTokenIndex state.

@MoMannn
Copy link
Collaborator

MoMannn commented Jun 24, 2018

Lets continue the discussion about tokenToRemoveIndex == lastTokenIndex in #105.

@MoMannn MoMannn closed this as completed Jun 24, 2018
@mg6maciej
Copy link
Contributor Author

Oh, yeah. I even commented on removal of ownerToIds[_from][lastTokenIndex] = 0;, but the issue still stands. #105 is about a bug, this is about optimization. To make it clearer optimization can be applied every time a token which has the largest index for given user (assuming users have only a few tokens this may happen frequently) is transferred or burned.

Better yet, you may apply something like this: OpenZeppelin/openzeppelin-contracts#1031

@MoMannn MoMannn reopened this Jun 24, 2018
@mg6maciej
Copy link
Contributor Author

mg6maciej commented Jun 24, 2018

If you are not willing to go in the direction of 0x PR on OZ repo (removing removeNFToken and addNFToken altogether), but still brave enough, I believe (all tests pass) this change in Enumerable's removeNFToken will work:

@@ -108,11 +108,12 @@ contract NFTokenEnumerable is
     uint256 lastTokenIndex = ownerToIds[_from].length.sub(1);
     uint256 lastToken = ownerToIds[_from][lastTokenIndex];

-    ownerToIds[_from][tokenToRemoveIndex] = lastToken;
+    if (tokenToRemoveIndex != lastToken) {
+      ownerToIds[_from][tokenToRemoveIndex] = lastToken;
+      idToOwnerIndex[lastToken] = tokenToRemoveIndex;
+    }

     ownerToIds[_from].length--;
-    idToOwnerIndex[_tokenId] = 0;
-    idToOwnerIndex[lastToken] = tokenToRemoveIndex;
   }

   /**

As you may noticed, two updates are done conditionally, but also idToOwnerIndex[_tokenId] = 0; is not there anymore. This is because in transfers this update is overridden by one in addNFToken and for _burn leaving this value in unspecified state won't ever cause any problems as token with this id doesn't exists and will not be read. If later token with this id is minted again, the value is set in addNFToken.

@mg6maciej mg6maciej changed the title [optimization] Gas use when in removeNFToken [optimization] Gas use in removeNFToken Jun 24, 2018
@xpepermint
Copy link
Member

@MoMannn let's test that.

@MoMannn
Copy link
Collaborator

MoMannn commented Jun 25, 2018

The decision of what to do here is connected to discussion in #114. So if decision there is to lose inheritance and go to full optimization that would mean losing function and be an answer for this as well.

@xpepermint
Copy link
Member

Closing in favor of #114.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants