So why doesn't popFront return an element?

Steven Schveighoffer schveiguy at yahoo.com
Thu Apr 14 10:46:44 PDT 2011


On Thu, 14 Apr 2011 13:36:07 -0400, Timon Gehr <timon.gehr at gmx.ch> wrote:

> 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+15%19p
> .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.
>

Nah, it's simpler than that:

assert(is(typeof(b) == int[]));

which is done by retro (it detects if you're retro-ing a retro range, and  
just returns the original).

-Steve


More information about the Digitalmars-d-learn mailing list