[SOLVED] How to decrease build time? 25103 ms is a lot of time

Hello everyone.

I have one website, with many authors and a lot of posts/articles. I migrated from WP to Hugo about 6 months ago. To develop with Hugo, I just run hugo server or hugo server --ignoreCache --disableLiveReload, but the time of build is very long. See the statistics of Hugo below:

Built site for language pt-br:
0 of 1 draft rendered
0 future content
0 expired content
1771 regular pages created
3128 other pages created
0 non-page files copied
0 paginator pages created
240 authors created
1219 tags created
98 categories created
total in 25103 ms

I saw this video: https://www.youtube.com/watch?v=CdiDYZ51a2o
But I don’t have any tips to increase Hugo build performance. Can someone enlight me?

The code of website can be found here:

25 seconds is hardly “a lot of time” for all the work done here.

But …

running with hugo --templateMetrics --templateMetricsHints gives a few hints:

Template Metrics:

      cache     cumulative       average       maximum
  potential       duration      duration      duration  count  template
      -----     ----------      --------      --------  -----  --------
          0 53.289292513s    30.15806ms    64.62134ms   1767  theme/post/single.html
          5 47.147678647s   14.137234ms   43.196219ms   3335  theme/partials/header.html
          0 24.348549795s    15.58806ms  151.642563ms   1562  theme/_default/list.html
          0  1.159257693s     739.794µs   89.294725ms   1567  _internal/_default/rss.xml
        100  777.615582ms     440.076µs    8.225484ms   1767  theme/partials/sidebar.html
         10  584.000871ms     330.504µs   26.923459ms   1767  theme/partials/posthero.html
         59  481.344118ms      30.549µs    5.863613ms  15756  theme/partials/byauthor.html
         99  220.747866ms     124.505µs    7.099817ms   1773  theme/partials/footer.html
          0  172.671316ms  172.671316ms  172.671316ms      1  theme/section/todos-os-posts.html
          0   75.900584ms   75.900584ms   75.900584ms      1  _internal/_default/sitemap.xml
          0   57.226729ms   14.306682ms   15.693283ms      4  theme/page/single.html
         83   51.637655ms      29.223µs    1.491962ms   1767  theme/partials/disqus.html
         50   41.510745ms      23.478µs    5.660257ms   1768  theme/partials/postdate.html
          0   31.628304ms   31.628304ms   31.628304ms      1  theme/index.html
          0    1.711742ms     131.672µs     319.473µs     13  shortcodes/codepen.html
         57     737.978µs     245.992µs     378.668µs      3  theme/partials/postbox.html
          0     405.555µs     405.555µs     405.555µs      1  shortcodes/jsfiddle.html
          0       91.04µs       91.04µs       91.04µs      1  _internal/_default/robots.txt
          0      79.004µs      79.004µs      79.004µs      1  theme/404.html
  • You can save 777 ms by using partialCached for the sidebar partial, and you can probably also do that for the footer.
  • You can save a lot by doing less work in theme/post/single.html – you need to look at the related feature in Hugo to replace the intersect construct. https://gohugo.io/content-management/related/

I tried to use the partialCached another time, but, without tips of --templateMetrics and --templateMetricsHints, I did not see any changes. Now, I’m using the partialCached in Header, Footer and Sidebar.

But, the tip of related feature is gold. Thank you a lot!

Built site for language pt-br:
0 draft content
0 future content
0 expired content
1775 regular pages created
3134 other pages created
0 non-page files copied
0 paginator pages created
240 authors created
1219 tags created
98 categories created
total in 2181 ms

Just to resume the changes:

  • Modify the related posts script to original solution of Hugo
  • Use partialCached in Footer, Sidebar and Header.

Just to know: I remove the Tags from my taxonomies. I realize that I use tags just to make the Related Posts. But, I configure the Related to take Categories first, title in second after authors. Removing the Tags Taxonomy, the build on the server got much faster. This decrease the number of other pages created from 3134 to 714.

This gives me more freedom to organize where I can use the partialCached without break some page information. In past post, I said that I was using partialCached in Sidebar and Header. Doing this, I can get just 2181ms to build in development environment, but I can’t use cached in this partials because the info in that areas changes sometimes. So, I rollback the partial default include, but the build increase to something like ~5000ms. Removing the tags like I said before, I get ~2500ms. Results:

Built site for language pt-br:
0 draft content
0 of 2 futures rendered
0 expired content
1779 regular pages created
714 other pages created
0 non-page files copied
0 paginator pages created
107 categories created
241 authors created
total in 2696 ms

Note that you can still use the tags values in related even if you don’t have a taxonomy configured for it.

Yeah. I’m using it, but with a small weight configured, since the categories is much to have taxonomy when authors write and tags don’t! :slight_smile:

Thank for your help @bep.

1 Like