You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ta...@apache.org on 2026/06/03 16:48:51 UTC
(myfaces) branch main updated: [perf] avoid new String creation
This is an automated email from the ASF dual-hosted git repository.
tandraschko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/myfaces.git
The following commit(s) were added to refs/heads/main by this push:
new af6fab956 [perf] avoid new String creation
af6fab956 is described below
commit af6fab9563b600fab6ae5a439706e2930d02e14d
Author: tandraschko <ta...@apache.org>
AuthorDate: Wed Jun 3 18:48:42 2026 +0200
[perf] avoid new String creation
---
.../renderkit/html/HtmlResponseWriterImpl.java | 159 ++++++++++-----------
.../renderkit/html/HtmlResponseWriterImplTest.java | 87 +++++++++++
2 files changed, 160 insertions(+), 86 deletions(-)
diff --git a/impl/src/main/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImpl.java b/impl/src/main/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImpl.java
index d5ecb4b77..5d65c3654 100644
--- a/impl/src/main/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImpl.java
@@ -66,6 +66,8 @@ public class HtmlResponseWriterImpl extends ResponseWriter
private static final String COMMENT_COMMENT_END = "\n//-->";
private static final String COMMENT_END = "\n-->";
+ private static final String JAVASCRIPT_SCHEME = "javascript:";
+
static private final String[][] EMPTY_ELEMENT_ARR = new String[256][];
static private final String[] A_NAMES = new String[]
@@ -184,7 +186,7 @@ public class HtmlResponseWriterImpl extends ResponseWriter
private String _startElementName;
private boolean _isInsideScript = false;
private boolean _isStyle = false;
- private Boolean _isTextArea;
+ private boolean _isTextArea = false;
private UIComponent _startElementUIComponent;
private boolean _startTagOpen;
private Map<String, Object> _passThroughAttributesMap;
@@ -368,21 +370,23 @@ public void startElement(String name, UIComponent uiComponent) throws IOExceptio
_startedElementsCount.set(i, _startedElementsCount.get(i)+1);
}
- // Each time we start a element, it is necessary to check <script> or <style>,
- // because we need to buffer all content to post process it later when it reach its end
- // according to the initialization properties used.
- if(isScript(_startElementName))
+ // Each time we start an element, check for <script>, <style>, or <textarea>
+ // so that writeText() can make the right encoding choice without lazy detection.
+ if (isScript(_startElementName))
{
- // handle a <script> start
_isInsideScript = true;
_isStyle = false;
- _isTextArea = Boolean.FALSE;
+ _isTextArea = false;
}
else if (isStyle(_startElementName))
{
_isInsideScript = false;
_isStyle = true;
- _isTextArea = Boolean.FALSE;
+ _isTextArea = false;
+ }
+ else
+ {
+ _isTextArea = HTML.TEXTAREA_ELEM.equalsIgnoreCase(_startElementName);
}
}
@@ -497,7 +501,7 @@ private void resetStartedElement()
_startElementUIComponent = null;
_passThroughAttributesMap = null;
_isStyle = false;
- _isTextArea = null;
+ _isTextArea = false;
}
@Override
@@ -509,9 +513,10 @@ public void endElement(String name) throws IOException
if (!_startedElementsCount.isEmpty())
{
- int i = _startedElementsCount.size()-1;
- _startedElementsCount.set(i, _startedElementsCount.get(i)-1);
- if (_startedElementsCount.get(i) == 0)
+ int i = _startedElementsCount.size() - 1;
+ int count = _startedElementsCount.get(i) - 1;
+ _startedElementsCount.set(i, count);
+ if (count == 0)
{
elementName = _startedChangedElements.get(i);
_startedChangedElements.remove(i);
@@ -584,9 +589,9 @@ else if (isStyle() && _isXhtmlContentType)
private void writeStyleContent() throws IOException
{
- String content = getInternalBuffer().toString();
-
- if(_isXhtmlContentType)
+ StreamCharBuffer buffer = getInternalBuffer();
+
+ if (_isXhtmlContentType)
{
// In xhtml, the content inside <style> tag is PCDATA, but
// in html the content is CDATA, so in order to preserve
@@ -594,7 +599,9 @@ private void writeStyleContent() throws IOException
// CDATA tags.
// Since the response content type is xhtml, we can use
// simple CDATA without comments, but note we need to check
- // when we are using any valid notation (simple CDATA, commented CDATA, xml comment)
+ // when we are using any valid notation (simple CDATA, commented CDATA, xml comment)
+ // toString() is only called here because we need trim()/startsWith() inspection.
+ String content = buffer.toString();
String trimmedContent = content.trim();
if (trimmedContent.startsWith(CommentUtils.CDATA_SIMPLE_START) && trimmedContent.endsWith(
CommentUtils.CDATA_SIMPLE_END))
@@ -620,27 +627,28 @@ else if (trimmedContent.startsWith(CommentUtils.COMMENT_SIMPLE_START) &&
else
{
_outputWriter.write(CDATA_START);
- _outputWriter.write(content);
+ buffer.writeTo(_outputWriter);
_outputWriter.write(CDATA_END);
return;
}
}
- // If the response is handled as text/html,
- // it is not necessary to wrap with xml comment tag,
- // so we can just write the content as is.
- _outputWriter.write(content);
+ // If the response is handled as text/html,
+ // it is not necessary to wrap with xml comment tag, so write the buffer directly
+ // without materialising it to a String first.
+ buffer.writeTo(_outputWriter);
}
private void writeScriptContent() throws IOException
{
- String content = getInternalBuffer().toString();
- String trimmedContent = null;
-
- if(_isXhtmlContentType)
+ StreamCharBuffer buffer = getInternalBuffer();
+
+ if (_isXhtmlContentType)
{
- trimmedContent = content.trim();
+ // toString() needed here for trim()/startsWith() inspection
+ String content = buffer.toString();
+ String trimmedContent = content.trim();
- if ( trimmedContent.startsWith(CommentUtils.COMMENT_SIMPLE_START) &&
+ if (trimmedContent.startsWith(CommentUtils.COMMENT_SIMPLE_START) &&
CommentUtils.isEndMatchtWithInlineCommentedXmlCommentTag(trimmedContent))
{
// In xhtml use xml comment to wrap is invalid, so it is only required to remove the <!--
@@ -692,7 +700,7 @@ else if (CommentUtils.isStartMatchWithInlineCommentedCDATA(trimmedContent) &&
_outputWriter.write(CDATA_COMMENT_START);
}
- _outputWriter.write(content);
+ buffer.writeTo(_outputWriter);
if (_cdataOpen)
{
@@ -706,42 +714,42 @@ else if (CommentUtils.isStartMatchWithInlineCommentedCDATA(trimmedContent) &&
return;
}
}
- else
+ else if (_wrapScriptContentWithXmlCommentTag)
{
- if (_wrapScriptContentWithXmlCommentTag)
+ // toString() needed here for trim()/startsWith() inspection
+ String content = buffer.toString();
+ String trimmedContent = content.trim();
+
+ if (trimmedContent.startsWith(CommentUtils.COMMENT_SIMPLE_START) &&
+ CommentUtils.isEndMatchtWithInlineCommentedXmlCommentTag(trimmedContent))
{
- trimmedContent = content.trim();
-
- if ( trimmedContent.startsWith(CommentUtils.COMMENT_SIMPLE_START) &&
- CommentUtils.isEndMatchtWithInlineCommentedXmlCommentTag(trimmedContent))
- {
- _outputWriter.write(content);
- return;
- }
- else if (CommentUtils.isStartMatchWithCommentedCDATA(trimmedContent) &&
- CommentUtils.isEndMatchWithCommentedCDATA(trimmedContent))
- {
- _outputWriter.write(content);
- return;
- }
- else if (CommentUtils.isStartMatchWithInlineCommentedCDATA(trimmedContent) &&
- CommentUtils.isEndMatchWithInlineCommentedCDATA(trimmedContent))
- {
- _outputWriter.write(content);
- return;
- }
- else
- {
- _outputWriter.write(COMMENT_START);
- _outputWriter.write(content);
- _outputWriter.write(COMMENT_COMMENT_END);
- return;
- }
+ _outputWriter.write(content);
+ return;
+ }
+ else if (CommentUtils.isStartMatchWithCommentedCDATA(trimmedContent) &&
+ CommentUtils.isEndMatchWithCommentedCDATA(trimmedContent))
+ {
+ _outputWriter.write(content);
+ return;
+ }
+ else if (CommentUtils.isStartMatchWithInlineCommentedCDATA(trimmedContent) &&
+ CommentUtils.isEndMatchWithInlineCommentedCDATA(trimmedContent))
+ {
+ _outputWriter.write(content);
+ return;
+ }
+ else
+ {
+ _outputWriter.write(COMMENT_START);
+ buffer.writeTo(_outputWriter);
+ _outputWriter.write(COMMENT_COMMENT_END);
+ return;
}
}
-
- //If no return, just write everything
- _outputWriter.write(content);
+
+ // Common path: plain HTML without script wrapping — write buffer directly,
+ // avoiding a full String materialisation of the script content.
+ buffer.writeTo(_outputWriter);
}
@@ -853,7 +861,7 @@ private void encodeAndWriteURIAttribute(String name, Object value) throws IOExce
_currentWriter.write(' ');
_currentWriter.write(name);
_currentWriter.write("=\"");
- if (strValue.toLowerCase().startsWith("javascript:"))
+ if (strValue.regionMatches(true, 0, JAVASCRIPT_SCHEME, 0, JAVASCRIPT_SCHEME.length()))
{
HTMLEncoder.encode(_currentWriter, strValue, false, false, !_isUTF8);
}
@@ -882,7 +890,7 @@ public void writeText(Object value, String componentPropertyName) throws IOExcep
closeStartTagIfNecessary();
- String strValue = value.toString();
+ String strValue = (value instanceof String s) ? s : value.toString();
if (isScriptOrStyle())
{
@@ -916,15 +924,14 @@ public void writeText(char[] cbuf, int off, int len) throws IOException
if (isScriptOrStyle())
{
- String strValue = new String(cbuf, off, len);
// Don't bother encoding anything if chosen character encoding is UTF-8
if (_isUTF8)
{
- _currentWriter.write(strValue);
+ _currentWriter.write(cbuf, off, len);
}
else
{
- UnicodeEncoder.encode(_currentWriter, strValue);
+ UnicodeEncoder.encode(_currentWriter, cbuf, off, len);
}
}
else if (isTextarea())
@@ -971,27 +978,7 @@ private boolean isStyle()
private boolean isTextarea()
{
- initializeStartedTagInfo();
-
- return _isTextArea != null && _isTextArea;
- }
-
- private void initializeStartedTagInfo()
- {
- if (_startElementName != null)
- {
- if (_isTextArea == null)
- {
- if (_startElementName.equalsIgnoreCase(HTML.TEXTAREA_ELEM))
- {
- _isTextArea = Boolean.TRUE;
- }
- else
- {
- _isTextArea = Boolean.FALSE;
- }
- }
- }
+ return _isTextArea;
}
@Override
diff --git a/impl/src/test/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImplTest.java b/impl/src/test/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImplTest.java
index 1128d55ff..3133df1de 100644
--- a/impl/src/test/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImplTest.java
+++ b/impl/src/test/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImplTest.java
@@ -291,6 +291,93 @@ public void testStyleOnXhtmlUTF8Encoding() throws IOException
Assertions.assertTrue(output.contains(CommentUtils.CDATA_SIMPLE_END), "script does not have end ]]> ");
}
+ /**
+ * writeText(char[]) inside a textarea must NOT map successive spaces to &nbsp;
+ * or newlines to <br/> — this is the key purpose of the _isTextArea flag.
+ */
+ @Test
+ public void testTextareaWriteTextPreservesSpacesAndNewlines() throws IOException
+ {
+ _writer.startDocument();
+ _writer.startElement("form", null);
+ _writer.startElement(HTML.TEXTAREA_ELEM, null);
+ _writer.writeText("hello world\nfoo".toCharArray(), 0, 17);
+ _writer.endElement(HTML.TEXTAREA_ELEM);
+ _writer.endElement("form");
+ _writer.endDocument();
+
+ String output = _stringWriter.toString();
+ // Spaces must not be turned into inside a textarea
+ Assertions.assertFalse(output.contains(" "),
+ "textarea: spaces must not be encoded as ");
+ // Newlines must not be turned into <br/> inside a textarea
+ Assertions.assertFalse(output.contains("<br"),
+ "textarea: newlines must not be encoded as <br/>");
+ // The original characters must appear verbatim
+ Assertions.assertTrue(output.contains("hello world"),
+ "textarea: original spaces must be present in output");
+ }
+
+ /**
+ * Same as testTextareaWriteTextPreservesSpacesAndNewlines but with uppercase element name
+ * to verify case-insensitive textarea detection.
+ */
+ @Test
+ public void testTextareaWriteTextUppercase() throws IOException
+ {
+ _writer.startDocument();
+ _writer.startElement("form", null);
+ _writer.startElement("TEXTAREA", null);
+ _writer.writeText("hello world".toCharArray(), 0, 13);
+ _writer.endElement("TEXTAREA");
+ _writer.endElement("form");
+ _writer.endDocument();
+
+ String output = _stringWriter.toString();
+ Assertions.assertFalse(output.contains(" "),
+ "TEXTAREA (uppercase): spaces must not be encoded as ");
+ }
+
+ /**
+ * writeText(Object) with a non-String value must produce the same output as
+ * writeText(Object) with the equivalent String — exercises the toString() fallback path.
+ */
+ @Test
+ public void testWriteTextObjectNonString() throws IOException
+ {
+ _writer.startDocument();
+ _writer.startElement("body", null);
+ _writer.writeText(Integer.valueOf(42), null);
+ _writer.endElement("body");
+ _writer.endDocument();
+
+ Assertions.assertTrue(_stringWriter.toString().contains("42"),
+ "Non-String value (Integer 42) must be rendered as its toString()");
+ }
+
+ /**
+ * Style content on plain text/html must be written verbatim without toString() overhead
+ * (exercises the StreamCharBuffer.writeTo path in writeStyleContent).
+ */
+ @Test
+ public void testStyleOnHtmlWritesContentVerbatim() throws IOException
+ {
+ _writer = new HtmlResponseWriterImpl(_stringWriter, "text/html", "UTF-8", false);
+ String css = "body { color: red; }";
+ _writer.startDocument();
+ _writer.startElement(HTML.STYLE_ELEM, null);
+ _writer.write(css);
+ _writer.endElement(HTML.STYLE_ELEM);
+ _writer.endDocument();
+
+ String output = _stringWriter.toString();
+ Assertions.assertTrue(output.contains(css),
+ "Plain HTML style content must appear verbatim in output");
+ // In plain text/html there is no CDATA wrapping
+ Assertions.assertFalse(output.contains(CommentUtils.CDATA_SIMPLE_START),
+ "Plain HTML style must not be wrapped in CDATA");
+ }
+
/**
* In html, it is not valid to have an empty tag with content
*