Do not allocate memory objects if they will be assigned directly.

This commit is contained in:
chriseth
2020-11-24 14:11:01 +01:00
parent 6f8435301e
commit 409e92580f
9 changed files with 126 additions and 5 deletions
@@ -0,0 +1,24 @@
contract C {
struct S { uint x; uint y; uint z; }
function memorySize() internal pure returns (uint s) {
assembly { s := mload(0x40) }
}
function withValue() public pure returns (uint) {
S memory x = S(1, 2, 3);
uint memorySizeBefore = memorySize();
S memory t = x;
uint memorySizeAfter = memorySize();
return memorySizeAfter - memorySizeBefore;
}
function withoutValue() public pure returns (uint) {
uint memorySizeBefore = memorySize();
S memory t;
uint memorySizeAfter = memorySize();
return memorySizeAfter - memorySizeBefore;
}
}
// ====
// compileViaYul: also
// ----
// withValue() -> 0x00
// withoutValue() -> 0x60