Skip to content

Conversation

@MiguelAMarcelino
Copy link
Contributor

Incorporating changes submitted on this PR: #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.

@MiguelAMarcelino MiguelAMarcelino changed the title Miguel marcelino/mob 10804 improve append memory [MOB-10804] Improve Append Memory Efficiency Feb 4, 2025
@MiguelAMarcelino MiguelAMarcelino deleted the MiguelMarcelino/MOB-10804-improve-append-memory branch February 4, 2025 16:36
@MiguelAMarcelino MiguelAMarcelino restored the MiguelMarcelino/MOB-10804-improve-append-memory branch February 4, 2025 16:37
@MiguelAMarcelino MiguelAMarcelino changed the title [MOB-10804] Improve Append Memory Efficiency Improve Append Memory Efficiency Feb 4, 2025
@MiguelAMarcelino MiguelAMarcelino deleted the MiguelMarcelino/MOB-10804-improve-append-memory branch February 4, 2025 16:38
@MiguelAMarcelino MiguelAMarcelino restored the MiguelMarcelino/MOB-10804-improve-append-memory branch February 4, 2025 16:39
@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.

5 participants