mouse-dev-1 / Akutar

15k randomized tiered airdrop collection for Akutar.
12 stars 9 forks source link

external vs. public #6

Open PhilJay opened 2 years ago

PhilJay commented 2 years ago

More an optimization suggestion rather than an issue: Apparently using external over public can save some gas when working with arrays: https://ethereum.stackexchange.com/a/19391

 function airdrop(uint256 airdropGrouping, address[] memory addresses)
        public // use external
        onlyOwner
    {
sonya75 commented 2 years ago

This only applies for old versions of solidity. Now you can explicitly mention calldata or memory for arrays. And using calldata in this particular case will definitely be better than using memory if they are planning to airdrop to a large number of addresses in every call.

0xildefonso commented 2 years ago

the gas saving is not that much, using tests from this repo

original

·------------------------|---------------------------|-------------|-----------------------------·
|  Solc version: 0.8.4   ·  Optimizer enabled: true  ·  Runs: 200  ·  Block limit: 29000000 gas  │
·························|···························|·············|······························
|  Methods                                                                                       │
·············|···········|·············|·············|·············|···············|··············
|  Contract  ·  Method   ·  Min        ·  Max        ·  Avg        ·  # calls      ·  eur (avg)  │
·············|···········|·············|·············|·············|···············|··············
|  Akutar    ·  airdrop  ·     375407  ·    4986387  ·    3308524  ·          153  ·          -  │

using address[] calldata addresses and external

·------------------------|---------------------------|-------------|-----------------------------·
|  Solc version: 0.8.4   ·  Optimizer enabled: true  ·  Runs: 200  ·  Block limit: 29000000 gas  │
·························|···························|·············|······························
|  Methods                                                                                       │
·············|···········|·············|·············|·············|···············|··············
|  Contract  ·  Method   ·  Min        ·  Max        ·  Avg        ·  # calls      ·  eur (avg)  │
·············|···········|·············|·············|·············|···············|··············
|  Akutar    ·  airdrop  ·     375295  ·    4986141  ·    3308287  ·          153  ·          -  │
sonya75 commented 2 years ago

That's actually a good observation. I just checked it and noticed that, solidity complier optimization process doesn't actually copy the array to memory unless you actually modify the array. This is even if you use memory instead of calldata. So if you add something like addresses[i]=...., then it will copy the whole array to memory at the beginning of the call.