Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handlebars performance on large templates #2211

Merged
merged 1 commit into from
Jun 9, 2023

Conversation

Mahoney
Copy link
Collaborator

@Mahoney Mahoney commented Jun 8, 2023

Handlebars 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.

Submitter checklist

  • The PR request is well described and justified, including the body and the references
  • The PR title represents the desired changelog entry
  • The repository's code style is followed (see the contributing guide)
  • Test coverage that demonstrates that the change works as expected
  • For new features, there's necessary documentation in this pull request or in a subsequent PR to wiremock.org

@Mahoney Mahoney force-pushed the efficient-handlebars branch 3 times, most recently from f6fbec5 to d09b13d Compare June 8, 2023 19:52
Handlebars `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.
@Mahoney
Copy link
Collaborator Author

Mahoney commented Jun 9, 2023

I've also raised jknack/handlebars.java#1066 to improve upstream

@tomakehurst tomakehurst merged commit f9208e0 into master Jun 9, 2023
7 checks passed
@tomakehurst tomakehurst deleted the efficient-handlebars branch June 9, 2023 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants