Tuple assignment

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Sat May 9 13:07:12 PDT 2015


On 5/9/2015 10:16 AM, Russel Winder via Digitalmars-d wrote:
> Python has tuple assignment so you see things like:
>
>          previous, current = current, previous + current
>
> especially if you are doing silly things like calculating Fibonacci
> Sequence values. Translating this to D, you end up with:
>
>          TypeTuple!(current, next) = tuple(next , current +next);
>
> I am assuming this is horrendously inefficient at run time compared to
> having the intermediate value explicit:
>
>          auto t = next;
>          next = current + next;
>          current = t;
>
> or is it?

It probably depends on the compiler. The way to find out is to look at the 
generated assembler.

Tuples are implemented as structs. I know that ldc is capable of slicing structs 
into register-sized pieces and optimizing them independently, dmd does not. So 
ldc would very possibly generate the kind of code for that that you'd like.



More information about the Digitalmars-d mailing list