Optional data locations with compiler optimization

Summary:

I propose making the data locations memory and calldata optional in function parameters. This enhancement would allow the Solidity compiler to automatically choose the most gas-efficient data location. The default behavior would assume calldata for external function parameters, with the compiler capable of upgrading this to memory when necessary.

Problem:

Currently, Solidity requires developers to explicitly specify data locations (memory, calldata or storage) for reference types in function parameters and returns. This requirement can be a barrier for new users and can lead to inefficiencies if not optimally chosen. Moreover, as the compiler already identifies incorrect data location usage (e.g., passing calldata as memory), it indicates a potential to automate this selection process.

Proposed Solution:

Implement compiler intelligence to automatically determine the most efficient data location for function parameters. The default assumption for external function parameters would be calldata, with automatic upgrades to memory as the use case demands (e.g., when modifications to the data are needed).

Benefits:

  1. Lower Barrier to Entry: New developers would find Solidity more approachable, reducing the initial learning curve associated with understanding data locations.
  2. Optimization: Automatic data location selection would ensure gas cost optimizations without manual intervention.
  3. Safety: Reducing manual specification could decrease the likelihood of bugs related to incorrect data location usage.

Considerations:

  • Providing experienced developers an option to override automatic selections is paramount.

Example

// Current requirement: 
function example(address[] calldata addresses) external {
    // function body 
}

// Proposed change: 
function example(address[] addresses) external {
    // Compiler decides whether `addresses` should be `calldata` or `memory`
}