On 08-01-2012 00:06, Manu wrote:
On 7 January 2012 20:59, bearophile <bearophileh...@lycos.com
<mailto:bearophileh...@lycos.com>> wrote:

    Manu:

     > The tendency to encourage use of dynamic arrays will be a major
    problem.

    I don't know how much big that problem will be, D dynamic arrays are
    quite handy, but I agree that static arrays need to be encouraged
    more in D (and currently most algorithms of Phobos don't work with
    static arrays (you need to slice them first)).


A slice doesn't produce a GC allocation does it?
I thought a slice was just a pointer-length pair. Should live on the
stack/in regs?

AFAIK yes.


...so slicing static arrays shouldn't be a problem right?

    Currently even this code with a stack-allocated fixed size array
    causes a heap allocation (DMD):

    void main() {
        int[2] a = [1, 2];
    }

    ASM, optimized build:

    __Dmain comdat
        push EAX
        push EAX
        mov EAX,offset FLAT:_D12TypeInfo_xAi6__initZ
        push EBX
        push 8
        push 2
        push EAX
        call near ptr __d_arrayliteralTX ; heap allocation
        add ESP,8
        mov EBX,EAX
        mov dword ptr [EAX],1
        mov ECX,EBX
        push EBX
        lea EDX,0Ch[ESP]
        mov dword ptr 4[EBX],2
        push EDX
        call near ptr _memcpy
        add ESP,0Ch
        xor EAX,EAX
        pop EBX
        add ESP,8
        ret


What the hell is it allocating?
Surely that's not necessary... that's gotta be fixable?

    I have suggested to add a safer version of VLAs to avoid some
    heap-allocated dynamic arrays:
    http://d.puremagic.com/issues/show_bug.cgi?id=5348


+1!!
I'm surprised this already isn't supported!


--
- Alex

Reply via email to