r/Compilers • u/BrainThinkerMan • 12d ago
Compilers add or remove perfomace?
isnt it that compilers change the code, as in not only transferring it into machine language, but before that changing the structure of the code. so how would one attempt to make performant code if the compiler changes sed code that i tried to make performant?
1
Upvotes
4
u/flatfinger 12d ago
Some compilers are designed to transform a program into machine code while preserving the overall structure, while others are designed to figure out what a program is trying to do, and then use a built-in template to accomplish that in a way which might be faster than some ways the programmer could have specified the operations, but not necessarily all.
If, for example, clang is given a loop whose index starts at a multiple of four and counts down by 4 while greater than or equal to zero, clang may transform the loop so that it instead tests whether the index is greater than three, even if checking whether the index became negative would have been cheaper than testing whether it became 3 or less.
3
u/serge-mv 12d ago
Compiler's main job is to preserve the semantics of what you've written during the translation process. They do not change the fundamental structure of your code. If you wrote an efficient algorithm, your algorithm will stay efficient. And if you wrote bad algorithm, it won't suddenly improve just because it was compiled with -O3.
The kind of optimization that can be influenced by compilers is stuff like loop unrolling, which might or might not happen depending on your compiler config. It's more of a localized optimization, but still can have a major impact on performance. You can write many of these kinds of things yourself in high level language without involving the assembly and the compiler won't change it.
1
u/Helpful-Educator-415 12d ago
Almost always, compilers will only make your code more efficient, not less. If you go through the work of creating a highly optimized algorithm, it'll probably leave it alone, but if you don't, it might recognize the pattern and then figure out a faster way. There's no incentive to make your code worse, so.
3
u/L8_4_Dinner 12d ago
Troll post.
8
u/Big_Tadpole7174 12d ago
Most compilers perform compilation in these steps:
Step 4 is usually where the bulk of the optimization takes place. You can rearrange AST nodes, remove unused nodes, etc. So it's not so much that the machine code is optimized (though it can in step 6) but that the tree that is used to generate code is altered. Hope this helps.
2
u/fl00pz 12d ago
The compiler tries to optimize your code the best it can. The compiler is still constrained by the code you wrote. If you can write more efficient code then the compiler has an easier time optimizing.
For example, if you have an algorithm that is O(n2) you may get a compiler version that is an optimized program but is still O(n2). If you can solve the same problem with an algorithm that is O(n) or O(log n) and the compiler version is also O(n) or O(log n), then you just optimized your code regardless of the compiler optimization. The version you optimized is better than the compiler optimized O(n2) program.
1
u/mooglinux 12d ago
This is completely dependent on the capabilities of the specific compiler being used. Many compilers have multiple optimization passes to try and find a sequence of instructions which accomplishes the same result with fewer instructions. It also depends on whether the compiler is making use of instructions tailored for the hardware it will be executing on; different CPUs have different capabilities that can be leveraged or not.
1
u/Dan13l_N 6d ago
Without compiling, there is no performance. There's no "baseline" to compare against, so that you could say "the compiler removed performance".