-
Notifications
You must be signed in to change notification settings - Fork 841
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
Conversation
[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;.
There was a problem hiding this 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++) { |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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 ___")
contracts/ERC721A.sol
Outdated
uint256 tokenIdsIdx; | ||
address currOwnershipAddr; | ||
|
||
// Counter overflow is incredibly unrealistic. |
There was a problem hiding this comment.
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 ___")
Thanks for that! I missed the last one. Also added an unchecked block to |
There was a problem hiding this 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
Love this |
* 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>
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:
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.