4 ms·
When talking about performance, C actually can have overhead though. For example, people rely on how C lays things out in memory, so C compilers have to lay th
by rtpg 10y ago
When talking about performance, C actually can have overhead though.
For example, people rely on how C lays things out in memory, so C compilers have to lay things out in memory in specific ways. Haskell doesn't have this constraint and so can do a bunch of tricks.
And then there are things where C cannot move a variable/optimise out a variable because it's unknown whether some unrelated bit of code my go poke at its memory.
Now one might say "well if you know what you're doing then you can get the compiler to do what you want." Everything's possible, since everything's turing complete. But I would consider "variables need to be somewhere in memory" to be mandatory overhead.
- ktRolster 10y agoThat's talking about automated compiler optimizations; if you want to in C, you can move all those things around yourself by changing the code.
- caf 10y agoVariables don't necessarily need to be somewhere in memory. Compilers absolutely can and will notice if you never take the address of a variable to do this optimisation - it's very common for local variables in C to be materialised nowhere but in a register.
- tacos 10y agoThank you. ANSI C defines a language containing 32 words. And one of them is "register."
- bluetomcat 10y ago> For example, people rely on how C lays things out in memory, so C compilers have to lay things out in memory in specific ways. One can rely on C compilers laying out things in memory in a specific way only within a struct and when compiler-specific alignment/packing attributes are used. All other assumptions about variables being adjacent in memory are wrong and lead to undefined behavior.
- yxhuvud 10y agoWell, one can rely on that array content is laid out sequentially in memory.
- sklogic 10y agoC99 has 'restrict' keyword.
- gpderetta 10y agoIt is certainly possible to do structure layout optimizations (like converting from AOS to SOA) in C and its siblings. Even where the standard requires a specific layout, the compiler can optimize as long as it can prove that the code can't tell the difference (known as the as-if rule), although in the general case this requires whole program optimization. This is not just a theoretical thing; at a certain point in the past, both LLVM and GCC had such an optimization, driven by PGO. I think it got removed because it just didn't trigger often enough and didn't give enough benefits to justify the complexity. Scalar replacement of structs/arrays is of course routinely done by all major compilers and allows to completely optimize away non-escaping local objects.