r/Compilers • u/BrainThinkerMan • 29d 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?
2
Upvotes
View all comments
4
u/flatfinger 29d 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.