On Sunday, 11 March 2012 at 06:49:27 UTC, H. S. Teoh wrote:
On Sun, Mar 11, 2012 at 01:29:01AM -0500, Nick Sabalausky wrote:
Suppose you have a delegate literal and immediately call it:

auto a = x + (){ doStuff(); return y; }() + z;

Does DMD ever (or always?) optimize away a delegate if it's executed immediately and never stored into a variable? If not, can it, and would it be a simple change? Is something like this already on the
table?
[...]

I've always wondered about whether delegates passed to opApply ever get
inlined.

Don't wonder. Find out!

import std.stdio;
void doStuff() { writeln("Howdy!"); }
void main() {
    int x = 1, y = 2, z = 3;
    auto a = x + (){ doStuff(); return y; }() + z;
    writeln(a);
}

$ dmd test.d -O -release -inline

__Dmain:
000000010000106c        pushq   %rbp
000000010000106d        movq    %rsp,%rbp
0000000100001070        pushq   %rax
0000000100001071        pushq   %rbx
0000000100001072        movq    $0x000000000000000c,%rdi
000000010000107c callq 0x1000237f0 ; symbol stub for: __d_allocmemory
0000000100001081        movq    %rax,%rbx
0000000100001084        movq    $0x00000000,(%rbx)
000000010000108b        movl    $0x00000002,0x08(%rbx)
0000000100001092        movq    %rbx,%rdi
0000000100001095        call    *0x0002318c(%rip)
000000010000109c        leal    0x04(%rax),%edx
000000010000109f        movl    $0x0000000a,%esi
00000001000010a4        leaq    0x00033eed(%rip),%rdi
00000001000010ab callq 0x10002319c ; symbol stub for: _D3std5stdio4File14__T5writeTiTaZ5writeMFiaZv
00000001000010b0        xorl    %eax,%eax
00000001000010b2        popq    %rbx
00000001000010b3        movq    %rbp,%rsp
00000001000010b6        popq    %rbp
00000001000010b7        ret

In short. No! It doesn't currently inline in this case.

Even if the lambda just returns a constant, it doesn't get inlined.

Reply via email to