4 ms·
You’re confused - nobody is suggesting writing anything but ‘while (1)’ at the source code level. We’re talking about how the compiler implements that, and how
by chrisseaton 4y ago
You’re confused - nobody is suggesting writing anything but ‘while (1)’ at the source code level. We’re talking about how the compiler implements that, and how internally it needs to do ‘1 == 1’ for each iteration if it doesn’t optimise at all.
We’re talking compiler implementation and optimisation - not coding aesthetics.
- rowanG077 4y agoThat makes little sense. There is no equality check in the generated ASM. A compuler doesn't synthesize 1==1 when a `while(1)` is used. Your link even shows this. There is no `cmp` opcode used. A simple unconditional `jmp` is used.
- chrisseaton 4y agoAre you trolling me? > Your link even shows this. Yes the link shows how it doesn't matter today! Because almost all compilers are now 'optimising compilers'! > There is no `cmp` opcode used. A simple unconditional `jmp` is used. Oh my god that's the entire point! That's how compilers work now. They always optimise. They didn't used to do that. The compiler checks the truthiness of the value in the while condition. Modern compilers remove it with either a tautology check, or a shortcut during IR building. They didn't used to do that, so there used to be a cost, which is why people started cargo-culting the for equivalent. Here's a worked example using a niche compiler with a very different architecture that unusually still doesn't canonicalise during building. Compare https://godbolt.org/z/3v3anacM8 https://godbolt.org/z/3v3anacM8 and https://godbolt.org/z/chWjsen4s https://godbolt.org/z/chWjsen4s See the difference? See the 'cmp eax, 1' and then how it goes away under optimisation?