chiru-labs / ERC721A

https://ERC721A.org
MIT License
2.51k stars 842 forks source link

Optimize ERC721AQueryable for smaller bytecode size #431

Closed Arkay92 closed 1 year ago

Vectorized commented 2 years ago

Right now, I think Queryable should be optimized for smaller bytecode size. Runtime gas is second priority.

https://github.com/Vectorized/ERC721A/blob/bytecodeopt/contracts/extensions/ERC721AQueryable.sol

Vectorized commented 2 years ago

Before:

·-----------------------------------|-------------|---------------·
|  Deployments                      ·             ·  % of limit   │
····································|·············|················
|  ERC721AQueryableMock             ·    1460247  ·        4.9 %  │
····································|·············|················
|  ERC721AQueryableStartTokenIdMock ·    1528641  ·        5.1 %  │
·-----------------------------------|-------------|---------------·

After:

·-----------------------------------|-------------|---------------·
|  Deployments                      ·             ·  % of limit   │
····································|·············|················
|  ERC721AQueryableMock             ·    1447033  ·        4.8 %  │
····································|·············|················
|  ERC721AQueryableStartTokenIdMock ·    1517582  ·        5.1 %  │
·-----------------------------------|-------------|---------------·

Could save more if we use assembly to directly write to the memory arrays, cuz Solidity adds out-of-bounds checks, which is redundant in this case.

Vectorized commented 1 year ago

@cygaar

After the changes, the deployment gas is 1356357.

This is about 100k gas less, which is around 500 bytes saved. Before, the Queryable extension alone adds 2000 bytes. With these changes, it adds 1500 bytes.

Can be useful for people trying to avoid hitting the max contract size.