4 ms·
In my experience, one of the biggest benefits that isn't too hard to implement is letting the compiler write/read more than it strictly needs to. For example,
by PhilipRoman 3y ago
In my experience, one of the biggest benefits that isn't too hard to implement is letting the compiler write/read more than it strictly needs to.
For example, pad your arrays to a multiple of 8, 16, etc. and where possible, run loops on the entire array including the padding. IIRC both GCC and Clang are smart enough to understand loops whose limit is rounded to the nearest multiple of 2^n via bitwise operators and they won't have to emit ugly loop tails which bloat code size.
This is a bit off topic, but I remember discovering a missed optimization in GCC this way: I wanted to signal to the compiler that it is allowed to read a particular pointer earlier than C semantics would allow and did something along the lines of (void)*p; Well it turns out that dead-end reads are removed pretty early in the optimization pipeline, so GCC removed the read and then refused to touch the pointer. Replacing it with some __builtin, I was able to confirm that was indeed the case.
- Cthulhu_ 3y agoThat sounds like an optimization the compiler could / should do for me, if I enable the option. Same with things like struct ordering and / or padding, although that can affect the functionality in subtle ways, because the language gives you too much power at that low level.
- gpderetta 3y agoIt sounds like that, but turns out that those are extremely hard optimizations to do as they require whole program analysis.
- MereInterest 3y agoAlso, because they involve trade-offs. Ideally, a compiler optimization would result in code that is better in every measurable aspect. For example, dead code elimination results in a smaller binary, fewer branch points, and fewer runtime computations. Array padding, on the other hand, requires a larger memory footprint to accommodate the padding. If an optimization is only beneficial in some cases, compiler authors are rightfully wary of enabling it in all cases.
- LorenPechtel 3y agoNo, because the compiler has no way of knowing if you have some reason to care about the layout. 99.99% of the time you don't--but in general it can't know that last .01% where your layout has outside implications.