Skip to content

[MOB-10804] Rework append for better memory efficiency#56

Closed
MiguelAMarcelino wants to merge 1 commit intomasterfrom
MiguelMarcelino/MOB-10804-improve-append-memory
Closed

[MOB-10804] Rework append for better memory efficiency#56
MiguelAMarcelino wants to merge 1 commit intomasterfrom
MiguelMarcelino/MOB-10804-improve-append-memory

Conversation

@MiguelAMarcelino
Copy link

Incorporating changes submitted on this PR: jknack#1156

Trying to optimize the append method to improve memory efficiency and reduce object allocations. Previously, the code relied on the textWithoutEscapeChar method, which involved converting the internal StringBuilder to a String and then to a char[]. My goal here is to reduce unnecessary intermediate allocations and processing overhead.

Changes Introduced

  • Streamlined operations to minimize the number of object switches and allocations during the append process.
  • Removed textWithoutEscapeChar to avoid the conversion chain (StringBuilderStringchar[]).

Alternative Consideration:

An alternative approach considered was to rewrite the textWithoutEscapeChar method for better performance. The revised method would look like this:

public char[] textWithoutEscapeChar() {
  char[] result = new char[text.length()];
  text.getChars(0, text.length(), result, 0);
  return result;
}

This version avoids converting the StringBuilder to a String and directly copies the characters into a char[], reducing unnecessary overhead.

Future Enhancements:

There is a potential case to investigate the frequent resizing of the StringBuilder used in Text. Pre-sizing the StringBuilder to an appropriate capacity may further improve performance and memory usage.

@gmethvin gmethvin force-pushed the master branch 17 times, most recently from 81a365f to 493047d Compare April 27, 2025 07:16
@MiguelAMarcelino MiguelAMarcelino self-assigned this May 6, 2025
@MiguelAMarcelino
Copy link
Author

Closing. This has been merged on the official handlebars repo. Now its a matter of synching both.

@MiguelAMarcelino MiguelAMarcelino deleted the MiguelMarcelino/MOB-10804-improve-append-memory branch September 29, 2025 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant