|
| 1 | +--- |
| 2 | +title: "Troubleshoot template instantiation impact on build time" |
| 3 | +description: "Tutorial for how to use Build Insights template view to analyze and optimize the impact of template instantiations on build time in your C++ projects." |
| 4 | +ms.date: 7/9/2025 |
| 5 | +helpviewer_keywords: ["C++ Build Insights", "template instantiation analysis", "build time analysis"] |
| 6 | +ms.topic: troubleshooting-general |
| 7 | +--- |
| 8 | +# Troubleshoot template instantiation impact on build time |
| 9 | + |
| 10 | +Use Build Insights **Templates** view to analyze the impact of template instantiations on build time in your C++ projects. This feature is especially useful for projects that make heavy use of templates, such as those using template metaprogramming or large generic libraries. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- Visual Studio 2022 version 17.10 or later. |
| 15 | +- The **C++ Build Insights** component must be installed. It's enabled by default if you install either the Desktop development with C++ workload or the Game development with C++ workload. You can ensure that it is installed by following these steps: |
| 16 | + 1. Open the Visual Studio Installer. |
| 17 | + 1. Modify your Visual Studio installation. |
| 18 | + 1. Under the **Individual components** tab, search for and select **C++ Build Insights**. |
| 19 | + :::image type="complex" 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.":::image-end::: |
| 20 | + 1. Click **Modify** to install the component. |
| 21 | + |
| 22 | +## Overview |
| 23 | + |
| 24 | +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 **Templates** view, which helps diagnose expensive template instantiations during build time. It displays the time it takes to instantiate each template and shows which template instantiations add the most to your build time. |
| 25 | + |
| 26 | +For optimized builds, the time spent on template instantiation contributes significantly to the total build time. In general, C++ template instantiation happens quickly. In exceptional cases, some template instantiations can become complex enough to noticeably slow down your builds. |
| 27 | + |
| 28 | +In this article, learn how to use the Build Insights **Templates** view to find template instantiation bottlenecks in your build. |
| 29 | + |
| 30 | +To try out Build Insights Templates view, follow along to create a project, set the build options, do a build, and analyze the results. |
| 31 | + |
| 32 | +## Create a test project |
| 33 | + |
| 34 | +1. Open Visual Studio and create a new **C++ Console App** project and name it `TemplateAnalysis`. |
| 35 | +1. Create a header file called `Templates.h`. |
| 36 | +1. Replace the contents of the `Templates.h` file with the following code: |
| 37 | + |
| 38 | + ```cpp |
| 39 | + #pragma once |
| 40 | + #include <utility> |
| 41 | + #include <vector> |
| 42 | + |
| 43 | + template<size_t> struct S1 {}; |
| 44 | + template<int n> using type = std::vector<S1<n>>; |
| 45 | + |
| 46 | + template<size_t...> struct S2 {}; |
| 47 | + |
| 48 | + template<typename> struct S3 {}; |
| 49 | + |
| 50 | + template<size_t... n> |
| 51 | + struct S3<std::index_sequence<n...>> |
| 52 | + { |
| 53 | + using type = S2<sizeof(type<n>)...>; |
| 54 | + }; |
| 55 | + |
| 56 | + inline size_t LargeValue() |
| 57 | + { |
| 58 | + return sizeof(S3<std::make_index_sequence<1000>>); |
| 59 | + }; |
| 60 | + |
| 61 | + inline size_t SmallValue() |
| 62 | + { |
| 63 | + return sizeof(S1<5>); |
| 64 | + } |
| 65 | + ``` |
| 66 | + |
| 67 | +1. Create a source file called `LargeValue.cpp` |
| 68 | +1. Replace the contents of the `LargeValue.cpp` file with the following code: |
| 69 | + |
| 70 | + ```cpp |
| 71 | + #include "Templates.h" |
| 72 | + |
| 73 | + size_t GetLargeValue() |
| 74 | + { |
| 75 | + return LargeValue(); |
| 76 | + } |
| 77 | + ``` |
| 78 | + |
| 79 | +1. Replace the contents of the `TemplateAnalysis.cpp` file with the following code: |
| 80 | + |
| 81 | + ```cpp |
| 82 | + #include "Templates.h" |
| 83 | + |
| 84 | + extern size_t GetLargeValue(); |
| 85 | + |
| 86 | + size_t GetSmallValue() |
| 87 | + { |
| 88 | + return SmallValue(); |
| 89 | + } |
| 90 | + |
| 91 | + int main() |
| 92 | + { |
| 93 | + size_t largeValue = GetLargeValue(); |
| 94 | + size_t smallValue = GetSmallValue(); |
| 95 | + return 0; |
| 96 | + } |
| 97 | + ``` |
| 98 | + |
| 99 | +1. ## Enable template instantiation data collection |
| 100 | + |
| 101 | +Template instantiation time collection is off by default to minimize build overhead. To enable it: |
| 102 | + |
| 103 | +1. In Visual Studio, go to **Tools** > **Options**. |
| 104 | +1. In the Options dialog, expand **Build Insights** in the left navigation. |
| 105 | +1. Select **Collect Template Instantiation**. |
| 106 | +1. You can also choose where to save the report by selecting **Store Build Insights reports in this directory:** and specifying a directory. By default, it's saved in the folder pointed to by the Windows `TEMP` environment variable. |
| 107 | + |
| 108 | +:::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 Instantion checkbox is selected."::: |
| 109 | + |
| 110 | +1. Select **OK**. |
| 111 | + |
| 112 | +> [!Note] |
| 113 | +> Enabling Templates view may increase build time due to the extra data collected. Only enable it when you need to analyze template instantiations. |
| 114 | +
|
| 115 | +## Build the project |
| 116 | + |
| 117 | +Run Build Insights by choosing from the main menu **Build** > **Run Build Insights on Selection** > **Rebuild**. You can also right-click a project in the solution explorer and choose **Run Build Insights** > **Rebuild**. Choose **Rebuild** instead of **Build** to measure the build time for the entire project and not for just the few files may be dirty right now. |
| 118 | + |
| 119 | +:::image type="content" source="./media/build-insights-rebuild-project.png" alt-text="Screenshot of the main menu with Run Build Insights on Selection > Rebuild selected."::: |
| 120 | + |
| 121 | +When the build finishes, an Event Trace Log (ETL) file opens.The generated name is based on the collection time. |
| 122 | + |
| 123 | +## Use Templates View to Optimize Build Time |
| 124 | + |
| 125 | +The Templates view lists all template instantiations that contributed significantly to build time. Columns include the template name, instantiation time, and the file where the instantiation occurred. |
| 126 | + |
| 127 | +- **Sort by Instantiation Time** to find the templates that take the longest to instantiate. |
| 128 | +- **Expand a template** to see which code triggered the instantiation and how much time each instance took. |
| 129 | +- **Filter templates** using the search box to focus on specific types or functions. |
| 130 | + |
| 131 | +If you see that a particular template instantiation dominates build time, consider: |
| 132 | +- Refactoring code to reduce the number of instantiations. |
| 133 | +- Moving expensive template code out of headers. |
| 134 | +- Using explicit instantiation declarations where possible. |
| 135 | + |
| 136 | +> **Screenshot suggestion:** Insert a screenshot of the Templates view showing a template instantiation with high build time. **Alt-text:** "Screenshot of the Build Insights Templates view highlighting a template instantiation with high build time." |
| 137 | +
|
| 138 | +## Interpreting Results |
| 139 | + |
| 140 | +- If the Templates view is empty, your build is likely not dominated by template instantiations. |
| 141 | +- The view only shows templates that take a significant amount of time to instantiate, to avoid clutter and reduce data collection overhead. |
| 142 | +- Collecting template instantiation data increases build time. Disable the feature when not actively analyzing templates. |
| 143 | + |
| 144 | +## Improving Build Times |
| 145 | + |
| 146 | +For more strategies on improving build times in template-heavy code, see: |
| 147 | +- [Templates View for Build Insights in Visual Studio](https://devblogs.microsoft.com/cppblog/templates-view-for-build-insights-in-visual-studio-2/) |
| 148 | +- [Build throughput series: More efficient template metaprogramming](https://devblogs.microsoft.com/cppblog/build-throughput-series-more-efficient-template-metaprogramming/) |
| 149 | + |
| 150 | +The first article provides an overview of the Templates view and how to use it. The second article dives deeper into template metaprogramming techniques that can reduce build time, with practical examples. |
| 151 | + |
| 152 | +## Troubleshooting |
| 153 | + |
| 154 | +- **Templates view does not appear:** Ensure you are using Visual Studio 2022 17.10 or later and that the C++ Build Insights component is installed. Also, make sure Templates view is enabled in the options. |
| 155 | +- **No templates listed:** Your project may not have template instantiations that take significant build time, or you may need to build in Release mode with optimizations enabled. |
| 156 | +- **Build is much slower with Templates view enabled:** This is expected due to the extra data collection. Disable Templates view when not needed. |
| 157 | + |
| 158 | +## See also |
| 159 | + |
| 160 | +- [Build Insights tips and tricks](build-insights-tips.md) |
| 161 | +- [Troubleshoot function inlining on build time](build-insights-function-view.md) |
| 162 | +- [Build Insights now available in Visual Studio 2022](build-insights-now-available-in-visual-studio-2022.md) |
| 163 | +- [Functions View for Build Insights in Visual Studio 2022 17.8](functions-view-for-build-insights-in-visual-studio-2022-17-8.md) |
| 164 | +- [Build throughput series: More efficient template metaprogramming](https://devblogs.microsoft.com/cppblog/build-throughput-series-more-efficient-template-metaprogramming/) |
| 165 | + |
| 166 | +--- |
| 167 | + |
| 168 | +**Summary of additional important points from the blog post:** |
| 169 | +- The Templates view helps identify which template instantiations are most expensive, making it easier to target optimizations. |
| 170 | +- The feature is especially useful for codebases that use heavy metaprogramming or generic libraries. |
| 171 | +- The blog post provides more advanced examples, such as using SFINAE and type traits, which can be referenced for deeper dives but are not included in this basic walkthrough. |
| 172 | +- The performance impact of enabling Templates view is non-trivial; only enable it when needed. |
| 173 | +- The Templates view is designed to be familiar to users of the Functions view, with similar UI and workflow. |
0 commit comments