{"id":4093,"date":"2010-01-26T13:08:00","date_gmt":"2010-01-26T13:08:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2010\/01\/26\/precompiled-header-files-in-visual-studio-2010\/"},"modified":"2019-02-18T18:45:40","modified_gmt":"2019-02-18T18:45:40","slug":"precompiled-header-files-in-visual-studio-2010","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/precompiled-header-files-in-visual-studio-2010\/","title":{"rendered":"Precompiled Header Files in Visual Studio 2010"},"content":{"rendered":"<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Hello, this is Andy Rich from the Visual C++ front-end team. Today, I\u2019ll be discussing the use of precompiled header files (aka PCH files) in our new intellisense architecture.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Back in May, Boris <\/font><a href=\"http:\/\/blogs.msdn.com\/vcblog\/archive\/2009\/05\/27\/rebuilding-intellisense.aspx\"><font face=\"Calibri\" color=\"#0000ff\" size=\"3\">briefly mentioned<\/font><\/a><font face=\"Calibri\" size=\"3\"> an intellisense optimization based on precompiled header technology. This post will elaborate on that comment by providing a glimpse into how Intellisense PCH files (or iPCH files) work. We\u2019ve all become accustomed to precompiled headers improving build throughput, and now in Visual Studio 2010, we use the same technology to improve intellisense performance in the IDE. <\/font><\/p>\n<h2><span><font size=\"5\"><font color=\"#365f91\"><font face=\"Cambria\">The Problem with \u201cPre-parses\u201d<\/font><\/font><\/font><\/span><\/h2>\n<h2><span><font size=\"5\"><font color=\"#365f91\"><font face=\"Cambria\"><\/font><\/font><\/font><\/span>&nbsp;<\/h2>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">The VC++ 2010 intellisense engine mimics the command-line compiler by using a translation unit (TU) model to service intellisense requests. A typical translation unit consists of a single source file and the several header files included by that source file (and the headers those headers include, etc.).&nbsp; The intellisense engine exists to provide users with answers to questions, such as what a particular type is, what the exact signature of a function is (and its overloads), or what variables are available in the current scope beginning with a particular substring.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">In order for the intellisense compiler to provide this information, the intellisense engine must initially parse the TU like the command-line compiler, recursively parsing all #include files listed at the top of the source file before<i> <\/i>parsing the rest of the source file.&nbsp; Thanks to C++ scoping rules, we know that we can skip all method bodies except the one you might currently be in, but, other than this optimization, the rest of the translation unit must be parsed to give an accurate answer.&nbsp; We refer to this as the \u201cpre-parse.\u201d<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Pre-parses are not always required, as users spend much of their time writing code inside of a local scope.&nbsp; Through careful tracking of user edits, we can say whether or not the user has changed information which requires a new pre-parse.&nbsp; When this happens, we throw away our old pre-parse and start again.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">So, even though you aren\u2019t editing header files, they must be continually parsed as part of the pre-parse.&nbsp; As a translation unit grows in size, these parses require progressively more CPU and memory resources, and will lead to a drop in intellisense performance.&nbsp; Parsing is slow, and parsing a lot of information (as in a complex translation unit) can be very slow; 3 seconds is not uncommon, and that is simply too long for an intellisense response.<\/font><\/p>\n<h2><span><font size=\"5\"><font color=\"#365f91\"><font face=\"Cambria\">The PCH Model<\/font><\/font><\/font><\/span><\/h2>\n<h2><span><font size=\"5\"><font color=\"#365f91\"><font face=\"Cambria\"><\/font><\/font><\/font><\/span>&nbsp;<\/h2>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">Luckily, there is an optimization developed for command-line compilers that can also be applied to the intellisense compiler: pre-compiled headers (PCHs).&nbsp; The PCH model presupposes that your translation units mostly share a lot of the same common includes.&nbsp; You inform the compiler of this set of headers, and it builds a pre-compiled header file.&nbsp; On subsequent compilations, instead of re-compiling this set of headers, the compiler loads the PCH file and then proceeds to compile the unique portion of your translation unit.<\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/Precompiled%20header%20files.png\"><b><\/b><\/font><\/font><\/p>\n<p class=\"MsoNormal\">\n<table class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\">\n<tbody>\n<tr>\n<td class=\"\">\n<div>\n<p class=\"MsoNormal\"><span><font face=\"Calibri\">Common Headers<\/font><\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\">\n<tbody>\n<tr>\n<td class=\"\">\n<div>\n<p class=\"MsoNormal\"><span><font face=\"Calibri\">a.cpp<\/font><\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\">\n<tbody>\n<tr>\n<td class=\"\">\n<div>\n<p class=\"MsoNormal\"><span><font face=\"Calibri\">PCH<\/font><\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\">\n<tbody>\n<tr>\n<td class=\"\">\n<div>\n<p class=\"MsoNormal\"><span><font face=\"Calibri\">PCH<\/font><\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\">\n<tbody>\n<tr>\n<td class=\"\">\n<div>\n<p class=\"MsoNormal\"><span><font face=\"Calibri\">Unique includes<\/font><\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><font face=\"Calibri\"><\/font><\/p>\n<table class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\">\n<tbody>\n<tr>\n<td class=\"\">\n<div>\n<p class=\"MsoNormal\"><span><font face=\"Calibri\">b.cpp<\/font><\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\">\n<tbody>\n<tr>\n<td class=\"\">\n<div>\n<p class=\"MsoNormal\"><span><font face=\"Calibri\">Unique includes<\/font><\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\">\n<tbody>\n<tr>\n<td class=\"\">\n<div>\n<p class=\"MsoNormal\"><span><font face=\"Calibri\">c.cpp<\/font><\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\">\n<tbody>\n<tr>\n<td class=\"\">\n<div>\n<p class=\"MsoNormal\"><span><font face=\"Calibri\">Unique includes<\/font><\/span><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><font face=\"Calibri\" size=\"3\">There are a few caveats to this model.&nbsp; First, the \u201ccommon\u201d portion of your headers must be the first files compiled in each translation unit, and they must be in the same order in all translation units.&nbsp; Most developers refactor their headers to have a common header file for this purpose; this is what stdafx.h in the Visual C++ project templates is intended for.<\/font><\/p>\n<h2><span><font size=\"5\"><font color=\"#365f91\"><font face=\"Cambria\">Leveraging PCH for the Intellisense Compiler<\/font><\/font><\/font><\/span><\/h2>\n<h2><span><font size=\"5\"><font color=\"#365f91\"><font face=\"Cambria\"><\/font><\/font><\/font><\/span>&nbsp;<\/h2>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">In general, if you have PCH set up for use with the build compiler, the intellisense compiler is able to pick up those PCH options and generate a PCH that can be used.&nbsp; Because the intellisense compiler uses a different PCH format from the build compiler, separate PCH files are created for the use of the intellisense compiler.&nbsp; These files are typically stored under your main solution directory, in a subdirectory labeled \u2018ipch\u2019.&nbsp; (Future releases may have the command-line and intellisense compilers share these PCHs, but for now, they are separate.)<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">The intellisense compiler can load these iPCH files to save not only parse time, but memory as well: all translation units that share a common PCH will share the memory for the loaded PCH, further reducing the working set.&nbsp; So, a properly set-up PCH scheme for your solution can make your intellisense requests execute much more rapidly and reduce memory consumption.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Here are some important things to keep in mind when configuring your project for iPCH:<\/font><\/p>\n<p class=\"MsoListParagraph\"><span><span><font size=\"3\">\u00b7<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><font face=\"Calibri\" size=\"3\">iPCH and build compiler PCH share the same configuration settings (configurable on a per-project or per-file basis through&nbsp; \u201cConfiguration Properties-&gt;C\/C++-&gt;Precompiled Headers\u201d).<\/font><\/p>\n<p class=\"MsoListParagraph\"><span><span><font size=\"3\">\u00b7<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><font face=\"Calibri\" size=\"3\">The iPCH should represent the largest set of common headers possible, except for commonly edited headers.<\/font><\/p>\n<p class=\"MsoListParagraph\"><span><span><font size=\"3\">\u00b7<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><font face=\"Calibri\" size=\"3\">All translation units should include the common headers in the same order.&nbsp; This may be best configured through a single header file that includes all the other headers, with respective .cpp files including only this \u201cmaster\u201dheader file.<\/font><\/p>\n<p class=\"MsoListParagraph\"><span><span><font size=\"3\">\u00b7<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><font face=\"Calibri\" size=\"3\">You can have different iPCH files for different translation units \u2013 but only one iPCH file can be used for any given translation unit.<\/font><\/p>\n<p class=\"MsoListParagraph\"><span><span><font size=\"3\">\u00b7<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><font face=\"Calibri\" size=\"3\">The intellisense compiler will not create a iPCH file if there are errors in it \u2013 open up the \u2018error list\u2019 window and look for any intellisense errors; eliminate these errors in order to get PCH working.<\/font><\/p>\n<p class=\"MsoListParagraph\"><span><span><font size=\"3\">\u00b7<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><font face=\"Calibri\" size=\"3\">If you feel that an iPCH has somehow become corrupted, you can shut down the IDE and delete the iPCH directory. <\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, this is Andy Rich from the Visual C++ front-end team. Today, I\u2019ll be discussing the use of precompiled header files (aka PCH files) in our new intellisense architecture. Back in May, Boris briefly mentioned an intellisense optimization based on precompiled header technology. This post will elaborate on that comment by providing a glimpse into [&hellip;]<\/p>\n","protected":false},"author":289,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[6,14,15],"class_list":["post-4093","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","tag-ide","tag-intellisense","tag-pch"],"acf":[],"blog_post_summary":"<p>Hello, this is Andy Rich from the Visual C++ front-end team. Today, I\u2019ll be discussing the use of precompiled header files (aka PCH files) in our new intellisense architecture. Back in May, Boris briefly mentioned an intellisense optimization based on precompiled header technology. This post will elaborate on that comment by providing a glimpse into [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/4093","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/289"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=4093"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/4093\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=4093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=4093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=4093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}