10-Jul-2014 13:36, bearophile пишет:
(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.


IMO this is a good idea and it pretty much NRVO/RVO for structs extended to fixed-size arrays (which more or less a special kind of struct). Since C/C++ do not have fixed-size arrays passed by value I see no damage to ABI compatibility.


--
Dmitry Olshansky

Reply via email to