Skip to content

enhance {_setOwnedIndex} to reduce the gas consumption #48

@RevelationOfTuring

Description

@RevelationOfTuring

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 % · - │
·-------------------------------------------------|-------------|--------------|-------------|---------------|-------------·

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions