Skip to content

feat: skip zero writes when minting token id 0 - #324

Draft
fulldecent wants to merge 1 commit into
mainfrom
enumerable-zero-mint
Draft

fulldecent wants to merge 1 commit into
mainfrom
enumerable-zero-mint

Conversation

@fulldecent

@fulldecent fulldecent commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

Draft only. This is not for merge.

On top of 2.8.0, token id 0 uses push() with no value, and idToIndex is not written when the new index is 0. The owner index is still always written.

Gas is from one run on Solidity 0.8.38 and the Osaka EVM. The first table has the optimizer off, which is this repository's setting. The second table enables the optimizer for 200 runs.

Approach First mint(0) First mint(1) Next nonzero mint mint(0) after another token
2.8.0 103,276 143,088 148,688 108,876
This pull request 96,614 140,866 148,756 104,504
Approach First mint(0) First mint(1) Next nonzero mint mint(0) after another token
2.8.0 102,415 142,227 147,827 108,015
This pull request 96,026 140,314 148,180 103,892

With the optimizer on, the next nonzero mint in this pull request costs 353 gas more than 2.8.0. The other three mints are cheaper.

This uses the trick:

- x.push(a);
+ if (a != 0)
+     x.push(a);
+ else
+     x.push();

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new zero-ID branches lack regression tests for minting, burning, re-minting, and transfers, leaving supply, enumeration, and index bookkeeping unverified.

1 open issue
What changed in this PR

This draft PR reduces zero-value storage writes in NFTokenEnumerable while preserving global and owner token indexing.

Changes:

  • Uses value-less push() for token ID 0 in global and owner arrays.
  • Captures the global index before insertion and skips writing idToIndex when that index is zero.
  • Keeps owner-index writes unconditional, using the owner array’s length before insertion.
File Description
src/​contracts/​tokens/​nf-token-enumerable.sol Optimizes zero-ID insertion and index writes in minting and owner assignment.

Review effort: Balanced

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.

2 participants