So why doesn't popFront return an element?

Timon Gehr timon.gehr at gmx.ch
Thu Apr 14 10:36:07 PDT 2011


Andrei Mitrovic:
> Can the compiler optimize the second case and convert b.front to just
do one field access?

In simple cases, obviously yes:

import std.range;
import std.stdio;

void main(){
    int[] a=new int[1000];
    auto b=retro(retro(a));
    writeln(b.front);
}

Produces:

.text:08094B64                 public _Dmain
.text:08094B64 _Dmain          proc near               ; CODE XREF:
_D2rt6dmain24mainUiPPaZi7runMainMFZv+15p
.text:08094B64                 push    ebp
.text:08094B65                 mov     ebp, esp
.text:08094B67                 mov     eax, offset _D11TypeInfo_Ai6__initZ
.text:08094B6C                 push    3E8h
.text:08094B71                 push    eax
.text:08094B72                 call    _d_newarrayT
.text:08094B77                 add     esp, 8
.text:08094B7A                 mov     eax, offset _D3std5stdio6stdoutS3std5stdio4File
.text:08094B7F                 push    dword ptr [edx]
.text:08094B81                 push    0Ah
.text:08094B83                 call    _D3std5stdio4File14__T5writeTiTaZ5writeMFiaZv
.text:08094B88                 xor     eax, eax
.text:08094B8A                 pop     ebp
.text:08094B8B                 retn
.text:08094B8B _Dmain          endp ; sp = -8

DMD even recognizes the fact, that edx contains the pointer to the first element of b.

I do not know about more complex cases, I think it depends on DMD's inlining caps,
which are (naturally) somewhat poorer than gcc's at the moment.



More information about the Digitalmars-d-learn mailing list