chiru-labs / ERC721A

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

Assembly mint loop #347

Closed Vectorized closed 2 years ago

Vectorized commented 2 years ago

Just letting it sit here.

May or may not decide to merge.

OpenSea Rinkeby is able to detect the events as per normal.

Saves 36 gas for the first mint and 28 gas per additional iteration (28 / 1958 ≈ 1.4% savings per additional iteration).

The variables and statements have been carefully arranged to ensure that the compiler is as well behaved as possible. (actually, it can perform better than 100% handwritten Yul cuz Solidity provides high level semantics that can aid optimizations).

Before:

|  ERC721AGasReporterMock  ·  mintOne          ·      56201  ·      90401  ·      59621  ·
···························|···················|·············|·············|·············|
|  ERC721AGasReporterMock  ·  mintTen          ·      73828  ·     108028  ·      97768  ·

After:

|  ERC721AGasReporterMock  ·  mintOne          ·      56165  ·      90365  ·      59585  ·
···························|···················|·············|·············|·············|
|  ERC721AGasReporterMock  ·  mintTen          ·      73540  ·     107740  ·      97480  ·

Some trivia: