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

enhance {_setOwnedIndex} to reduce the gas consumption #48

Open
RevelationOfTuring opened this issue Mar 18, 2024 · 0 comments
Open

enhance {_setOwnedIndex} to reduce the gas consumption #48

RevelationOfTuring opened this issue Mar 18, 2024 · 0 comments

Comments

@RevelationOfTuring
Copy link

RevelationOfTuring commented Mar 18, 2024

Issue

There is a useless assembly operation in the data packing of internal function {_setOwnedIndex}:

function _setOwnedIndex(uint256 id_, uint256 index_) internal virtual {
   ...

    assembly {
      data := add(
        and(data, _BITMASK_ADDRESS),
        // shl(160, index_) makes sure all 0 in the low 160 bits so that `and(xxx, _BITMASK_OWNED_INDEX)` is needless
        and(shl(160, index_), _BITMASK_OWNED_INDEX)
      )
    }

    _ownedData[id_] = data;
  }

I have run the test with my modification and it actually reduces the gas:
·-------------------------------------------------|----------------------------|-------------|-----------------------------·
| Solc version: 0.8.20 · Optimizer enabled: false · Runs: 200 · Block limit: 30000000 gas │
··················································|····························|·············|······························
| Methods │
··················|·······························|·············|··············|·············|···············|··············
| Contract · Method · Min · Max · Avg · # calls · usd (avg) │
··················|·······························|·············|··············|·············|···············|··············
| ERC404Example · setApprovalForAll · 24773 · 46685 · 40424 · 7 · - │
··················|·······························|·············|··············|·············|···············|··············
| ERC404Example · setERC721TransferExempt · 29287 · 204448 · 94214 · 5 · - │
··················|·······························|·············|··············|·············|···············|··············
| ERC404Example · setSelfERC721TransferExempt · 27026 · 46333 · 36680 · 2 · - │
··················|·······························|·············|··············|·············|···············|··············
| ERC404Example · transfer · 32509 · 5034556 · 385571 · 21 · - │
··················|·······························|·············|··············|·············|···············|··············
| MinimalERC404 · approve · 27039 · 51490 · 46518 · 13 · - │
··················|·······························|·············|··············|·············|···············|··············
| MinimalERC404 · mintERC20 · 43161 · 5058633 · 1717416 · 17 · - │
··················|·······························|·············|··············|·············|···············|··············
| MinimalERC404 · permit · 76946 · 77318 · 77132 · 4 · - │
··················|·······························|·············|··············|·············|···············|··············
| MinimalERC404 · safeTransferFrom · 130010 · 132655 · 131773 · 3 · - │
··················|·······························|·············|··············|·············|···············|··············
| MinimalERC404 · setApprovalForAll · 46584 · 46596 · 46590 · 2 · - │
··················|·······························|·············|··············|·············|···············|··············
| MinimalERC404 · setERC721TransferExempt · 49209 · 3205288 · 1451911 · 9 · - │
··················|·······························|·············|··············|·············|···············|··············
| MinimalERC404 · transfer · 59231 · 261414 · 120135 · 13 · - │
··················|·······························|·············|··············|·············|···············|··············
| MinimalERC404 · transferFrom · 58908 · 129681 · 103574 · 13 · - │
··················|·······························|·············|··············|·············|···············|··············
| Deployments · · % of limit · │
··················································|·············|··············|·············|···············|··············
| ERC404Example · - · - · 4135940 · 13.8 % · - │
··················································|·············|··············|·············|···············|··············
| ERC404ExampleUniswapV2 · - · - · 4205985 · 14 % · - │
··················································|·············|··············|·············|···············|··············
| ERC404ExampleUniswapV3 · - · - · 4334783 · 14.4 % · - │
··················································|·············|··············|·············|···············|··············
| MinimalERC404 · - · - · 4038373 · 13.5 % · - │
··················································|·············|··············|·············|···············|··············
| MockInvalidERC721Receiver · - · - · 198533 · 0.7 % · - │
··················································|·············|··············|·············|···············|··············
| MockValidERC721Receiver · - · - · 193569 · 0.6 % · - │
·-------------------------------------------------|-------------|--------------|-------------|---------------|-------------·

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

No branches or pull requests

1 participant