Using precompiled headers, Pros and Cons

Programmers discuss here anything related to FreeOrion programming. Primarily for the developers to discuss.

Moderator: Committer

Post Reply
Message
Author
User avatar
Trilarion
Krill Swarm
Posts: 14
Joined: Mon Aug 04, 2008 2:33 pm
Location: Germany

Using precompiled headers, Pros and Cons

#1 Post by Trilarion »

Hi,

since compilation takes a lot of time and many header files must be traversed several times in the course, we might consider using precompiled headers to speed up compilation. I did some kind of research in this topic and here are the results, so you can see if this would be something for FO.

I found the following sites useful: http://www.cygnus-software.com/papers/p ... aders.html and http://en.wikipedia.org/wiki/Precompiled_header

Why should we use them:
All third party include files, where we can safely assume that they rarely change, can be brought to a precompiled form that can be accessed faster. This precompilation needs to be done only once and than used for all further include calls. Why not also include project specific files? Well if we need to create the precompiled headers every time we change something than there will be no speed advantage.

How does ist work
:
It is possible for MSVC compiler and GCC compiler and is involved by a project settings switch, so the source code will compile with or without them, only eiter faster or not faster. For MSVC you need to create a *.pch file and for GCC a *.gch file, however in the following I concentrate on MSVC. The standard way is to put all third party includes in one(!) header file called stdafx.h. This file must be in any case included in all(? guess so) sourcefiles above all other commands! Then you have to alter the settings of your project to use/create precompiled header files. MSVC will create then stdafx.pch (a 100MB file) and use it later on.

How much faster is it:

I implemented it for the freeorion_ca project with MSVC 2008. The stdafx.h header file that includes all our third party files is attached. Then I took the time for compilation with / without precompiled headers and no other running programms on my system. For that I set Tools/Options/ProjectsandSolutions/VC++ Project Settings/BuildTiming to yes in the MSVC IDE.

Results without precompiled headers (sourcecode as it is)
Clean Solution and Build Project: 13:37 (min:sec)
A change (inserted a space, deleted a space, saved) in AIInterface.cpp afterwards: 0:26
A change in AIInterface.cpp and compile: 0:21
A change in AppInterface.h (very often used): 11:51

Note: 800MB of memory use

Results with precompiled headers (included attached stdafx.h in every source file)
Clean solution and Build of Project (and also build of precompiled header): 10:38 (-22%)
A change in AIInterface.cpp and build: 0:46 (+100%)
A change in AIInterface.cpp and compile: 0:07 (-66%)
A change in AppInterface.h: 6:44 (-43%)

Except the surprising increase in the build after changing a single cpp file (??) it seems to be save ~ 20% when compiling the first time and about 40% later on when the precompiled header is already created.

Summary:

Pros: Makes compilation faster and therefore work on FO more productive and FO will come out earlier. :) The sourcecode will be smaller (less include files per source file) and might be easier to read and understand.

Cons: Requires some additional tweaking of the settings, also on the linux side. If precompiled headers are turned off, the use of a central stdafx.h headerfile will make compilation even slower than before (23 minutes as 13 minutes before). And we loose the information on which particular third party libraries our individual source files depend.

So, all in all it seems like it has advantages and disadvantages. However, so that you can experiment with it, I also attached a patch (or whatever Tortoise-SVN makes if pressing 'create patch') versus the current revision. Only the solution for MSVC 2008 and project freeorion_ca is using precompiled headers there.

Greetings, :)
Jan

P.S.: Please remove the .zip in the attached names if you want to use them.
Attachments
PrecompiledHeaders_Rev2670.patch.zip
(54.6 KiB) Downloaded 139 times
stdafx.h.zip
(4.12 KiB) Downloaded 137 times

User avatar
Geoff the Medio
Programming, Design, Admin
Posts: 13603
Joined: Wed Oct 08, 2003 1:33 am
Location: Munich

Re: Using precompiled headers, Pros and Cons

#2 Post by Geoff the Medio »

A major issue I have with this is that we'd need to put stdafx.h into every source file for MSVC. This would be like adding a bunch of msvc-specific "#pragma once" commands into what is more or less platform-independent C++ code. This might be doable if it were just a matter of setting a switch in the MSVC project files, or if it worked like (I read about) GCC which just looks for the precompiled header files without requiring the source files to be modified.

If you want to maintain a patch that enables the use of precompiled headers on MSVC, feel free to do so (much like kroddn puts out linux statically linked binaries). However, unless tzlaine feels otherwise, I don't think we should make it standard.

Post Reply