D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 13093 - D ABI change for guaranteed efficient return of fixed size array
Summary: D ABI change for guaranteed efficient return of fixed size array
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dlang.org (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2014-07-11 08:16 UTC by bearophile_hugs
Modified: 2015-02-18 03:40 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2014-07-11 08:16:51 UTC
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.
Comment 1 Kenji Hara 2014-07-11 09:36:04 UTC
It's already supported, but not explicitly described in ABI page.

From http://dlang.org/abi

Return Value
...
* 1, 2 and 4 byte structs are returned in EAX.
* 8 byte structs are returned in EDX,EAX, where EDX gets the most significant half.
* For other struct sizes, the return value is stored through a hidden pointer passed as an argument to the function.
...

Normally static arrays are treated as the same size structs, so ubyte[1000] is always returned through hidden pointer.

https://github.com/D-Programming-Language/dlang.org/pull/611
Comment 2 Kenji Hara 2014-07-11 09:39:01 UTC
You can test it by the code.

void* p;
ubyte[10000] foo() nothrow {
    typeof(return) data;
    p = &data;
    return data;
}
void main() nothrow {
    immutable data = foo();
    assert(p == &data);
}
Comment 3 github-bugzilla 2014-11-27 12:50:32 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dlang.org

https://github.com/D-Programming-Language/dlang.org/commit/70813e9b9fe6152c83a6d24ea44080a3697a5b00
fix Issue 13093 - D ABI change for guaranteed efficient return of fixed size array

https://github.com/D-Programming-Language/dlang.org/commit/22697d464ece7d64ba5cc2792a0863a5ffbaa88d
Merge pull request #611 from 9rnsr/fix13093

Issue 13093 - D ABI change for guaranteed efficient return of fixed size array
Comment 4 github-bugzilla 2015-02-18 03:40:19 UTC
Commits pushed to 2.067 at https://github.com/D-Programming-Language/dlang.org

https://github.com/D-Programming-Language/dlang.org/commit/70813e9b9fe6152c83a6d24ea44080a3697a5b00
fix Issue 13093 - D ABI change for guaranteed efficient return of fixed size array

https://github.com/D-Programming-Language/dlang.org/commit/22697d464ece7d64ba5cc2792a0863a5ffbaa88d
Merge pull request #611 from 9rnsr/fix13093