On Monday, 1 October 2012 at 19:22:37 UTC, Philippe Sigaud wrote:
Something I wanted to ask for a long time: is there any runtime
speed penalty in using __ctfe?
No. What happens is when it goes to the compile the runtime code,
__ctfe is a constant false, so then the optimizer can see it is
obviously dead code and eliminate the branch entirely. You don't
even have to use the -O switch to get this:
void test() {
if(__ctfe) {
asm { nop; nop; nop; nop; }
} else {
asm { hlt; }
}
}
$ dmd test.d -c
$ objdump --disassemble test.o
Disassembly of section .text._D4test4testFZv:
00000000 <_D4test4testFZv>:
0: 55 push %ebp
1: 8b ec mov %esp,%ebp
3: f4 hlt
4: 5d pop %ebp
5: c3 ret
Note that there's no trace of a compare, jmp, nor the nops from
the dead ctfe branch.