Debian Codegraph @

docs/content/en/troubleshooting/performance.md

raw
1 ---
2 title: Performance
3 description: Tools and suggestions for evaluating and improving performance.
4 categories: []
5 keywords: []
6 aliases: [/troubleshooting/build-performance/]
7 ---
8
9 ## Virus scanning
10
11 Virus scanners are an essential component of system protection, but the performance impact can be severe for applications like Hugo that frequently read and write to disk. For example, with Microsoft Defender Antivirus, build times for some sites may increase by 400% or more.
12
13 Before building a site, your virus scanner has already evaluated the files in your project directory. Scanning them again while building the site is superfluous. To improve performance, add Hugo's executable to your virus scanner's process exclusion list.
14
15 For example, with Microsoft Defender Antivirus:
16
17 **Start** > **Settings** > **Privacy & security** > **Windows Security** > **Open Windows Security** > **Virus & threat protection** > **Manage settings** > **Add or remove exclusions** > **Add an exclusion** > **Process**
18
19 Then type `hugo.exe` and press the **Add** button.
20
21 > [!NOTE]
22 > Virus scanning exclusions are common, but use caution when changing these settings. See the [Microsoft Defender Antivirus documentation][] for details.
23
24 Other virus scanners have similar exclusion mechanisms. See their respective documentation.
25
26 ## Template metrics
27
28 Hugo is fast, but inefficient templates impede performance. Enable template metrics to determine which templates take the most time, and to identify caching opportunities:
29
30 ```sh
31 hugo build --templateMetrics --templateMetricsHints
32 ```
33
34 The result will look something like this:
35
36 <!--
37 Run hugo build --templateMetrics --templateMetricsHints in this repo and paste
38 the results below. You must remove any line containing "__hdeferred" to prevent
39 this:
40
41 panic: deferred execution with id "__h" not found
42 -->
43
44 ```text {details=true}
45 Template Metrics:
46
47 cumulative average maximum cache percent cached total
48 duration duration duration potential cached count count template
49 ---------- -------- -------- --------- ------- ------ ----- --------
50 14.95 s 20.54 ms 278.74 ms 0 0 0 728 single.html
51 3.20 s 4.03 ms 33.23 ms 99 0 0 793 _partials/layouts/header/header.html
52 2.91 s 3.67 ms 29.58 ms 34 0 0 793 _partials/layouts/head/head.html
53 1.82 s 2.30 ms 23.97 ms 100 0 0 793 _partials/layouts/head/head-js.html
54 1.54 s 231.79 µs 23.49 ms 0 0 0 6632 _markup/render-link.html
55 1.37 s 1.73 ms 19.99 ms 91 0 0 793 _partials/layouts/header/qr.html
56 898.03 ms 338.24 µs 28.84 ms 0 0 0 2655 _markup/render-codeblock.html
57 828.14 ms 13.15 ms 42.49 ms 0 0 0 63 list.html
58 648.23 ms 817.44 µs 18.71 ms 100 0 0 793 _partials/layouts/footer.html
59 624.87 ms 787.98 µs 18.88 ms 23 0 0 793 _partials/opengraph/opengraph.html
60 515.61 ms 1.44 ms 23.17 ms 0 0 0 359 _shortcodes/code-toggle.html
61 431.74 ms 544.44 µs 16.66 ms 100 0 0 793 _partials/layouts/search/input.html
62 361.07 ms 227.66 µs 3.56 ms 58 0 0 1586 _partials/_inline/qr
63 353.60 ms 445.90 µs 17.17 ms 18 0 0 793 _partials/schema.html
64 292.99 ms 369.00 µs 18.63 ms 99 0 0 794 _partials/layouts/home/sponsors.html
65 282.03 ms 355.64 µs 15.65 ms 28 0 0 793 _partials/twitter_cards.html
66 279.74 ms 279.74 ms 279.74 ms 100 0 0 1 _partials/helpers/linkcss.html
67 230.48 ms 642.01 µs 9.04 ms 0 0 0 359 _markup/render-blockquote.html
68 227.22 ms 287.25 µs 12.19 ms 38 0 0 791 _partials/layouts/docsheader.html
69 206.41 ms 260.29 µs 10.04 ms 100 0 0 793 _partials/layouts/header/mobilemenu.html
70 202.75 ms 127.83 µs 15.57 ms 75 0 0 1586 _partials/helpers/linkjs.html
71 152.33 ms 192.09 µs 16.63 ms 100 0 0 793 _partials/layouts/search/results.html
72 137.02 ms 172.78 µs 10.88 ms 100 0 0 793 _partials/layouts/header/githubstars.html
73 121.06 ms 152.66 µs 1.55 ms 0 0 0 793 _partials/opengraph/get-featured-image.html
74 120.00 ms 165.07 µs 2.71 ms 15 0 0 727 _partials/layouts/in-this-section.html
75 116.10 ms 607.86 µs 11.93 ms 0 0 0 191 _shortcodes/new-in.html
76 84.77 ms 29.47 µs 5.92 ms 100 0 0 2876 _partials/inline/h-rh-l/validate-fragment.html
77 71.97 ms 80.51 µs 2.81 ms 0 0 0 894 _partials/inline/h-rh-l/get-glossary-link-attributes.html
78 70.27 ms 88.84 µs 5.66 ms 42 0 0 791 _partials/layouts/breadcrumbs.html
79 53.72 ms 1.68 ms 8.50 ms 0 0 0 32 _shortcodes/img.html
80 50.23 ms 63.34 µs 4.77 ms 0 0 0 793 _partials/layouts/hooks/body-main-start.html
81 48.01 ms 48.01 ms 48.01 ms 0 0 0 1 home.html
82 42.23 ms 42.23 ms 42.23 ms 0 0 0 1 404.html
83 41.13 ms 56.50 µs 2.49 ms 5 0 0 728 _partials/layouts/toc.html
84 39.26 ms 24.75 µs 1.28 ms 99 0 0 1586 _partials/_funcs/get-page-images.html
85 24.70 ms 31.15 µs 1.09 ms 100 0 0 793 _partials/helpers/gtag.html
86 23.87 ms 2.39 ms 10.01 ms 0 0 0 10 _markup/render-codeblock-goat.html
87 22.62 ms 96.24 µs 2.12 ms 80 0 0 235 _partials/layouts/blocks/feature-state.html
88 21.58 ms 9.07 µs 3.63 ms 100 100 2378 2380 _partials/helpers/funcs/get-github-info.html
89 21.19 ms 29.10 µs 902.28 µs 97 0 0 728 _partials/layouts/page-edit.html
90 20.03 ms 90.24 µs 1.49 ms 0 0 0 222 _shortcodes/include.html
91 20.02 ms 27.50 µs 593.31 µs 97 0 0 728 _partials/layouts/related.html
92 19.91 ms 19.91 ms 19.91 ms 0 0 0 1 /news/_content.gotmpl
93 19.83 ms 450.58 µs 4.69 ms 0 0 0 44 _shortcodes/deprecated-in.html
94 18.70 ms 18.70 ms 18.70 ms 100 0 0 1 _partials/helpers/funcs/get-remote-data.html
95 17.03 ms 74.37 µs 1.80 ms 0 0 0 229 _markup/render-table.html
96 10.61 ms 13.38 µs 2.29 ms 90 0 0 793 _partials/layouts/blocks/modal.html
97 10.14 ms 12.79 µs 1.17 ms 100 0 0 793 _partials/layouts/header/mastodon.html
98 9.57 ms 9.57 ms 9.57 ms 0 0 0 1 sitemap.xml
99 9.35 ms 24.68 µs 637.73 µs 50 0 0 379 _partials/layouts/blocks/alert.html
100 6.96 ms 8.77 µs 935.10 µs 99 0 0 794 _partials/layouts/search/button.html
101 5.38 ms 2.69 ms 3.20 ms 0 0 0 2 _shortcodes/quick-reference.html
102 5.32 ms 9.92 µs 1.03 ms 75 0 0 536 _partials/docs/functions-signatures.html
103 5.13 ms 6.47 µs 1.36 ms 100 0 0 793 _partials/layouts/hooks/body-end.html
104 5.02 ms 152.09 µs 1.22 ms 0 0 0 33 _shortcodes/glossary-term.html
105 4.65 ms 132.78 µs 1.71 ms 2 0 0 35 _partials/inline/get-resource.html
106 3.98 ms 1.99 ms 3.56 ms 0 0 0 2 _shortcodes/datatable.html
107 3.89 ms 388.92 µs 929.41 µs 0 0 0 10 _shortcodes/render-list-of-pages-in-section.html
108 3.80 ms 2.39 µs 963.41 µs 100 0 0 1586 _partials/layouts/header/theme.html
109 3.44 ms 6.42 µs 795.02 µs 19 0 0 536 _partials/docs/functions-aliases.html
110 3.10 ms 3.10 ms 3.10 ms 0 0 0 1 _shortcodes/glossary.html
111 2.57 ms 4.80 µs 256.63 µs 88 0 0 536 _partials/docs/functions-return-type.html
112 2.54 ms 2.54 ms 2.54 ms 100 0 0 1 _partials/layouts/home/opensource.html
113 1.97 ms 2.48 µs 501.94 µs 100 0 0 793 _partials/layouts/icons.html
114 1.85 ms 32.98 µs 178.18 µs 0 0 0 56 _shortcodes/eturl.html
115 1.74 ms 1.74 ms 1.74 ms 0 0 0 1 home.redir
116 1.70 ms 340.42 µs 596.87 µs 0 0 0 5 _shortcodes/qr.html
117 1.65 ms 235.82 µs 1.03 ms 0 0 0 7 _markup/render-passthrough.html
118 1.53 ms 1.53 ms 1.53 ms 0 0 0 1 _shortcodes/syntax-highlighting-styles.html
119 1.51 ms 1.51 ms 1.51 ms 0 0 0 1 _shortcodes/root-configuration-keys.html
120 1.45 ms 1.82 µs 839.08 µs 100 0 0 793 _partials/layouts/templates.html
121 1.34 ms 1.34 ms 1.34 ms 100 0 0 1 _partials/helpers/validation/validate-keywords.html
122 1.30 ms 1.30 ms 1.30 ms 0 0 0 1 _shortcodes/per-lang-config-keys.html
123 1.18 ms 21.89 µs 76.24 µs 0 0 0 54 _markup/render-image.html
124 1.15 ms 1.46 µs 36.50 µs 100 0 0 793 _partials/layouts/search/algolialogo.html
125 775.57 µs 775.57 µs 775.57 µs 100 0 0 1 _partials/helpers/picture.html
126 748.41 µs 374.21 µs 402.25 µs 0 0 0 2 list.rss.xml
127 718.91 µs 718.91 µs 718.91 µs 0 0 0 1 _shortcodes/figure.html
128 696.14 µs 232.04 µs 263.44 µs 0 0 0 3 _shortcodes/newtemplatesystem.html
129 676.21 µs 852.00 ns 16.23 µs 100 0 0 793 _partials/layouts/head/speculationrules.html
130 618.07 µs 618.07 µs 618.07 µs 0 0 0 1 _shortcodes/chroma-lexers.html
131 605.82 µs 763.00 ns 29.52 µs 100 0 0 793 _partials/layouts/hooks/body-start.html
132 444.33 µs 222.17 µs 342.78 µs 0 0 0 2 _shortcodes/highlight.html
133 398.33 µs 398.33 µs 398.33 µs 0 0 0 1 _shortcodes/render-table-of-pages-in-section.html
134 243.64 µs 121.82 µs 125.32 µs 0 0 0 2 _shortcodes/youtube.html
135 235.46 µs 39.24 µs 66.03 µs 0 0 0 6 _shortcodes/current-go-version.html
136 229.98 µs 12.78 µs 39.15 µs 0 0 0 18 _shortcodes/get-page-desc.html
137 193.19 µs 8.05 µs 34.85 µs 52 0 0 24 _partials/layouts/date.html
138 184.80 µs 184.80 µs 184.80 µs 0 0 0 1 _shortcodes/x.html
139 125.61 µs 125.61 µs 125.61 µs 0 0 0 1 _shortcodes/details.html
140 106.29 µs 106.29 µs 106.29 µs 0 0 0 1 _shortcodes/hl.html
141 81.00 µs 81.00 µs 81.00 µs 0 0 0 1 _shortcodes/syntax-highlighting-styles-light-with-counterpart.html
142 47.32 µs 23.66 µs 40.66 µs 0 0 0 2 _shortcodes/param.html
143 42.67 µs 42.67 µs 42.67 µs 100 0 0 1 _partials/layouts/home/features.html
144 37.00 µs 37.00 µs 37.00 µs 0 0 0 1 _shortcodes/instagram.html
145 35.17 µs 35.17 µs 35.17 µs 0 0 0 1 _shortcodes/vimeo.html
146 1.95 µs 1.95 µs 1.95 µs 0 0 0 1 home.headers
147 1.81 µs 258.00 ns 977.00 ns 0 0 0 7 _shortcodes/module-mounts-note.html
148 ```
149
150 From left to right, the columns represent:
151
152 cumulative duration
153 : The cumulative time spent executing the template.
154
155 average duration
156 : The average time spent executing the template.
157
158 maximum duration
159 : The maximum time spent executing the template.
160
161 cache potential
162 : Displayed as a percentage, any _partial_ template with a 100% cache potential should be called with the [`partialCached`][] function instead of the [`partial`][] function. See the [caching](#caching) section below.
163
164 > [!WARNING]
165 > A 100% cache potential is calculated by comparing each execution's rendered output to the first execution's output, so it cannot detect side effects such as calls to [`warnf`][] or [`errorf`][], or execution order dependencies such as a conditional based on [`IsHome`][]. A _partial_ template that produces no visible output but performs validation, logging, or conditional logic based on context can show 100% cache potential even though caching it would suppress that behavior for all but the first invocation. Review the template's logic before switching it to `partialCached`.
166
167 percent cached
168 : The number of times the rendered templated was cached divided by the number of times the template was executed.
169
170 cached count
171 : The number of times the rendered templated was cached.
172
173 total count
174 : The number of times the template was executed.
175
176 template
177 : The path to the template, relative to the `layouts` directory.
178
179 > [!NOTE]
180 > Hugo builds pages in parallel where multiple pages are generated simultaneously. Because of this parallelism, the sum of "cumulative duration" values is usually greater than the actual time it takes to build a site.
181
182 ## Caching
183
184 Some _partial_ templates such as sidebars or menus are executed many times during a site build. Depending on the content within the _partial_ template and the desired output, the template may benefit from caching to reduce the number of executions. The [`partialCached`][] function provides caching capabilities for _partial_ templates.
185
186 > [!NOTE]
187 > Note that you can create cached variants of each _partial_ template by passing additional arguments to `partialCached` beyond the initial context. See the `partialCached` documentation for more details.
188
189 ## Timers
190
191 Use the [`debug.Timer`][] function to determine execution time for a block of code, useful for finding performance bottlenecks in templates.
192
193 [Microsoft Defender Antivirus documentation]: https://support.microsoft.com/en-us/topic/how-to-add-a-file-type-or-process-exclusion-to-windows-security-e524cbc2-3975-63c2-f9d1-7c2eb5331e53
194 [`debug.Timer`]: /functions/debug/timer/
195 [`errorf`]: /functions/fmt/errorf/
196 [`IsHome`]: /methods/page/ishome/
197 [`partialCached`]: /functions/partials/includecached/
198 [`partial`]: /functions/partials/include/
199 [`warnf`]: /functions/fmt/warnf/