solidity icon indicating copy to clipboard operation
solidity copied to clipboard

Inefficient code generation for empty dynamic storage array assignment in IR pipeline

Open lum7na opened this issue 2 weeks ago • 0 comments

When assigning a newly pushed (and thus empty) dynamic storage array to another, the IR-based pipeline (--via-ir) generates less efficient code compared to the legacy pipeline.

We discovered this inefficiency by analyzing the storage trace of the following contract:

contract C {
    int8[][] array;

    function s() public {
        array.push() = array.push();
    }
}

The trace shows that the IR pipeline generates an extra, unnecessary SLOAD operation. This operation attempts to read the first data element from the source array (the right-hand side array.push()), even though the array's length is zero.

The legacy pipeline correctly deduces that the source array is empty and avoids accessing its data area altogether, resulting in more optimized bytecode.

The IR pipeline appears to use a more generic array copy mechanism that calculates the data area's starting address (keccak256(keccak256(p))) and performs a read before checking the array's length. This leads to a redundant storage read and unnecessary gas consumption.

lum7na avatar Nov 26 '25 11:11 lum7na