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

Add gas optimizations #59

Merged
merged 17 commits into from
Feb 2, 2022
Merged

Add gas optimizations #59

merged 17 commits into from
Feb 2, 2022

Conversation

DaniPopes
Copy link
Contributor

Wrote up a few optimizations on the main contracts, open to feedback especially for the unchecked blocks if you think that over/underflow checks are needed in those functions.

1b32797: _exists(uint256 id) returns id < currentIndex, and we call it with currentIndex, which always results in it returning false.

ae1cdcf: Explanations in the code comments.

5fca737: Initializing a variable with a 0 is more expensive than not doing so, even if both results are the same. Source:

uint256 value; is cheaper than uint256 value = 0;

c23e2e0: All values are integers which cannot be negative, saves a few opcodes.

ee2abe2: Previous changes applied to ERC721AOwnersExplicit.

81daf25: Loading nextOwnerToExplicitlySet into memory saves an SLOAD opcode later on line 23.

DaniPopes added 6 commits January 31, 2022 21:28
[source:](https://medium.com/coinmonks/gas-optimization-in-solidity-part-i-variables-9d5775e43dde)
> Initialization
> Every variable assignment in Solidity costs gas. When initializing variables, we often waste gas by assigning default values that will never be used.
> uint256 value; is cheaper than uint256 value = 0;.
Copy link
Owner

@chiru-labs chiru-labs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet stuff. Just some nits

}

for (uint256 i = oldNextOwnerToSet; i <= endIndex; i++) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we don't need oldNextOwnerToSet anymore and can just use _nextOwnerToExplicitlySet here

require(_nextOwnerToExplicitlySet < currentIndex, 'all ownerships have been set');

// Index underflow is impossible.
// Counter or index overflow is incredibly unrealistic.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please elaborate a bit more on this line (e.g.- "incredibly unrealistic, collection size would have to be ___")

uint256 tokenIdsIdx;
address currOwnershipAddr;

// Counter overflow is incredibly unrealistic.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please elaborate a bit more on this line (e.g.- "incredibly unrealistic, collection size would have to be ___")

@DaniPopes
Copy link
Contributor Author

DaniPopes commented Feb 1, 2022

Thanks for that! I missed the last one. Also added an unchecked block to ownershipOf and added a condition to prevent underflow.

DaniPopes added 3 commits February 1, 2022 04:43
@DaniPopes DaniPopes requested a review from chiru-labs February 1, 2022 05:08
Copy link
Owner

@chiru-labs chiru-labs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome looks good after rebase

@DaniPopes DaniPopes changed the title [WIP] Optimize code Optimize code Feb 1, 2022
@cygaar cygaar changed the title Optimize code Add gas optimizations Feb 2, 2022
@cygaar
Copy link
Collaborator

cygaar commented Feb 2, 2022

Love this

@cygaar cygaar merged commit 1129166 into chiru-labs:main Feb 2, 2022
@cygaar cygaar mentioned this pull request Feb 3, 2022
Vectorized added a commit to Vectorized/ERC721A that referenced this pull request Feb 9, 2022
cygaar pushed a commit that referenced this pull request Feb 12, 2022
* Added burn functionality.

* Changed _initOneIndexed

* Moved burn function into ERC721ABurnable

* Moved burn function into ERC721ABurnable

* Remove redundant burn check in ownershipOf

* Optimized ownershipOf

* Removed aux from AddressData for future PR

* Packed currentIndex and totalBurned for gas savings

* Added gas optimizations

* Added gas optimizations

* Added requested changes

* Edited comments.

* Renamed totalBurned to burnedCounter

* Renamed to burnCounter

* Updated comments.

* Mark transferFrom/safeTransferFrom virtual

* Mark transferFrom/safeTransferFrom virtual

* Updated comments.

* Tidy up tests

* Inlined _exists for _burn and _transfer.

* Merged custom errors

* Merged custom errors

* Fixed missing change from #59

* Gas optimization

* update specs for _beforeTokenTransfers and _afterTokenTransfers hooks

* Added #84

* Added #87

* Added #85

* Added #89

* Added comments on packing _currentIndex and _burnCounter

* Removed overflow check for updatedIndex

* Added requested test changes

* Removed unused variable in burn test

* Removed unused variable in burn test

Co-authored-by: Amirhossein Banavi <ahbanavi@gmail.com>
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

Successfully merging this pull request may close these issues.

3 participants