Skip to content
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 optimization by implementing unchecked index increment #13

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
ERC721ReadOnlyTest:testApprove() (gas: 10677)
ERC721ReadOnlyTest:testGetApproved() (gas: 16236)
ERC721ReadOnlyTest:testGetApproved() (gas: 16241)
ERC721ReadOnlyTest:testIsApprovedForAll() (gas: 10051)
ERC721ReadOnlyTest:testSetApprovalForAll() (gas: 10714)
ERC721ReadOnlyTest:testTransferFunctions() (gas: 19077)
PBTSimpleTest:testGetTokenDataForChipSignature() (gas: 127958)
PBTSimpleTest:testGetTokenDataForChipSignatureBlockNumTooOld() (gas: 122512)
PBTSimpleTest:testGetTokenDataForChipSignatureInvalid() (gas: 131804)
PBTSimpleTest:testGetTokenDataForChipSignatureInvalidBlockNumber() (gas: 122351)
PBTSimpleTest:testIsChipSignatureForToken() (gas: 217369)
PBTSimpleTest:testMintTokenWithChip() (gas: 176650)
PBTSimpleTest:testSeedChipToTokenMapping() (gas: 115247)
PBTSimpleTest:testSeedChipToTokenMappingExistingToken() (gas: 212294)
PBTSimpleTest:testGetTokenDataForChipSignature() (gas: 127772)
PBTSimpleTest:testGetTokenDataForChipSignatureBlockNumTooOld() (gas: 122321)
PBTSimpleTest:testGetTokenDataForChipSignatureInvalid() (gas: 131618)
PBTSimpleTest:testGetTokenDataForChipSignatureInvalidBlockNumber() (gas: 122155)
PBTSimpleTest:testIsChipSignatureForToken() (gas: 217173)
PBTSimpleTest:testMintTokenWithChip() (gas: 176459)
PBTSimpleTest:testSeedChipToTokenMapping() (gas: 115101)
PBTSimpleTest:testSeedChipToTokenMappingExistingToken() (gas: 212146)
PBTSimpleTest:testSeedChipToTokenMappingInvalidInput() (gas: 17178)
PBTSimpleTest:testSupportsInterface() (gas: 7059)
PBTSimpleTest:testTokenIdFor() (gas: 117084)
PBTSimpleTest:testTokenIdMappedFor() (gas: 66558)
PBTSimpleTest:testTransferTokenWithChip(bool) (runs: 256, μ: 211956, ~: 211875)
PBTSimpleTest:testUpdateChips() (gas: 171052)
PBTSimpleTest:testUpdateChipsInvalidInput() (gas: 16433)
PBTSimpleTest:testUpdateChipsUnsetChip() (gas: 23430)
PBTSimpleTest:testTokenIdFor() (gas: 117009)
PBTSimpleTest:testTokenIdMappedFor() (gas: 66483)
PBTSimpleTest:testTransferTokenWithChip(bool) (runs: 256, μ: 211765, ~: 211684)
PBTSimpleTest:testUpdateChips() (gas: 170817)
PBTSimpleTest:testUpdateChipsInvalidInput() (gas: 16436)
PBTSimpleTest:testUpdateChipsUnsetChip() (gas: 23431)
29 changes: 24 additions & 5 deletions src/PBTSimple.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,41 @@ contract PBTSimple is ERC721ReadOnly, IPBT {
if (tokenIdsLength != chipAddresses.length) {
revert ArrayLengthMismatch();
}
for (uint256 i = 0; i < tokenIdsLength; ++i) {

if (tokenIdsLength == 0) {
return;
}

uint256 i = 0;
do {
address chipAddress = chipAddresses[i];
uint256 tokenId = tokenIds[i];
if (throwIfTokenAlreadyMinted && _exists(tokenId)) {
revert SeedingChipDataForExistingToken();
}
_tokenDatas[chipAddress] = TokenData(tokenId, chipAddress, true);
}
unchecked {
i++;
}
} while (i < tokenIdsLength);
}

// Should only be called for tokenIds that have been minted
// If the tokenId hasn't been minted yet, use _seedChipToTokenMapping instead
// Should only be used and called with care and rails to avoid a centralized entity swapping out valid chips.
// TODO: consider preventing multiple chip addresses mapping to the same tokenId (store a tokenId->chip mapping)
function _updateChips(address[] calldata chipAddressesOld, address[] calldata chipAddressesNew) internal {
if (chipAddressesOld.length != chipAddressesNew.length) {
uint256 chipAddressesOldLength = chipAddressesOld.length;
if (chipAddressesOldLength != chipAddressesNew.length) {
revert ArrayLengthMismatch();
}
for (uint256 i = 0; i < chipAddressesOld.length; ++i) {

if (chipAddressesOldLength == 0) {
return;
}

uint256 i = 0;
do {
address oldChipAddress = chipAddressesOld[i];
TokenData memory oldTokenData = _tokenDatas[oldChipAddress];
if (!oldTokenData.set) {
Expand All @@ -80,7 +96,10 @@ contract PBTSimple is ERC721ReadOnly, IPBT {
emit PBTChipRemapping(tokenId, oldChipAddress, newChipAddress);
}
delete _tokenDatas[oldChipAddress];
}
unchecked {
i++;
}
} while (i < chipAddressesOldLength);
}

function tokenIdFor(address chipAddress) external view override returns (uint256) {
Expand Down