Calldata in Solidity is the read-only data passed to a smart contract function when it's called externally. It contains the encoded function selector(bytes4) and all the function arguments( encoded in bytes32).
calldata contains:
- Function selector (first 4 bytes) — The function's keccak256 hash truncated to 4 bytes
- Function arguments (remaining bytes) — Encoded parameters passed to the function
Important:
Use calldata for function parameters when you:
- Only need to read the data
- Want to save gas by avoiding memory copies
- Are working with arrays or strings that won't be modified
Calldata vs Memory vs Storage:
| Feature | Location | Type | Gas Cost | Persistence | Use Case |
|---|---|---|---|---|---|
| Calldata | Read-only | Cheapest | Temporary | Function inputs | |
| Memory | Read/Write | Medium | Temporary | Local variables, arrays | |
| Storage | Read/Write | Most expensive | Permanent | State variables |
Top comments (0)