none
how to detect and avoid template code explosion

    Question

  • Hi,

    we are using mainly VS 2012 c++ (but planning to switch to VS2015 soon). In our project we have some issues with compilation time and temporary objects size and I'm trying to figure out how to improve the situation. In short, our release build takes more than 10 minutes and temporary objs are around 10GB in size. To try to troubleshoot the issue I tried to take some small c++ file from our project and inspect obj that gets created by VS2012 c++ compiler. The sample c++ in only like 50 lines of simple functions, but because of includes overall compilation time is a few seconds and resulting obj file is like 2.5MB... and here's the most important part: we do use precompiled headers and all headers that are included by the file are also included in precompiled header!

    So, how should I figure out what's the reason for such code explosion? I tried to enable "Assembly With Machine Code (/FAc)" output to see what's generated and it seems that biggest culprit is all the std::xxx functions are duplicated in each object file even though these were supposed to be inside precompiled header. Frankly, I think it's a bug in the compiler.

    Can you suggest some tools that help to figure out where why object files end up so big and where compilation time is spent?

    Regarding buggy behavior with duplicated template code. In my case I was simply using std::string, and even though <string> is included in precompiled header whenever I use anything from std::string code gets duplicated in each obj file. How can I avoid that buggy behavior, what can i do to leave all std::string code inside recompiled header only? From quick observation it looks like I have more than a thousand files and each one of them have most of std::string functions, which means that it's compiled thousand times, duplicated on disk thousand times and then thousand copies have to be merged at link time.

    I tried explicit template instantiation in stdafx.h:

    template std::basic_string<char, std::char_traits<char>, std::allocator<char> >;

    I tried to add some functions in precompiled header that would trigger all commonly duplicated function from std::string to be pulled in into the precompiled header, but no matter what I try std::string function are still duplicated throughout all object files, and that why I say it's a bug in the compiler.

    Thanks


    • Edited by ppsmtl Monday, January 30, 2017 1:24 AM
    Monday, January 30, 2017 1:18 AM

All replies

  • Hi friend,

    The situation you described above all happened in VS2012 right?

    Please try the suggestions below:

    Open Run from start -> type %temp% -> delete everything from there

    Open Run again -> type prefetch -> delete everything from there

    Then restart VS to see the performance.

    If above doesn’t work, please try to run this: devenv /resetsettings

    Or go to Tools -> Import and Export settings -> reset all settings

    Best regards,

    Fletch


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 31, 2017 4:04 AM
  • Excuse me... how is all that related to the problems that I described?

    slow compilation time and huge object size is mainly due to our spaghetti-style includes and frivolous use of templates where they shouldn't not have been used in the first place. This problem is observed on many PC in our organizations (hundreds of dev machines, build farms etc). I'm asking if there are any tools to troubleshoot and figure out what to tweak in our code to get some improvements without doing brain surgery on our codebase.

    Then, I described that I used "Assembly With Machine Code (/FAc)" output and I see what looks like buggy behavior: compiler duplicates code all over the place which it shouldn't have done in the first place.

    Thanks


    • Edited by ppsmtl Wednesday, February 01, 2017 6:04 AM
    Wednesday, February 01, 2017 6:03 AM
  • Hi friend,

    >> Can you suggest some tools that help to figure out where why object files end up so big and where compilation time is spent?

    Tools -> Options -> Projects and Solutions -> VC++ Project Settings -> Build Timing

    should work, snapshot like below:

    Best regards,

    Fletch


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 02, 2017 2:17 AM