[Issue 13093] New: D ABI change for guaranteed efficient return of fixed size array

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Jul 11 01:16:51 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=13093

          Issue ID: 13093
           Summary: D ABI change for guaranteed efficient return of fixed
                    size array
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: DMD
          Assignee: nobody at puremagic.com
          Reporter: bearophile_hugs at eml.cc

I suggest 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[10000] foo() nothrow @safe {
    typeof(return) data;
    // Some code here.
    return data;
}
void main() nothrow {
    immutable data = foo();
}


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

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


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

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 (and more DRY code that avoids to
repeat ubyte[10000] more than once as it happens in the second program).


Dmitry Olshansky has commented:
http://forum.dlang.org/thread/xvtnfpsqlzxptjjfhibq@forum.dlang.org

> 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.

--


More information about the Digitalmars-d-bugs mailing list