Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 37 additions & 36 deletions docs/build-insights/tutorials/build-insights-template-view.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 <utility>
#include <vector>

template<size_t> struct S1 {};
template<int n> using type = std::vector<S1<n>>;

template<size_t...> struct S2 {};

template<typename> struct S3 {};

template<size_t... n>
#pragma once

#include <utility>
#include <vector>

template<size_t> struct S1 {};
template<int n> using type = std::vector<S1<n>>;

template<size_t...> struct S2 {};

template<typename> struct S3 {};

template<size_t... n>
struct S3<std::index_sequence<n...>>
{
using type = S2<sizeof(type<n>)...>;
};
{
using type = S2<sizeof(type<n>)...>;
};

inline size_t LargeValue()
{
return sizeof(S3<std::make_index_sequence<1000>>);
};
{
return sizeof(S3<std::make_index_sequence<1000>>);
}

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()
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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.

Expand All @@ -156,7 +157,7 @@ From the **Specialization Name** column, the expensive instantiation is `S3<std:
inline size_t LargeValue()
{
return sizeof(S3<std::make_index_sequence<1000>>);
};
}
```

There are three main ways to decrease the cost of template instantiations.
Expand Down Expand Up @@ -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)
- [Build throughput series: More efficient template metaprogramming](https://devblogs.microsoft.com/cppblog/build-throughput-series-more-efficient-template-metaprogramming)