From f5838ab6036181d864be96a180e00b7295d5f497 Mon Sep 17 00:00:00 2001 From: Bernhard Haumacher Date: Thu, 25 Jun 2015 17:36:41 +0200 Subject: [PATCH] Prevent multiple initializations of the Thymeleaf template engine. Initializing the template engine is a costly operation, because this drops the resolved templates. When initializing the template engine for each page, all used templates must be resolved for each rendering operation. This greatly reduces rendering performance. Instead, only re-initialize the template engine, if the template mode changes. --- .../org/jbake/template/ThymeleafTemplateEngine.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/jbake/template/ThymeleafTemplateEngine.java b/src/main/java/org/jbake/template/ThymeleafTemplateEngine.java index cbcaa73a3..935e67dc7 100644 --- a/src/main/java/org/jbake/template/ThymeleafTemplateEngine.java +++ b/src/main/java/org/jbake/template/ThymeleafTemplateEngine.java @@ -49,15 +49,21 @@ public class ThymeleafTemplateEngine extends AbstractTemplateEngine { private TemplateEngine templateEngine; private FileTemplateResolver templateResolver; + private String templateMode; + public ThymeleafTemplateEngine(final CompositeConfiguration config, final ContentStore db, final File destination, final File templatesPath) { super(config, db, destination, templatesPath); - initializeTemplateEngine(); } - private void initializeTemplateEngine() { + private void initializeTemplateEngine(String mode) { + if (mode.equals(templateMode)) { + return; + } + templateMode = mode; templateResolver = new FileTemplateResolver(); templateResolver.setPrefix(templatesPath.getAbsolutePath() + File.separatorChar); templateResolver.setCharacterEncoding(config.getString(Keys.TEMPLATE_ENCODING)); + templateResolver.setTemplateMode(mode); templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); try { @@ -75,7 +81,6 @@ public void renderDocument(final Map model, final String templat Context context = new Context(locale, wrap(model)); lock.lock(); try { - initializeTemplateEngine(); @SuppressWarnings("unchecked") Map config = (Map) model.get("config"); @SuppressWarnings("unchecked") @@ -88,7 +93,7 @@ public void renderDocument(final Map model, final String templat mode = configMode; } } - templateResolver.setTemplateMode(mode); + initializeTemplateEngine(mode); templateEngine.process(templateName, context, writer); } finally { lock.unlock();