chiru-labs / ERC721A

https://ERC721A.org
MIT License
2.5k stars 841 forks source link

0xInu~ _packedOwnershipOf optimization, saving 2143 gas on transfers #433

Closed 0xInuarashi closed 1 year ago

0xInuarashi commented 2 years ago

Summary:

ERC721A: 
1>Mint(a,5): 98279
2>TransferFrom(a,b,3): 107582
3>TransferFrom(a,b,1): 86054
4>TransferFrom(b,a,1): 44343
5>TransferFrom(b,a,3): 39543
6>Mint(a,50): 151064
7>TransferFrom(a,b,50): 200570
ERC721A 0xInu Optimization: 
1>Mint(a,5): 98257
2>TransferFrom(a,b,3): 107603
3>TransferFrom(a,b,1): 86075
4>TransferFrom(b,a,1): 42200
5>TransferFrom(b,a,3): 37400
6>Mint(a,50): 151042
7>TransferFrom(a,b,50): 200591
0xInuarashi commented 2 years ago

The assumption is that, if the packed token data exists, it must already be within-bounds of < _currentIndex Thus, if the token data exists (and in this case, it is also not burnt), we can save 1 SLOAD by skipping the _currentIndex and go straight to returning the already-initialized token data.

Vectorized commented 2 years ago

Holy shit, this is gigabrain.

cygaar commented 2 years ago

Good catch