About structs and performant handling

deadalnix deadalnix at gmail.com
Tue Mar 12 07:59:06 PDT 2013


On Tuesday, 12 March 2013 at 11:24:43 UTC, Namespace wrote:
> On Tuesday, 12 March 2013 at 09:38:19 UTC, deadalnix wrote:
>> On Tuesday, 12 March 2013 at 09:24:11 UTC, Namespace wrote:
>>>> You never gave any rationale reason on that.
>>>
>>> Because I just like to have the control over what is passed 
>>> by ref and what by value.
>>
>> I'm sorry, but what you like is exactly the opposite of a 
>> rational example.
>
> I'll always like to disabuse. So why do you think this is not a 
> rational example? And what would a rational example for you?

Let's consider the given example :

struct Foo {
     uint i;
     this(this) {
         writeln("foo");
     }
}

void bar(Foo f) {
     writeln("bar");
}

uint main() {
     Foo f;
     f.i = 42;
     bar(f);
     return f.i,
}

You think you pass by value ? In fact, if you let the optimizer 
do its job, you don't even have a function call. Thing gets 
rewriten this way :

uint main() {
     writeln("foo");
     writeln("bar");
     return 42;
}

You have no function call and no pass by value anymore. In fact, 
the only thing the current definition garantee it that you have 
the side effect of the postblit (ie, memory allocation, etc . . 
.), but nothing is said about having an actual function call or a 
pass by value.

If you think you control the code that way, you are living a 
delusional world, because the compiler rewrite everything under 
you. You are asking to keep that illusion in place by asking for 
an explicit syntax to allow the optimization to take place.

And note that this is very good thing as the code generated is 
much faster and the code much cleaner. If you had to specify 
optimisations, you'd get them wrong, you'd forget them, your code 
would be less reliable, slower and less readable.


More information about the Digitalmars-d mailing list