Thymeleaf Cacheing Optimization

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Thymeleaf Cacheing Optimization

pschultz
Once in the cache, does Thymeleaf optimize processing of DOM subtrees that have no processing need? In other words, will it short circuit its recursive processing and move to the next sibling or parent if there are no dialects applied?

I think I'm looking in the right place to answer this question (NestableNode): https://github.com/thymeleaf/thymeleaf/blob/2.0-master/src/main/java/org/thymeleaf/dom/NestableNode.java#L621

It looks like it will process every node and there is no mechanism to mark an entire subtree as immutable.

Does it make sense what I'm trying to ask?
Reply | Threaded
Open this post in threaded view
|

Re: Thymeleaf Cacheing Optimization

Emanuel
Administrator
AFAIK, caching just saves having to request a template from the TemplateResolver (which is the interface that loads a template from the servlet context / classloader / file system / database / whatever), creating a DOM of that template once, then just serving that preconstructed DOM every time the template is requested by the user, dialects, or by other Thymeleaf internals.

Saving processing time by skipping 'no-processing-required' DOM nodes in the template is an interesting idea for a performance optimization.  If that isn't already in the todo list, I might want to create an enhancement for the idea :)
Reply | Threaded
Open this post in threaded view
|

Re: Thymeleaf Cacheing Optimization

danielfernandez
Administrator
Actually, thymeleaf works like that since 2.0 :-)

After a template is parsed, and before it is stored at the cache, all its nodes are "precomputed": https://github.com/thymeleaf/thymeleaf/blob/2.0-master/src/main/java/org/thymeleaf/dom/Node.java#L749

Among other things, this precomputation computes the list of processors that should be applied on each node, and marks those that have no applicable processors as "skippable", which avoids them from being executed again: https://github.com/thymeleaf/thymeleaf/blob/2.0-master/src/main/java/org/thymeleaf/dom/Node.java#L843

Of course, this "skippable" flag might be modified at runtime because some processors might create new attributes or nodes in the DOM, and therefore the processors that should be applied to these attributes or nodes would have to be recomputed agan (that's why the "recomputeProcessorsImmediately" and "recomputeProcessorsAfterEachExecution" exist at the Node class).

Hope this answers your question!

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: Thymeleaf Cacheing Optimization

pschultz
Ah, this is exactly what I was looking for. I just needed to look at the parent class a bit more :)

Thank You,
Patrick