Lately I'm diving into optimizing my C++ code and as such started to play around with the compiler explorer. Since i am mainly developing on windows with Visual Studio i used the msvc compiler.
At some point msvc got out of hand. after some fiddling around i could narrow it down to the iostream header, that's supposed to be preferred for I/O (SL.io.3).
#include <iostream>
int main() {
std::cout << "Hello World!\n";
return 0;
}
While gcc or clang's total output (main + a static initializer that calls some ios_base
init functions) totals about 20 lines of assembly (after the Godbolt compiler explorer filters out directives and comments).
MSVC explodes it into 4000. Most of those lines are separate functions; MSVC's definition of main
itself is 7 instructions vs. 8 for gcc/clang. (gcc/clang using GNU/Linux libstdc++
pass an extra length arg to the cout operator overload function, not just 2 pointers like MSVC does when using its own C++ library.)
If i use something like puts
instead, MSVC's total output is reasonably compact and comparable to gcc/clang, like here.
Can someone kindly explain to me what is happening here, what im doing wrong or point me in the right direction?
Why are MSVC asm listings so bloated for simple functions using C++ libraries?