calldata
?calldata
is a non-modifiable, temporary, read-only data location, primarily used for storing function arguments passed during external calls in the Ethereum Virtual Machine (EVM). It is used to handle input data for external function calls in smart contracts.
Unlike other data locations like memory
and storage
, calldata
is optimized for efficiency. It is particularly useful for lightweight data that doesn’t require modifications or long-term storage
.
calldata
calldata
is used to pass information to smart contract functions, providing the necessary inputs for the function to perform its intended operations.
When an external user or another contract interacts with a function, the input arguments are encoded and sent as calldata
. The EVM then processes it and executes the requested function.
calldata
is used:calldata
for large arrays avoids the gas costs of copying data into memory
, making the transaction less expensive to execute.calldata
ensures inputs, such as digital signatures, remain immutable and secure from tampering during execution.calldata
to reduce gas costs.calldata
for temporary inputs without incurring storage costs.calldata
for gas-efficient, immutable data handling.
calldata
Data in calldata
cannot be modified once set, making it ideal for handling function arguments without risking accidental or malicious changes.
calldata
exists only during function execution and is discarded afterward, enabling efficient input processing without requiring long-term storage
.
calldata
avoids the high gas costs of memory
or storage
operations, making it cost-effective for handling function arguments in smart contracts.
Functions accessing calldata
can only read it, reinforcing its immutability and ensuring the safety of external inputs without risking unintended state changes.
calldata
vs. other data locationsIn EVM smart contracts, there are four main data locations, each with distinct purposes.
Note: Transient storage is not yet supported natively in Solidity (but is expected soon). For now, it can be implemented using assembly.
Here’s a quick comparison:
Property | calldata |
memory |
storage |
Transient storage |
---|---|---|---|---|
Modifiability | Immutable (read-only) | Modifiability | Modifiability | Modifiability |
Lifespan | Temporary (function execution only) | Temporary (function execution only) | Persistent (stored on the blockchain) | Temporary (persists during the transaction but is reset after execution) |
Gas costs | Low (no write costs) | Moderate (data must be copied and stored temporarily in memory) | High (writing to storage is costly) | Low to Moderate (cheaper than permanent storage, but has write costs) |
Primary use | Function arguments for external calls | Intermediate computations | Long-term storage of contract data | Temporary data sharing across functions in a single transaction |
calldata
is the most lightweight and gas-efficient option for short-lived, read-only data, making it particularly useful for handling function inputs.
calldata
in SolidityHere is an example of how calldata
is used in a Solidity smart contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract CalldataExample {
// A function that takes an immutable array as calldata
function processInput(uint256[] calldata inputArray) external pure returns (uint256) {
uint256 sum = 0;
// Iterate over the array to compute the sum
for (uint256 i = 0; i < inputArray.length; i++) {
sum += inputArray[i];
}
return sum;
}
// A function that accepts a single string as calldata
function echoMessage(string calldata message) external pure returns (string memory) {
return message; // Simply return the input message
}
}
Explanation of the code:
processInput
Function:uint256[]
) as calldata
.inputArray
is defined as calldata
, it is immutable and can only be read during function execution.echoMessage
Function:calldata
and returns it as a response.
calldata
data// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract CalldataErrorExample {
// Function that takes an array as calldata
function modifyArray(uint256[] calldata inputArray) external pure returns (uint256[] memory) {
// Attempting to modify a calldata parameter - This will cause a compilation error
inputArray[0] = 42; // Error: calldata is immutable and cannot be modified
// To fix this, the developer would need to copy the calldata into memory
return inputArray;
}
}
modifyArray
takes an array of unsigned integers (uint256[]
) as a calldata
parameter, which makes the array immutable and read-only.inputArray[0] = 42
; attempts to assign a new value to the first element of the array, but this fails because calldata
cannot be modified.calldata
to optimize gas usage and ensure data integrity. Any attempt to modify data stored in calldata
results in a compilation error.