- What does it mean to develop cycles in the context of the discussion of optimization (if it is possible for workers and peasants)
- In some options, there is no embedding of functions ... and how, without embedding, then? (under embedding I understood the presence of the function code in the executable)
- Can I compile individual modules with different optimizations and then click them into an executable file?
2 answers
Cycle sweep is the transformation of a cyclic code into a non-cyclic code. For example, the following code:
for(i=0; i<5; i++) func(i);unfolds in
func(0); func(1); func(2); func(3); func(4);Cycle deployment works when the number of loop iterations is known at compile time. Increases the size of the machine code, but can increase performance. Performance may increase due to the absence of transitions (if it is completely simplified - it is easier for the processor to execute linear code, plus the linear code can be further optimized by the compiler, grouping / rearranging commands in a special way).
Embedding ("inlining"), if absolutely simplified, then it is the substitution of the function body into the place (instead of) of its call. If a function is called from several places, it will increase the size of the machine code, but it can improve performance, for example, if the function is called inside a loop. Depending on the level of optimization, the compiler may well embed a function not marked with the
inline. Or do not embed a function with this keyword.You can. Exactly as you wrote in the question - compile each module separately, then link the resulting object files into the executable. I do not see much practical sense in this.
Before choosing an optimization parameter, you must first evaluate the cycles and inline functions of the code?
Optimizations can improve everything, and can break everything, especially if your code has parts with undefined behavior. Plus, at a high level of optimization, machine instructions specific to your processor may be used, as a result, the executable file may not work on any computers. Therefore - try to compile at different levels of optimization, and test on different computers. Especially to sharpen your code for the features of a compiler, or for some optimization to occur, IMHO, is not worth it. It is better to do high-level optimizations (optimization algorithms), and the low-level compiler will do it for you.
- Thanks for the answer. Could you please clarify something else 1) Deploying a loop will increase performance by not having to work with an array counter (increment and condition check)? 2) Regarding the third point. Purely theoretically, can I?) Is someone practicing from adequate people? 3) Can the compiler embed non-inline functions ??? 4) Before choosing an optimization parameter, you must first evaluate the cycles and inline functions of the code? - Semerkin
- @Semerkin, added the answer. - insolor
What does scan cycles in the context of the discussion of optimization (if possible for workers and peasants).
Suppose there are three vectors and a cycle:
int a[3], b[3] , c[3]; // ... for (int i =0; i<3; ++i) { c[i] = a[i] +b[i]; } Scanning loops means that the compiler can replace this loop with an assembler code similar to that generated from the following:
c[0] = a[0] + b[0]; c[1] = a[1] + b[1]; c[2] = a[2] + b[2]; In some options, there is no embedding of functions ... and how, without embedding, then? (under embedding I understood the presence of the function code in the executable)
Not correct, embedding functions is what the inline keyword gives a hint about. Those. when:
inline int add (int a, int b) { return a+b;} int main (void) { int aMain,bMain; // ... int c = add (aMain, bMain); } The body can be substituted in the place of its call. Those. The code in fact can be the same as the following:
int main (void) { int aMain,bMain; // ... int c = aMain + bMain; } An important point: the decision about embedding is always made by the compiler, inline only “advises” it and adjusts its heuristics.
Can I compile individual modules with different optimizations and then click them into an executable file?
Generally speaking - yes, at least for flags from -O[123s] this should not be a problem. But IMHO you can pick up some combination of flags that will cause conflicts or errors (for example, various -fabi-version=* ).
- Thank! Do you often use optimization in practice? 6 years ago, a similar question was already asked on the stack and it was noted that the use of optimizations could lead to the collapse of the program, moreover during execution some time later ... - Semerkin
- @Semerkin, Optimizations for combat code are almost always required. Low levels of optimization, relatively speaking (-O1 and -O2) can be used almost always. Changing their behavior is indicative of an error in the program and most likely caused by UB, so if you stick to the standard, they are actually safe.
-Ofaston the contrary, adds dangerous optimizations that change behavior. When it is applied, it is necessary to read about all the flags it adds and preferably write tests.-O3is almost always as safe as -O [12] but may, for example, aggravate the race condition. - Fat-Zer - Thank! Is it possible to exacerbate the state of the race (honestly for the first time I see that it is exacerbated)? The state of the race is either there or not. - Semerkin
- @Semerkin relatively speaking .... on -O2 an error can occur once out of 1000, and on -O3 one out of 10. It is clear that this is an ephemeral and probabilistic thing and the opposite may well happen ... - Fat-Zer
- Wow! so then all these optimizations deprive the programmer of any understanding of what his code does! The race for resources is serious ... it turns out that every .acqua is better to drive with all sorts of debuggers - I think it will save you from problems - Semerkin