Fixed size array return

bearophile via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 10 02:36:07 PDT 2014


(This is a duplicated post of D.learn.)

Is it possible and a good idea to change the D ABI to make code 
like this avoid an array copy in 100% of the cases (without 
inlining, and regardless the optimization levels, and in all D 
compilers)?


ubyte[1000] foo() nothrow @safe {
     typeof(return) data;
     // Some code here.
     return data;
}
void main() nothrow {
     immutable data = foo();
}


That means that code is equivalent to (also note the need for the 
explicit cast):

void foo(ref ubyte[1000] __data) nothrow @safe {
     __data[] = 0;
     // Some code here.
}
void main() nothrow {
     ubyte[1000] __data = void;
     foo(__data);
     immutable data = cast(immutable ubyte[1000])__data;
}

If the returned fixed-size array is very small (like one or two 
CPU words, the new ABI can specify it's returned by value).


In my @nogc code I'd like to use fixed-size arrays, so it's nice 
to be sure they are _always_ returned efficiently, and at the 
same time keep a nice syntax that allows me to tag the result as 
immutable.

Bye,
bearophile


More information about the Digitalmars-d mailing list