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

Add gas optimization by implementing unchecked index increment #13

wants to merge 6 commits into from

Conversation

sennett-lau
Copy link

Here is the comparison of the updated and the original gas snapshot for the test of PBTSimple.sol

PBTSimpleTest:testGetTokenDataForChipSignature() (gas: 127965 → 127811) -154
PBTSimpleTest:testGetTokenDataForChipSignatureBlockNumTooOld() (gas: 122516 → 122357) -159
PBTSimpleTest:testGetTokenDataForChipSignatureInvalid() (gas: 131807 → 131653) -154
PBTSimpleTest:testGetTokenDataForChipSignatureInvalidBlockNumber() (gas: 122355 → 122191) -164
PBTSimpleTest:testIsChipSignatureForToken() (gas: 216733 → 216594) -139
PBTSimpleTest:testMintTokenWithChip() (gas: 176613 → 1776454) -159
PBTSimpleTest:testSeedChipToTokenMapping() (gas: 115251 → 115137) -114
PBTSimpleTest:testSeedChipToTokenMappingExistingToken() (gas: 212298 → 212184) -114
PBTSimpleTest:testSeedChipToTokenMappingInvalidInput() (gas: 17175) ±0
PBTSimpleTest:testSupportsInterface() (gas: 7059) ±0
PBTSimpleTest:testTokenIdFor() (gas: 117085 → 117028) -57
PBTSimpleTest:testTokenIdMappedFor() (gas: 66779 → 66722) -57
PBTSimpleTest:testTransferTokenWithChip(bool) (runs: 256, μ: 211915 → 211757, ~: 211834 → 211675) (±0, -158, -159)
PBTSimpleTest:testUpdateChips() (gas: 171055 → 170868) -187
PBTSimpleTest:testUpdateChipsInvalidInput() (gas: 16433) ±0
PBTSimpleTest:testUpdateChipsUnsetChip() (gas: 23430) ±0

@cygaar
Copy link
Collaborator

cygaar commented Oct 21, 2022

LGTM after rebase

@sennett-lau
Copy link
Author

Have first merged with the updated main branch, updated the gas-snapshot, and combined unchecked logic with the change of for loop with do while loop after seeing @Arkay92 's PR as the PR seems did not pass the test.
Also, extracted chipAddressesOld.length to chipAddressesOldLength by following what @LunEyx 's PR has done to the tokenIds.length

Here is the gas snapshot comparison by running forge snapshot --diff gas-snapshot

testApprove() (gas: 0 (0.000%))
testGetApproved() (gas: 0 (0.000%))
testIsApprovedForAll() (gas: 0 (0.000%))
testSetApprovalForAll() (gas: 0 (0.000%))
testTransferFunctions() (gas: 0 (0.000%))
testSeedChipToTokenMappingInvalidInput() (gas: 0 (0.000%))
testSupportsInterface() (gas: 0 (0.000%))
testUpdateChipsUnsetChip() (gas: 1 (0.004%))
testUpdateChipsInvalidInput() (gas: 3 (0.018%))
testIsChipSignatureForToken() (gas: -41 (-0.019%))
testTransferTokenWithChip(bool) (gas: -41 (-0.019%))
testTokenIdFor() (gas: -24 (-0.021%))
testSeedChipToTokenMappingExistingToken() (gas: -46 (-0.022%))
testMintTokenWithChip() (gas: -41 (-0.023%))
testGetTokenDataForChipSignatureInvalid() (gas: -41 (-0.031%))
testGetTokenDataForChipSignature() (gas: -41 (-0.032%))
testUpdateChips() (gas: -55 (-0.032%))
testGetTokenDataForChipSignatureBlockNumTooOld() (gas: -41 (-0.034%))
testGetTokenDataForChipSignatureInvalidBlockNumber() (gas: -41 (-0.034%))
testSeedChipToTokenMapping() (gas: -41 (-0.036%))
testTokenIdMappedFor() (gas: -24 (-0.036%))
Overall gas change: -473 (-0.315%)

@sennett-lau
Copy link
Author

Gas comparison with v0.2.0

testApprove() (gas: 0 (0.000%))
testIsApprovedForAll() (gas: 0 (0.000%))
testSetApprovalForAll() (gas: 0 (0.000%))
testTransferFunctions() (gas: 0 (0.000%))
testSeedChipToTokenMappingInvalidInput() (gas: 0 (0.000%))
testSupportsInterface() (gas: 0 (0.000%))
testUpdateChipsUnsetChip() (gas: 1 (0.004%))
testUpdateChipsInvalidInput() (gas: 3 (0.018%))
testGetApproved() (gas: 5 (0.031%))
testTokenIdFor() (gas: -75 (-0.064%))
testSeedChipToTokenMappingExistingToken() (gas: -148 (-0.070%))
testTransferTokenWithChip(bool) (gas: -191 (-0.090%))
testIsChipSignatureForToken() (gas: -196 (-0.090%))
testMintTokenWithChip() (gas: -191 (-0.108%))
testTokenIdMappedFor() (gas: -75 (-0.113%))
testSeedChipToTokenMapping() (gas: -146 (-0.127%))
testUpdateChips() (gas: -235 (-0.137%))
testGetTokenDataForChipSignatureInvalid() (gas: -186 (-0.141%))
testGetTokenDataForChipSignature() (gas: -186 (-0.145%))
testGetTokenDataForChipSignatureBlockNumTooOld() (gas: -191 (-0.156%))
testGetTokenDataForChipSignatureInvalidBlockNumber() (gas: -196 (-0.160%))
Overall gas change: -2007 (-1.348%)

@sennett-lau sennett-lau closed this by deleting the head repository Feb 13, 2023
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