neuland / pug4j

a pug implementation written in Java (formerly known as jade)
MIT License
61 stars 12 forks source link

Add customizability to JaxelEngine #24

Open Selaron opened 1 year ago

Selaron commented 1 year ago

In order to play with performance relevant options I'd like to be able to customize more properties of the JexlExpressionHandler:

Including:

  1. cache size (max number of cached expressions; default 5000)
  2. cacheThreshold (max length of expressions to be cached; default 64)
  3. debug (whether or not to provide debug info; default true)

While the advantage of being able to play around with 1 and 2 is quite self explaining, 3 maybe not:

As debug is enabled by default and all these options do not seem to be customizable easily without implementing a very custom ExpressionHandler, each rendering (huge) pug templates results in thousands of these executions:

    public JexlInfo() {
       final StackTraceElement[] stack = new Throwable().getStackTrace();
       ...
     }

which produces a lot of full stack traces of arbitrary length and is likely to hit the rendering speed.

As a simple solution maybe it would be enough to have the method de.neuland.pug4j.expression.JexlExpressionHandler#getJexlEngine refactored to use a PugJexlBuilder received from a protected method which we can override like:

@Override
protected PugJexlBuilder getPugJexlBuilder() {
   PugJexlBuilder builder = super.getPugJexlBuilder();
   builder.debug(false);
   builder.cache(10000);
   builder.cacheThreshold(256);
   return builder;
}