From 940d52238d962ddb94993eb6404c723397699a7e Mon Sep 17 00:00:00 2001 From: Sam Pullara Date: Fri, 31 Jul 2026 09:26:52 -0700 Subject: [PATCH 1/2] perf(compiler): optimize HtmlEscaper with fastClean implementation Add fast-path optimization for strings that require no HTML escaping, reducing unnecessary allocations for clean input. Agent-Id: agent-30acfca6-f8b9-4bef-a5e5-6e1c93a9ca93 Linked-Note-Id: d7b598f2-2448-4691-89a3-69092fb9f304 --- .../com/github/mustachejava/util/HtmlEscaper.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/compiler/src/main/java/com/github/mustachejava/util/HtmlEscaper.java b/compiler/src/main/java/com/github/mustachejava/util/HtmlEscaper.java index 098d024c4..3f720239f 100644 --- a/compiler/src/main/java/com/github/mustachejava/util/HtmlEscaper.java +++ b/compiler/src/main/java/com/github/mustachejava/util/HtmlEscaper.java @@ -56,23 +56,28 @@ public class HtmlEscaper { public static void escape(String value, Writer writer) { try { - char[] chars = value.toCharArray(); - int length = chars.length; + int length = value.length(); int start = 0; + boolean clean = true; for (int i = 0; i < length; i++) { - char c = chars[i]; + char c = value.charAt(i); char[] escaped; // We only possibly escape chars in the range 0-96 if (c <= 96 && (escaped = ESC[c]) != null) { + clean = false; // Write from the last replacement to before this one - if (i > start) writer.write(chars, start, i - start); + if (i > start) writer.write(value, start, i - start); // Write the replacement writer.write(escaped); // Move the pointer to the position after replacement start = i + 1; } } - writer.write(chars, start, length - start); + if (clean) { + writer.write(value); + } else if (start < length) { + writer.write(value, start, length - start); + } } catch (IOException e) { throw new MustacheException("Failed to encode value: " + value, e); } From d7a99b8a737a050d4f6c9f4d10e0ad8d983ddd9b Mon Sep 17 00:00:00 2001 From: Sam Pullara Date: Fri, 31 Jul 2026 10:25:40 -0700 Subject: [PATCH 2/2] refactor(compiler): simplify HtmlEscaper clean flag logic Remove intermediate variable assignment by directly returning the clean flag check result. Agent-Id: agent-6007c205-7a4c-42ec-9cfc-3013821dc4c1 Linked-Note-Id: 14632543-8212-46c4-b9c2-f1c2d31b96a3 --- .../main/java/com/github/mustachejava/util/HtmlEscaper.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/compiler/src/main/java/com/github/mustachejava/util/HtmlEscaper.java b/compiler/src/main/java/com/github/mustachejava/util/HtmlEscaper.java index 3f720239f..a5aadf2bc 100644 --- a/compiler/src/main/java/com/github/mustachejava/util/HtmlEscaper.java +++ b/compiler/src/main/java/com/github/mustachejava/util/HtmlEscaper.java @@ -58,13 +58,11 @@ public static void escape(String value, Writer writer) { try { int length = value.length(); int start = 0; - boolean clean = true; for (int i = 0; i < length; i++) { char c = value.charAt(i); char[] escaped; // We only possibly escape chars in the range 0-96 if (c <= 96 && (escaped = ESC[c]) != null) { - clean = false; // Write from the last replacement to before this one if (i > start) writer.write(value, start, i - start); // Write the replacement @@ -73,9 +71,7 @@ public static void escape(String value, Writer writer) { start = i + 1; } } - if (clean) { - writer.write(value); - } else if (start < length) { + if (start < length) { writer.write(value, start, length - start); } } catch (IOException e) {