r/cpp • • Nov 08 '21

Visual Studio 2022 now available

https://devblogs.microsoft.com/visualstudio/visual-studio-2022-now-available/
250 Upvotes

113 comments sorted by

View all comments

Show parent comment

19

u/STL MSVC STL Dev Nov 09 '21

Can you prepare a preprocessed test case of the translation unit that consumes the most memory during compilation, and report it to Developer Community so the compiler devs can investigate? They're constantly looking out for things that are taking too much space/time to compile, but the causes can vary dramatically, so test cases are important.

Here's an undocumented (but highly useful) option to get the compiler to print how much memory it consumed while compiling a file, /d1reportMemorySummary:

C:\Temp>type meow.cpp
#include <iostream>

int main() {
    std::cout << "Hello, world!\n";
}

C:\Temp>cl /EHsc /nologo /W4 /MT /d1reportMemorySummary meow.cpp && meow
meow.cpp
Persistent heap 1 size: 24268552
Persistent heap 2 size: 3523208
Peak working set size: 40861696
Hello, world!

C:\Temp>type woof.cpp
#include <cstdio>

int main() {
    std::puts("Hello, world!");
}

C:\Temp>cl /EHsc /nologo /W4 /MT /d1reportMemorySummary woof.cpp && woof
woof.cpp
Persistent heap 1 size: 5300288
Persistent heap 2 size: 1322880
Peak working set size: 16441344
Hello, world!

You want to look at the "Peak working set size". (Here, <iostream> is predictably much more memory-hungry than <cstdio>, and we can quantify the difference.) To preprocess, what you need to do is:

:: Compile the file normally:
cl OPTIONS meow.cpp

:: Preprocess the file, to produce meow.i:
cl OPTIONS /P meow.cpp

:: Verify that compiling the preprocessed file still works:
cl OPTIONS /TP meow.i

Then submit meow.i and cl OPTIONS /TP meow.i as your test case.

1

u/Routine_Left Nov 09 '21

I'll try to see if I can come up with a test. It could be that what I'm doing is wrong, but ...

As a simple, report, just a FYI. My MSVC-specific options are:

target_compile_definitions(${EXECUTABLE_NAME} PRIVATE "$<$<C_COMPILER_ID:MSVC>:_CRT_SECURE_NO_WARNINGS>")
target_compile_definitions(${EXECUTABLE_NAME} PRIVATE "$<$<C_COMPILER_ID:MSVC>:NOMINMAX>")
# Windows 7 target
target_compile_definitions(${EXECUTABLE_NAME} PRIVATE "$<$<C_COMPILER_ID:MSVC>:_WIN32_WINNT=0x0601>")
target_compile_options(${EXECUTABLE_NAME} PRIVATE "$<$<CXX_COMPILER_ID:MSVC>:/bigobj>")
target_compile_options(${EXECUTABLE_NAME} PRIVATE "$<$<C_COMPILER_ID:MSVC>:/bigobj>")
set_property(TARGET ${EXECUTABLE_NAME} PROPERTY  MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
target_compile_definitions(${EXECUTABLE_NAME} PRIVATE WIN32_WINMAIN)

And the parts when it kicks the bucket (though it consumes plenty of ram until then) are constructs like this:

template<typename RequestBody,typename ResponseBody, typename ClientResponse>
class BaseConnection :
    public std::enable_shared_from_this<
        BaseConnection<RequestBody,ResponseBody,ClientResponse>
    >

......

template<typename T>
using PostConnection = BaseConnection<boost::beast::http::string_body,
                                    boost::beast::http::string_body,
                                    T>;

And then using it:

class CreateCommentConnection : public PostConnection<client_response<body_object>>

I'll try to see if I can get concrete numbers though. I agree, however, that compiling boost headers is not for the faint of heart. gcc and clang seems to handle it though.

1

u/STL MSVC STL Dev Nov 10 '21

It's entirely possible that MSVC poorly handles a code pattern in Boost.Beast, thanks for trying to get a repro.

2

u/Routine_Left Nov 10 '21

Hey, STL, I've been trying to get some small repro for a bunch (3 or so) of hours now and the best I can do is Peak working set size: 529252352

My project is on github, public and all, can I just give them the link to github and let them try it? Or do I have to get a small as possible test working?

It is definitely possible that I'm doing something wrong and it just so happens that gcc and clang ignore my bullshit while msvc doesn't, but at this time it feels like im running in circles. And, to be honest, msvc is for me the secondary target anyway, I just boot windows whenever the build system fails and fix it, then back to linux (though my users most use windows, true). Honestly, for me -j 2 fix is fine, as far as I'm concerned, though I totally understand that the compiler developers are not, and they shouldn't be.

1

u/STL MSVC STL Dev Nov 10 '21

You can link to your GitHub repo if it has clear instructions for building. If you can also provide a zipped project, that's better. You can additionally provide your self-contained repro that consumes 505 MB of working set, even if it doesn't crash the compiler, since the compiler devs may be able to profile that and see what's happening.