Comments (4)
Quick addition to @aschwarte10 's comment above: the good results for the Java StringWriter are with JDK 17. There have been some changes between e.g. JDK 11 and 17 there, so the performance of the plain StringWriter might depend on the underlying Java version...
from handlebars.java.
I think we may be seeing something similar... we've got a large template (4MB).
In a docker container running in Docker Desktop on my box, rendering it precompiled takes > 2,000 milliseconds.
On the same box but running the JVM outside docker, rendering it precompiled takes < 30 milliseconds.
Handlebars 4.3.1
Docker env:
openjdk version "17.0.7" 2023-04-18
OpenJDK Runtime Environment (build 17.0.7+7-alpine-r1)
OpenJDK 64-Bit Server VM (build 17.0.7+7-alpine-r1, mixed mode, sharing)
I'm surprised that it can be 2 orders of magnitude difference between in and out of a docker container... but taking a JFR recording it does look like a lot of the time is spent in reallocate:
java.util.Arrays.copyOf(Arrays.java:3633)
at org.apache.commons.text.TextStringBuilder.reallocate(TextStringBuilder.java:2610)
at org.apache.commons.text.TextStringBuilder.ensureCapacity(TextStringBuilder.java:1829)
at org.apache.commons.text.TextStringBuilder.append(TextStringBuilder.java:646)
at org.apache.commons.text.TextStringBuilder.append(TextStringBuilder.java:619)
at org.apache.commons.text.TextStringBuilder.append(TextStringBuilder.java:531)
at com.github.jknack.handlebars.internal.FastStringWriter.append(FastStringWriter.java:55)
at com.github.jknack.handlebars.internal.Variable.merge(Variable.java:148)
at com.github.jknack.handlebars.internal.BaseTemplate.apply(BaseTemplate.java:126)
at com.github.jknack.handlebars.internal.TemplateList.merge(TemplateList.java:95)
at com.github.jknack.handlebars.internal.BaseTemplate.apply(BaseTemplate.java:126)
at com.github.jknack.handlebars.internal.BaseTemplate.apply(BaseTemplate.java:114)
at com.github.jknack.handlebars.internal.ForwardingTemplate.apply(ForwardingTemplate.java:100)
from handlebars.java.
I'm an idiot, I wasn't actually evaluating the template locally - when I did it had similar performance characteristics to the behaviour in Docker.
The issue is that the buffer char[]
is only 1,600 characters by default, and as you evaluate a template it only increases by the amount you can append to it each time. In our case we end up with a 4,000,000 chars, and it mostly increments by 10 to 50 characters, so we literally copy the data tens of thousands of times.
I can "fix" it by setting the system property -Dhbs.buffer=4200000
- but allocating a 4MB buffer for every template seems very wasteful when most are much much smaller. If the buffer doubled each time it was too small that might be a better solution? Or if it used the template size as a starting point?
from handlebars.java.
I've raised #1066 as a suggested fix for this
from handlebars.java.
Related Issues (20)
- Question: Custom classpath loader for bundles
- Inline Partials allow infinite recursion
- Unexpected IllegalArgumentException using an inline template and a custom Writer
- Whitespace trimming does not work on partial blocks HOT 1
- Can't find any helper to do sum of field in list
- Ambiguity in a Helper function's first parameter
- The Options.params array omits the first parameter in a Helper
- Expressions that start with numbers fail parsing
- [StringHelpers] Add support for Calendar in dateFormat
- Java Helpers Converted to Javascript Helpers HOT 1
- Broken feature: `EnumMap` lookup HOT 1
- Escaping expressions (ESC_VAR) does not work for raw vars or any custom delimiter
- Dependency commons-text not shown in gradle and web UI
- I can't build successfully, why? I use jdk 8 HOT 1
- Handle Bars for Android
- Helpers without context fail to render correctly
- Curly braces parser greediness
- 4.4.0 breaks Java 8 support
- If-Else block allow multiple else blocks
- Preserving whitespace in response at MissingValueHelper
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from handlebars.java.