samskivert / jmustache

A Java implementation of the Mustache templating language.
Other
828 stars 129 forks source link

Partial thread safe (merge #154 first) #155

Closed agentgt closed 7 months ago

agentgt commented 7 months ago

@samskivert

This PR builds on top of #154 . Merge that one first and I will rebase it on master and then you can merge it.

It fixes two problems:

Without the lock in a highly threaded environment the template can re-parsed an overwhelming amount of times.

For example the unit test code shows how the template can be loaded and parsed 63 times!

With the advent of Virtual Threads in JDK 21 you can have this happen way more often and if the template is coming from a database call this can be very expensive and outweighs the additional overhead of a lock (in almost all cases regardless of where the template comes from).

If you are wondering why _template never had threading problems (e.g. NPE on _template being null) not being volatile is because in 64 bit platforms all references are inherently atomic but this is not the case for 32 bit platforms like Raspberry PI or other JVM implementations.

agentgt commented 7 months ago

@samskivert You can merge now. PR is now rebased onto master.