Discussion Thread: DIP 1040--Copying, Moving, and Forwarding--Community Review Round 1

Max Haughton maxhaton at gmail.com
Fri Mar 5 22:04:27 UTC 2021


On Friday, 5 March 2021 at 20:29:01 UTC, tsbockman wrote:
> On Friday, 5 March 2021 at 18:35:39 UTC, Ben Jones wrote:
>> Second, it might be good to include a discussion of how 
>> structs are passed at the ABI level (I assume that's in the 
>> spec, but it would be helpful context to just highlight in the 
>> DIP).  My understanding is that this proposal doesn't actually 
>> change the function ABIs at all (for struct parameters and 
>> return values the caller always passes a pointer to the 
>> appropriate structs).
>
> Very small structs are often placed in registers when passed by 
> value. This is true both arguments, and return values. For 
> example, when compiled with LDC for AMD64, the function below 
> does not use any stack memory at all - only registers.
>
> struct S {
>     int* ptr;
>     size_t length;
> }
>
> S sliceroo(S s) {
>     const bump = int(s.length >= 2);
>     return S(s.ptr + bump, s.length - bump);
> }
>
> (You can check out the disassembly online with the 
> https://godbolt.org/ Compiler Explorer.)
>
> This optimization would need to be disabled for any type with a 
> move constructor.

This is true, however it's worth saying that typically the reason 
for having move semantics isn't so much performance (for small 
objects) but managing the lifetime of the struct properly. In 
this case for move semantics to be useful S would probably have a 
destructor, which prevents passing in registers anyway.


More information about the Digitalmars-d mailing list