Open PhilJay opened 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.
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 · - │
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.
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