<div class="gmail_quote">On 7 January 2012 20:59, bearophile <span dir="ltr"><<a href="mailto:bearophileHUGS@lycos.com">bearophileHUGS@lycos.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Manu:<br>
<div class="im"><br>
> The tendency to encourage use of dynamic arrays will be a major problem.<br>
<br>
</div>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)).<br>
</blockquote><div><br></div><div>A slice doesn't produce a GC allocation does it?</div><div>I thought a slice was just a pointer-length pair. Should live on the stack/in regs?</div><div><br></div><div>...so slicing static arrays shouldn't be a problem right?</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Currently even this code with a stack-allocated fixed size array causes a heap allocation (DMD):<br>
<br>
void main() {<br>
    int[2] a = [1, 2];<br>
}<br>
<br>
ASM, optimized build:<br>
<br>
__Dmain comdat<br>
    push EAX<br>
    push EAX<br>
    mov EAX,offset FLAT:_D12TypeInfo_xAi6__initZ<br>
    push EBX<br>
    push 8<br>
    push 2<br>
    push EAX<br>
    call near ptr __d_arrayliteralTX ; heap allocation<br>
    add ESP,8<br>
    mov EBX,EAX<br>
    mov dword ptr [EAX],1<br>
    mov ECX,EBX<br>
    push EBX<br>
    lea EDX,0Ch[ESP]<br>
    mov dword ptr 4[EBX],2<br>
    push EDX<br>
    call near ptr _memcpy<br>
    add ESP,0Ch<br>
    xor EAX,EAX<br>
    pop EBX<br>
    add ESP,8<br>
    ret<br></blockquote><div><br></div><div>What the hell is it allocating?</div><div>Surely that's not necessary... that's gotta be fixable?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I have suggested to add a safer version of VLAs to avoid some heap-allocated dynamic arrays:<br>
<a href="http://d.puremagic.com/issues/show_bug.cgi?id=5348" target="_blank">http://d.puremagic.com/issues/show_bug.cgi?id=5348</a></blockquote><div><br></div><div>+1!!</div><div>I'm surprised this already isn't supported!</div>
</div>