jbake
jbake copied to clipboard
Make tags available as a map to scale better (minor)
I have use case pages such as "vehicleRouting.adoc" that define an attribute jbake-relatedTag=vehicle routing
which is picked up by the useCaseTemplate.ftl
to add a link to all related blog posts for that relatedTag.
Here's how that code looks like:
<#assign relatedTags = tags?filter(tag -> tag.name == content.related_tag)>
<#if relatedTags?size > 0>
<#assign relatedTag = relatedTags?first>
<h2>Related blog posts</h2>
<ul>
<#list relatedTag.tagged_posts as blog>
<li>
<div class="title">
<a href="${content.rootpath}${blog.uri}">${blog.title}</a>
</div>
</li>
</#list>
</ul>
</#if>
What's the problem with this? This part won't scale, as every single page executes it:
tags?filter(tag -> tag.name == content.related_tag)?first
Given 1000 pages and 2000 tags, this causes 2 000 000 iterations. If the tags would be put into map by jbake at bootstrap, that would instead be:
tags[content.related_tag]
which it would give 2000 iterations to create that map once, and then 1000 times a hash lookup. So 3000 iterations instead of 2 000 000.
That being said, I haven't seen this cause significant performance issues yet with our website with 105 pages.