`in` parameters made useful

Kagamin spam at here.lot
Thu Aug 27 15:38:11 UTC 2020


On Tuesday, 25 August 2020 at 16:05:51 UTC, Mathias LANG wrote:
> On Tuesday, 25 August 2020 at 12:09:45 UTC, Kagamin wrote:
>>
>> The backend doesn't know how to load the data. It matters to 
>> the programmer, because it affects performance.
>>
>> Another example:
>> void log(in ref int n)
>> {
>>   write(fd, &n, n.sizeof);
>> }
>> If the argument here is passed by value, it will need to 
>> fiddle with stack and the code will be less optimal.
>
> I assume you imply that it's less optimal because it's taking 
> the address of it ?

Because it has an extra write to memory, pushing the parameter 
passed by value to stack to take its address. In general when the 
parameter is passed by reference somewhere, like the refactoring 
you did in phobos replacing `in ref` with `scope const ref` - the 
parameter can be passed to one of those functions that have 
precise ref semantics. I also wonder how they interact with auto 
ref.

>> If a non-POD object is passed by value, it will have extra 
>> calls to postblit and destructor.
>
> https://github.com/dlang/dmd/blob/master/changelog/preview-in.dd
>
>> - If the type has an elaborate copy or destruction (postblit, 
>> copy constructor, destructor),
>> the type is always passed by reference.

If ref semantics doesn't depend on parameter position, then it's 
fine, but you say it depends.


More information about the Digitalmars-d mailing list