December 29, 2021

On 12/29/21 3:55 AM, rempas wrote:

>

Thanks! That's cool but I don't want this to be this way. Or at least I want it to be able to take a default value so we don't have to get passed all the time.
OK:

>
extern (C) void main() {
   void print_num(int mul = 100)(int num) {
     static if (is(mul == ten)) {
       printf("%d\n", num * 10);
     } else static if (is(mul == three)) {
       printf("%d\n", num * 3);
     } else {
       printf("%d\n", num);
     }
   }

   int multi = 211;
   print_num!3(10);     // Set the value
   print_num(30);        // Get the default value, have the "else" branch executed
}

Template parameters are just like regular parameters, but compile time.

-Steve

December 29, 2021

On 12/29/21 3:56 AM, rempas wrote:

>

On Tuesday, 28 December 2021 at 22:26:33 UTC, max haughton wrote:

>

Why do you need this? What's wrong with a normal branch in this case.

Runtime performance. I want the value to get checked at compile time and use "static if" with it

Oof, just let the compiler do its job. Inlining and optimization will take care of this for you (for the most part).

-Steve

December 29, 2021

On Wednesday, 29 December 2021 at 11:09:04 UTC, max haughton wrote:

>

If the value is known at compile time the compiler can pretty easily do that for you unless you're really unlucky.

How is this even possible?

December 29, 2021

On Wednesday, 29 December 2021 at 14:49:40 UTC, Steven Schveighoffer wrote:

>

On 12/29/21 3:55 AM, rempas wrote:

>

Thanks! That's cool but I don't want this to be this way. Or at least I want it to be able to take a default value so we don't have to get passed all the time.
OK:

>
extern (C) void main() {
   void print_num(int mul = 100)(int num) {
     static if (is(mul == ten)) {
       printf("%d\n", num * 10);
     } else static if (is(mul == three)) {
       printf("%d\n", num * 3);
     } else {
       printf("%d\n", num);
     }
   }

   int multi = 211;
   print_num!3(10);     // Set the value
   print_num(30);        // Get the default value, have the "else" branch executed
}

Template parameters are just like regular parameters, but compile time.

-Steve

Oh! Didn't knew you could do something like that! It works as expected! Thanks a lot!

December 29, 2021

On Wednesday, 29 December 2021 at 15:53:38 UTC, rempas wrote:

>

On Wednesday, 29 December 2021 at 11:09:04 UTC, max haughton wrote:

>

If the value is known at compile time the compiler can pretty easily do that for you unless you're really unlucky.

How is this even possible?

Inlining + constant propagation. Fancier iterations on those exist too but 90% of the speedup will come from those since for it to matter they likely would've been used in first place.

December 29, 2021

On Wednesday, 29 December 2021 at 16:27:22 UTC, max haughton wrote:

>

Inlining + constant propagation. Fancier iterations on those exist too but 90% of the speedup will come from those since for it to matter they likely would've been used in first place.

Sounds like black magic? So If I write this:

int add(int num, int num2) { return num1 + num2; }

void main() {
  int number = add(10, 20);
}

The parameters are literals so will D translate this to:

int add(int num, int num2) { return num1 + num2; } // Normal one
int add_temp_func() { return 30; } // Created for the function call in main. No `add` instruction

void main() {
  int number = add(10, 20); // Will actually create and call "add_temp_func"
}

Or even better, this:

int add(int num, int num2) { return num1 + num2; }
void main() {
  int number = add(10, 20); // What we will type and it will get replaced with the following line
  int number = 30; // So it calculates the result at compile times and doesn't even do a function call
}

Is this what D can do? This is what I'm talking about when saying been able to use values at compile time.

December 29, 2021

On Wednesday, 29 December 2021 at 16:51:47 UTC, rempas wrote:

>

On Wednesday, 29 December 2021 at 16:27:22 UTC, max haughton wrote:

>

Inlining + constant propagation. Fancier iterations on those exist too but 90% of the speedup will come from those since for it to matter they likely would've been used in first place.

Sounds like black magic? So If I write this:

int add(int num, int num2) { return num1 + num2; }

void main() {
  int number = add(10, 20);
}

The parameters are literals so will D translate this to:

int add(int num, int num2) { return num1 + num2; } // Normal one
int add_temp_func() { return 30; } // Created for the function call in main. No `add` instruction

void main() {
  int number = add(10, 20); // Will actually create and call "add_temp_func"
}

Or even better, this:

int add(int num, int num2) { return num1 + num2; }
void main() {
  int number = add(10, 20); // What we will type and it will get replaced with the following line
  int number = 30; // So it calculates the result at compile times and doesn't even do a function call
}

Is this what D can do? This is what I'm talking about when saying been able to use values at compile time.

This is handled by the compiler backend. The simplest way it can do this kind of optimization is by "inlining" the function.

This is done by transplanting the function body into the place it's used. At this point the compiler simply sees "= 30 + 30" which it can trivially turn into "= 60" through something called constant-folding.

The compiler can create new function bodies (like the temp one you introduce above) but this is a much more niche optimization. They favour inlining much more aggressively.

I'm tempted to do a YouTube video of a D program being compiled all the way down the machine code, to show what the compiler does for you.

December 29, 2021

On Wednesday, 29 December 2021 at 17:20:59 UTC, max haughton wrote:

>

This is handled by the compiler backend. The simplest way it can do this kind of optimization is by "inlining" the function.

This is done by transplanting the function body into the place it's used. At this point the compiler simply sees "= 30 + 30" which it can trivially turn into "= 60" through something called constant-folding.

The compiler can create new function bodies (like the temp one you introduce above) but this is a much more niche optimization. They favour inlining much more aggressively.

I'm tempted to do a YouTube video of a D program being compiled all the way down the machine code, to show what the compiler does for you.

I didn't knew that compilers work like that. I thought that it will treat the values normally like they were values that cannot get computed at compile time even if you pass them as literals. That's good to know

1 2
Next ›   Last »