Cyfrin / solidity-by-example.github.io

Solidity By Example
https://solidity-by-example.org/
MIT License
614 stars 201 forks source link

improvement: using calldata instead of memory #219

Closed qd-qd closed 2 years ago

qd-qd commented 2 years ago

In the examples I updated, the argument has the storage location memory. When the functions get called externally, the arguments are kept in calldata and copied to memory during ABI decoding (using the opcode calldataload and mstore). When the argument is read in the function, the value is accesses in memory using a mload. We can avoid all this logic by simply using calldata for read-only arguments. In my proposal, instead of going via memory, the value is directly read from calldata using calldataload. I think this is important to expose good practices like this one to the user.


What do you think about this @t4sk? I'm ready to open a PR that changes memory argument to calldata argument when needed.

t4sk commented 2 years ago

What do you think about this @t4sk? I'm ready to open a PR that changes memory argument to calldata argument when needed

Good suggestion. Merged