Table of Contents

What is 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.

Purpose of 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.

Examples of how calldata is used:

  • Passing large arrays in external calls: Using calldata for large arrays avoids the gas costs of copying data into memory, making the transaction less expensive to execute.
  • Immutable input for security: calldata ensures inputs, such as digital signatures, remain immutable and secure from tampering during execution.
  • Read-only data for efficiency: Functions that only read data, like fetching user details, can use calldata to reduce gas costs.
  • Temporary input in stateless functions: Stateless functions, such as hash functions or sum calculations, use calldata for temporary inputs without incurring storage costs.
  • Oracles or external data feeds: Smart contracts using Oracles (e.g., price feeds) rely on calldata for gas-efficient, immutable data handling.

Key features of calldata

Immutable

Data in calldata cannot be modified once set, making it ideal for handling function arguments without risking accidental or malicious changes.

Temporary

calldata exists only during function execution and is discarded afterward, enabling efficient input processing without requiring long-term storage.

Gas efficient

calldata avoids the high gas costs of memory or storage operations, making it cost-effective for handling function arguments in smart contracts.

Read only

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 locations

In 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.

Code example of calldata in Solidity

Here 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:

  1. processInput Function:
    • This function accepts an array of unsigned integers (uint256[]) as calldata.
    • It computes and returns the sum of all elements in the array.
    • Since inputArray is defined as calldata, it is immutable and can only be read during function execution.
  2. echoMessage Function:
    • This function takes a string as calldata and returns it as a response.
    • Using calldata here ensures the function remains gas-efficient and prevents modifications to the input.

Code example: error when trying to modify 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;
    }
}

Explanation of the code:

  • The function modifyArray takes an array of unsigned integers (uint256[]) as a calldata parameter, which makes the array immutable and read-only.
  • The line inputArray[0] = 42; attempts to assign a new value to the first element of the array, but this fails because calldata cannot be modified.
  • Solidity enforces the immutability of calldata to optimize gas usage and ensure data integrity. Any attempt to modify data stored in calldata results in a compilation error.

Related Terms

No items found.