diff --git a/docs/build-insights/tutorials/build-insights-template-view.md b/docs/build-insights/tutorials/build-insights-template-view.md index d4cd01924b6..47382cff0bd 100644 --- a/docs/build-insights/tutorials/build-insights-template-view.md +++ b/docs/build-insights/tutorials/build-insights-template-view.md @@ -21,7 +21,7 @@ The **Templates** view works like the Build Insights [Functions view](build-insi 1. Select **Modify** to change your Visual Studio installation. 1. On the **Individual components** tab, search for and select **C++ Build Insights**, then select **Close** to finish installing the component. :::image type="content" source="./media/installer-build-insights.png" alt-text="Screenshot of the Visual Studio Installer. The search box contains C++ Build Insights. The item C++ Build Insights is visible and selected."::: - + ## Overview Build Insights, integrated into Visual Studio, helps you optimize your build times--especially for large projects like AAA games. Build Insights provides analytics such as the **Templates** view, which shows the time it takes to instantiate each template and which template instantiations add the most to your build time. @@ -36,55 +36,56 @@ In this article, you create a project that shows how template instantiation affe 1. Create a header file named `Templates.h`, then replace its contents with the following code: ```cpp - #pragma once - #include - #include - - template struct S1 {}; - template using type = std::vector>; - - template struct S2 {}; - - template struct S3 {}; - - template + #pragma once + + #include + #include + + template struct S1 {}; + template using type = std::vector>; + + template struct S2 {}; + + template struct S3 {}; + + template struct S3> - { - using type = S2)...>; - }; - + { + using type = S2)...>; + }; + inline size_t LargeValue() - { - return sizeof(S3>); - }; - + { + return sizeof(S3>); + } + inline size_t SmallValue() - { - return sizeof(S1<5>); - } + { + return sizeof(S1<5>); + } ``` 1. Create a source file named `LargeValue.cpp`, then replace its contents with the following code: ```cpp - #include "Templates.h" + #include "Templates.h" size_t GetLargeValue() - { - return LargeValue(); - } + { + return LargeValue(); + } ``` 1. Replace the contents of the `TemplateAnalysis.cpp` file with the following code: ```cpp - #include "Templates.h" + #include "Templates.h" extern size_t GetLargeValue(); size_t GetSmallValue() - { - return SmallValue(); + { + return SmallValue(); } int main() @@ -107,7 +108,7 @@ Template instantiation time collection is off by default to minimize build overh :::image type="content" source="./media/tools-options-build-insights.png" alt-text="Screenshot of the project property pages dialog. The settings are open to Build Insights > Trace Collection. The Collect Template Instantiation checkbox is selected."::: -> [!Note] +> [!NOTE] > Collecting template instantiation times increases build time due to the extra data collected. Only enable it when you want to analyze template instantiation bottlenecks. ## Run Build Insights to get template instantiation data @@ -128,7 +129,7 @@ The **Templates** view lists the template instantiations that contributed signif - **Instantiation File Name** shows where the template is defined. :::image type="complex" source="./media/templates-view-before-fix.png" alt-text="Screenshot of the Build Insights Templates view showing expensive template instantiations." lightbox="./media/templates-view-before-fix.png"::: -The Templates view shows two template instantiations of struct S3 taking most (79.448 percent) of the build time. The Translation Unit column shows that both LargeValue.cpp and SmallValue.cpp are affected. The build time is 4.066 seconds. +The Templates view shows two template instantiations of struct S3 taking most (79.448 percent) of the build time. The Translation Unit column shows that both LargeValue.cpp and TemplateAnalysis.cpp are affected. The build time is 4.966 seconds. :::image-end::: - Sort by **Time** to find the templates that take the longest to instantiate. @@ -146,7 +147,7 @@ To interpret the **Templates** view results: ## Improve build time by optimizing template instantiations -In the example, two template instantiations of `S3` take 79 percent of the build time. The **Translation Unit** column shows that both `SmallValue.cpp` and `LargeValue.cpp` cause this template instantiation. +In the example, two template instantiations of `S3` take 79 percent of the build time. The **Translation Unit** column shows that both `LargeValue.cpp` and `TemplateAnalysis.cpp` cause this template instantiation. The **Instantiation File Name** and the **Specialization Name** are the same for both entries, which means one expensive template instantiation that affects both source files. That's why the time for each of the two template instantiations is roughly equal. Including `Templates.h` in both source files causes one template instantiation to add significant time to the build. @@ -156,7 +157,7 @@ From the **Specialization Name** column, the expensive instantiation is `S3>); -}; +} ``` There are three main ways to decrease the cost of template instantiations. @@ -231,4 +232,4 @@ For more advanced template optimization techniques, see [Build Throughput Series - [Troubleshoot header file impact on build time](build-insights-included-files-view.md) - [Troubleshoot function inlining on build time](build-insights-function-view.md) - [Build Insights now available in Visual Studio 2022](https://devblogs.microsoft.com/cppblog/build-insights-now-available-in-visual-studio-2022) -- [Build throughput series: More efficient template metaprogramming](https://devblogs.microsoft.com/cppblog/build-throughput-series-more-efficient-template-metaprogramming) \ No newline at end of file +- [Build throughput series: More efficient template metaprogramming](https://devblogs.microsoft.com/cppblog/build-throughput-series-more-efficient-template-metaprogramming)