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

Reply via email to