Comment #0 by dlang-bugzilla — 2021-07-30T09:02:07Z
void fun(int a, int b)
{
for (int i = a; i < b; i++) {}
}
With -O -inline -release, DMD compiles the above function to:
push RBP
mov RBP,RSP
mov ECX,ESI
cmp ESI,EDI
jge $+7h
inc ECX
cmp ECX,EDI
jl $+FFFFFFFBh
pop RBP
ret
LDC and GDC (with their respective optimization flags on) both compile it to just a "ret".
This optimization could help writing simpler code in performance-critical areas, such as the GC:
https://github.com/dlang/druntime/pull/3523#discussion_r676094354
Comment #1 by b2.temp — 2021-07-30T10:06:37Z
I suggest to edit the tittle:
Optimize away empty loops -> Optimize away empty loops that have no side effect
because the Test and the Increment can do things even if the loop body is empty
Comment #2 by robert.schadek — 2024-12-13T19:17:44Z