Debugging performance problems with a theme

Today I tried switching to a new theme — the “Bootstrap 4 alpha” theme at https://github.com/alanorth/hugo-theme-bootstrap4-blog

I immediately noticed a couple of problems. First of all, it was taking over 2 minutes to render my site; and secondly, almost all the posts were being missed.

% hugo --stepAnalysis
Started building sites ...
Go initialization:
	3.533811622s (3.535578132s)	    6.12 MB 	71418 Allocs
initialize:
	277.577µs (3.53594467s)	    0.17 MB 	276 Allocs
load data:
	3.002568ms (3.539011427s)	    0.06 MB 	1278 Allocs
load i18n:
	573ns (3.539063599s)	    0.00 MB 	0 Allocs
read pages from source:
	913.070545ms (4.452292788s)	  119.12 MB 	3074045 Allocs
convert source:
	335.236608ms (4.787836593s)	  105.54 MB 	831970 Allocs
build Site meta:
	87.606369ms (4.875880306s)	    4.63 MB 	149951 Allocs
prepare pages:
	6.426753969s (11.303129469s)	   76.02 MB 	798712 Allocs
render and write aliases:
	2.051386ms (11.305582615s)	    0.02 MB 	221 Allocs
render and write pages:
	2m40.103532343s (2m51.409721918s)	 36652.70 MB 	964158256 Allocs
render and write Sitemap:
	236.939836ms (2m51.647254629s)	   25.27 MB 	537135 Allocs
render and write robots.txt:
	20.683µs (2m51.647944767s)	    0.00 MB 	8 Allocs
render and write 404:
	165.863µs (2m51.649152121s)	    0.01 MB 	147 Allocs
Built site for language en:
0 draft content
0 future content
0 expired content
2321 regular pages created
5709 other pages created
0 non-page files copied
8003 paginator pages created
5702 tags created
2 categories created
total in 168114 ms

After some experimentation, I determined that the common factor causing pages to vanish from the output was that they had the line

layout = "post"

in the TOML frontmatter. They had this as a side effect of bulk converting them from WordPress. I don’t know whether this counts as a bug or not?

I wrote a quick sed incantation to remove that line from every file, and also remove a couple of other unnecessary lines (categories and author). This has reduced the run time somewhat, and the posts now all appear, but it’s still slow, so my secondary issue is performance:

% hugo --stepAnalysis
Started building sites ...
Go initialization:
	1.011678455s (1.012267451s)	    6.13 MB 	71433 Allocs
initialize:
	232.278µs (1.012539412s)	    0.17 MB 	276 Allocs
load data:
	1.142027ms (1.013765438s)	    0.06 MB 	1278 Allocs
load i18n:
	331ns (1.01380913s)	    0.00 MB 	0 Allocs
read pages from source:
	491.554132ms (1.505508617s)	  103.52 MB 	2376479 Allocs
convert source:
	197.364436ms (1.703117013s)	  105.52 MB 	831901 Allocs
build Site meta:
	27.543657ms (1.73092476s)	    4.27 MB 	136336 Allocs
prepare pages:
	3.189907433s (4.921231731s)	   76.11 MB 	798736 Allocs
render and write aliases:
	8.042099ms (4.929590933s)	    0.02 MB 	220 Allocs
render and write pages:
	4m29.984140654s (4m34.914395447s)	 35765.13 MB 	946783013 Allocs
render and write Sitemap:
	278.499853ms (4m35.193721854s)	   25.06 MB 	537141 Allocs
render and write robots.txt:
	15.514µs (4m35.194282554s)	    0.00 MB 	8 Allocs
render and write 404:
	490.542µs (4m35.195725495s)	    0.01 MB 	147 Allocs
Built site for language en:
0 draft content
0 future content
0 expired content
2321 regular pages created
5709 other pages created
0 non-page files copied
7549 paginator pages created
5702 tags created
2 categories created
total in 274184 ms

I’m using release 0.19:

% hugo version
Hugo Static Site Generator v0.19 darwin/amd64 BuildDate: 2017-02-27T12:36:32-06:00

If I switch back to the old theme, everything renders in a reasonable amount of time again:

% hugo --stepAnalysis
Started building sites ...
Go initialization:
	134.523734ms (135.103929ms)	    5.22 MB 	55176 Allocs
initialize:
	254.437µs (135.413814ms)	    0.17 MB 	296 Allocs
load data:
	69.402µs (135.517164ms)	    0.01 MB 	45 Allocs
load i18n:
	263ns (135.56444ms)	    0.00 MB 	0 Allocs
read pages from source:
	278.998834ms (414.6632ms)	  103.23 MB 	2367197 Allocs
convert source:
	148.017776ms (562.899758ms)	  105.53 MB 	831901 Allocs
build Site meta:
	27.295549ms (590.478514ms)	    4.26 MB 	136323 Allocs
prepare pages:
	2.261568164s (2.85234764s)	   76.17 MB 	798740 Allocs
render and write aliases:
	470.861µs (2.85305317s)	    0.02 MB 	220 Allocs
render and write pages:
	2.31579503s (5.169258963s)	  219.21 MB 	4637326 Allocs
render and write Sitemap:
	297.951902ms (5.467664325s)	   39.23 MB 	737082 Allocs
render and write robots.txt:
	16.477µs (5.468096967s)	    0.00 MB 	8 Allocs
render and write 404:
	369.684µs (5.46895659s)	    0.02 MB 	323 Allocs
Built site for language en:
0 draft content
0 future content
0 expired content
2321 regular pages created
5709 other pages created
0 non-page files copied
465 paginator pages created
5702 tags created
2 categories created
total in 5334 ms

So, this is a theme problem, yes? Any idea how I could work out where the performance issues are coming from?

  1. When defining layout in front matter, you tell Hugo to use that layout (template), so if that template is not found, then you get nothing.
  2. Re. preformance: This looks like the suspect:

I set custom_navbar and created the nav manually, and got a ~50% improvement:

render and write pages:
	2m3.56504188s (2m9.199885211s)	 30916.45 MB 	786266456 Allocs

This also looks expensive in a big site:

We will eventually fix this by adding some cache in where and friends, but this “top n” should definitively be put in a partial fetched with partialCached.

OK! Down to 18 seconds now. I can live with that.

render and write pages:
	18.153088161s (20.179328929s)	 4623.76 MB 	118113944 Allocs

As far as the other issue, I think it would be a good idea if hugo warned about any content that specifies a layout for which there is no such template. Is there any case where you would want that to happen?

Yes we should, but you would have to create a GitHub issue to track it.

Curious; how many content pages in your site?

Yes we should, but you would have to create a GitHub issue to track it.

OK, done.

Curious; how many content pages in your site?

2340 Markdown files. I have content going back to around 1988, and I remember the good ol’ days of HTML 1.0 and my own static site generator in Perl!

3 Likes