You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/build-insights/tutorials/build-insights-template-view.md
+12-18Lines changed: 12 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -136,7 +136,7 @@ The **Templates** view lists the template instantiations that contributed signif
136
136
-**Instantiation File Path** shows where in your source code the template instantiation happens. This helps you locate the exact line of code responsible for the expensive instantiation.
137
137
138
138
:::image type="complex" source="./media/templates-view-before-fix.png" alt-text="Screenshot of the Build Insights Templates view showing expensive template instantiations.":::
139
-
The Templates view shows two template instantiations of struct S3 taking most (79.4480%) of the build time. The Translation Unit column shows that both LargeValue.cpp and SmallValue.cpp are affected.
139
+
The Templates view shows two template instantiations of struct S3 taking most (79.448%) 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.
140
140
:::image-end:::
141
141
142
142
- Sort by **Time** to find the templates that take the longest to instantiate.
@@ -145,9 +145,9 @@ The Templates view shows two template instantiations of struct S3 taking most (7
145
145
146
146
## Improve build time by optimizing template instantiations
147
147
148
-
In the example, we can see that two template instantiations of `S3` are taking 83% of the entire build time. The **Translation Unit** column shows that both `SmallValue.cpp` and `LargeValue.cpp` are causing this template instantiation to be included in the build.
148
+
In the example, we can see that two template instantiations of `S3` are taking 79% of the build time. The **Translation Unit** column shows that both `SmallValue.cpp` and `LargeValue.cpp` are causing this template instantiation to be included in the build.
149
149
150
-
Since the **Instantiation File Path** and the **Specialization Name** are the same for both entries, we can infer that there's one expensive template instantiation affecting both of our source files. This explains why the time of each of the two template instantiations are about equal. By including `Templates.h` in both source files, we are causing one template instantiation to add significant time to our build.
150
+
Since the **Instantiation File Path** and the **Specialization Name** are the same for both entries, we infer that there's one expensive template instantiation affecting both of our source files. This explains why the time of each of the two template instantiations are about equal. By including `Templates.h` in both source files, we are causing one template instantiation to add significant time to the build.
151
151
152
152
From the **Specialization Name** column, we can see that the expensive instantiation is `S3<std::make_index_sequence<1000>>`, which corresponds to this code in `Templates.h`:
153
153
@@ -170,15 +170,13 @@ You can also consider removing include directives that bring in unnecessary temp
170
170
171
171
For our purposes, let's assume that we need the template instantiation as-is. We can rely on the third approach: moving the definition that causes the expensive template instantiation to a source file.
172
172
173
-
Since `LargeValue.cpp` is the only source file that calls `LargeValue()`, we can move the definition to `LargeValue.cpp`. This prevents the template instantiation from happening in every translation unit that includes `Templates.h`.
174
-
175
-
To do this, remove the current definition of `LargeValue()` from `Templates.h` and replace it with a declaration:
173
+
Since `LargeValue.cpp` is the only source file that calls `LargeValue()`, we can move the definition to `LargeValue.cpp`. This prevents the template instantiation from happening in every translation unit that includes `Templates.h`. Remove the current definition of `LargeValue()` from `Templates.h` and replace it with a declaration:
176
174
177
175
```cpp
178
176
size_tLargeValue();
179
177
```
180
178
181
-
Then, inside`LargeValue.cpp` add the definition:
179
+
Inside`LargeValue.cpp` add the definition:
182
180
183
181
```cpp
184
182
size_tLargeValue()
@@ -187,16 +185,14 @@ size_t LargeValue()
187
185
}
188
186
```
189
187
190
-
Now, when you rebuild the project and run Build Insights again, you should see that the expensive template instantiation is no longer listed in the **Templates** view. The build time should also decrease significantly.
191
-
192
-
:::image type="complex" source="./media/templates-view-after-fix.png" alt-text="Screenshot of the Build Insights Templates view after optimization showing reduced template instantiation time.":::
188
+
Rebuild the project and run Build Insights again. From the main menu, select **Build** > **Run Build Insights on Selection** > **Rebuild**.
193
189
194
-
The Templates view now shows only one instantiation of S3 instead of two, and the total build time has been significantly reduced.
190
+
The build time has significantly decreased. While the template instantiation of `S3` is still contributing to the build time, we've been able to roughly halve the total time by only including necessary template instantiations. You can see the count of `S3` instantiations is now 1 instead of 2.
195
191
192
+
:::image type="complex" source="./media/templates-view-after-fix.png" alt-text="Screenshot of the Build Insights Templates view after optimization showing reduced template instantiation time.":::
193
+
The Templates view now shows only one instantiation of S3 instead of two, and the total build time is significantly less at 3.152 seconds.
196
194
:::image-end:::
197
195
198
-
The build time has significantly decreased. While the template instantiation of `S3` is still contributing to the build time, we've been able to roughly halve the total time by only including necessary template instantiations. You can see the count of `S3` instantiations is now 1 instead of 2.
199
-
200
196
This technique scales well to larger projects. If multiple files included `Templates.h`, each of those files would have added the instantiation time of `LargeValue()` to the total build time. By moving the definition of `LargeValue()` to a dedicated source file, we minimize our build time.
201
197
202
198
### Optimize the template implementation
@@ -212,12 +208,10 @@ For more advanced template optimization techniques, see [Build Throughput Series
212
208
213
209
## Tips
214
210
215
-
- You can **File** > **Save As** the ETL file to a more permanent location to keep a record of the build time. You can then compare it to future builds to see if your changes are improving build time.
216
-
- If you inadvertently close the Build Insights window, reopen it by finding the `<dateandtime>.etl` file in your temporary folder. The `TEMP` Windows environment variable provides the path of your temporary files folder.
217
-
- To dig into the Build Insights data with Windows Performance Analyzer (WPA), click the **Open in WPA** button in the bottom right of the ETL window.
218
-
- Drag columns to change the order of the columns. For instance, you may prefer moving the **Time** column to be the first column. You can hide columns by right-clicking on the column header and deselecting the columns you don't want to see.
211
+
- You can use **File** > **Save As** to save the ETL file to a more permanent location to keep a record of the build time. You can then compare it to future builds to see if your changes are improving build time.
212
+
- If you inadvertently close the Build Insights window, reopen it by finding the `<dateandtime>.etl` file either where you specified it should be saved, or in your temporary folder. The `TEMP` Windows environment variable provides the path of your temporary files folder.
213
+
- Drag columns to change the order of the columns. For instance, you may prefer moving the **Wall Time Responsibility** column to be the first column. You can add or hide columns by right-clicking on the column header and selecting or deselecting the columns you don't want to add or hide.
219
214
- The **Templates** view provides a filter box to find a specific template instantiation. It does partial matches on the name you provide.
220
-
- If you forget how to interpret what the **Templates** view is trying to show you, hover over the tab to see a tooltip that describes the view.
0 commit comments