Open pde-rent opened 9 months ago
Sadly, the compiler is much less smarter than people think. You can attempt the optimization yourself and observe that all warm SLOAD
operations are not present:
contract Foo {
uint256[] public values;
constructor() {
for (uint256 i = 0; i< 100; i++) {
values.push(i);
}
}
// 259524 w/ limit as values.length and optimizations on
// 249525 w/ limit as 100 and optimizations on
function test() external view returns (uint256 total) {
for (uint256 i = 0; i < 100; i++) {
total += values[i];
}
}
}
The compiler is unable to detect whether the dynamic type the length
is evaluated from is mutated within its body.
Insightful, thank you @omniscia-core. I will make sure the team gets compiler-versed.
This seems to me like a basic compiler optimization, are you sure that solc+pragma >8.20 does not take care of it already?