handlebars.java icon indicating copy to clipboard operation
handlebars.java copied to clipboard

Improve handlebars performance on large templates

Open Mahoney opened this issue 1 year ago • 1 comments

org.apache.commons.text.TextStringBuilder has a very poor buffer growth strategy.

It increases it by the length of the string to be appended, so every append causes all the existing data to be copied to a new location in memory.

java.io.StringBuilder has a much better approach of doubling the capacity of the buffer each time it is too small, resulting in far fewer copies.

Additionally, it seems to me that setting the initial capacity to double the size of the template will minimise the chances of a copy being needed at all.

On my machine LargeHbsBench runs c. 100 times faster, andHbsBench runs about 20% faster.

Mahoney avatar Jun 08 '23 20:06 Mahoney