On Tue, 26 Mar 2013 23:21:35 -0400, deadalnix <deadal...@gmail.com> wrote:

On Tuesday, 26 March 2013 at 23:57:32 UTC, bearophile wrote:
Timon Gehr:

IOW, what is the point of "null" if you can just use [].

I usually prefer to use [], because null is a literal for pointers and class references, while [] is a literal specific for arrays (and strings), so its meaning is more clear (in D I'd even like a [:] literal that represents an empty associative array).

On the other hand if you compile a program that uses null instead of [] you see some differences. In the current dmd compiler returning null is more efficient. I have seen code where this difference in performance matters:


int[] foo() {
    return [];
}
int[] bar() {
    return null;
}
void main() {}



_D4temp3fooFZAi:
L0:     push    EAX
        mov EAX,offset FLAT:_D11TypeInfo_Ai6__initZ
        push    0
        push    EAX
        call    near ptr __d_arrayliteralTX
        mov EDX,EAX
        add ESP,8
        pop ECX
        xor EAX,EAX
        ret

_D4temp3barFZAi:
        xor EAX,EAX
        xor EDX,EDX
        ret

Bye,
bearophile

That is a compiler bug isn't it ?

No. [] calls the hook for _d_arrayliteral, whose source is not known at compile time. Runtime functions cannot be inlined, which will be set in stone once the runtime is a dynamic library.

-Steve

Reply via email to