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 17:05:18 UTC

(myfaces) 03/05: [perf] avoid new String creation

This is an automated email from the ASF dual-hosted git repository.

tandraschko pushed a commit to branch 4.1.x
in repository https://gitbox.apache.org/repos/asf/myfaces.git

commit 5952afa90ef535f7d444243c68077b263c1f6ed5
Author: tandraschko <ta...@apache.org>
AuthorDate: Wed Jun 3 18:48:42 2026 +0200

    [perf] avoid new String creation
---
 .../renderkit/html/HtmlResponseWriterImpl.java     | 179 ++++++++++-----------
 .../renderkit/html/HtmlResponseWriterImplTest.java |  87 ++++++++++
 2 files changed, 170 insertions(+), 96 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 798e3b477..85d7203f2 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
@@ -35,6 +35,7 @@
 import jakarta.faces.render.Renderer;
 
 import org.apache.myfaces.config.webparameters.MyfacesConfig;
+import org.apache.myfaces.renderkit.html.util.CommonHtmlEventsUtil;
 import org.apache.myfaces.core.api.shared.ComponentUtils;
 import org.apache.myfaces.renderkit.ContentTypeUtils;
 import org.apache.myfaces.renderkit.html.util.UnicodeEncoder;
@@ -65,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[]
@@ -183,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;
@@ -306,6 +309,7 @@ public void startDocument()
     public void endDocument() throws IOException
     {
         FacesContext facesContext = getFacesContext();
+        CommonHtmlEventsUtil.flushDeferredCspBehaviorScripts(facesContext, this);
         MyfacesConfig myfacesConfig = MyfacesConfig.getCurrentInstance(facesContext);
         if (myfacesConfig.isEarlyFlushEnabled())
         {
@@ -336,9 +340,9 @@ public void startElement(String name, UIComponent uiComponent) throws IOExceptio
             Object value = _passThroughAttributesMap.get(Renderer.PASSTHROUGH_RENDERER_LOCALNAME_KEY);
             if (value != null)
             {
-                if (value instanceof ValueExpression)
+                if (value instanceof ValueExpression expression)
                 {
-                    value = ((ValueExpression)value).getValue(getFacesContext().getELContext());
+                    value = expression.getValue(getFacesContext().getELContext());
                 }
                 String elementName = value.toString().trim();
                 
@@ -366,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);
         }
     }
 
@@ -421,9 +427,9 @@ private void closeStartTagIfNecessary() throws IOException
                     }
                     
                     Object value = entry.getValue();
-                    if (value instanceof ValueExpression)
+                    if (value instanceof ValueExpression expression)
                     {
-                        value = ((ValueExpression)value).getValue(getFacesContext().getELContext());
+                        value = expression.getValue(getFacesContext().getELContext());
                     }
                     // encodeAndWriteURIAttribute(key, value, key);
                     // Faces 2.2 In the renderkit javadoc of jsf 2.2 spec says this 
@@ -437,7 +443,7 @@ private void closeStartTagIfNecessary() throws IOException
                     // Note here it says "as if they were passed", instead say "... attributes are
                     // encoded and rendered as if ...". Black box testing against RI shows that there
                     // is no URI encoding at all in this part, so in this case the best is do the
-                    // same here. After all, it is resposibility of the one who set the passthrough
+                    // same here. After all, it is responsibility of the one who set the passthrough
                     // attribute to do the proper encoding in cases when a URI is provided. However,
                     // that does not means the attribute should not be encoded as other attributes.
                     encodeAndWriteAttribute(key, value);
@@ -495,7 +501,7 @@ private void resetStartedElement()
         _startElementUIComponent = null;
         _passThroughAttributesMap = null;
         _isStyle = false;
-        _isTextArea = null;
+        _isTextArea = false;
     }
 
     @Override
@@ -507,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);
@@ -582,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 
@@ -592,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))
@@ -618,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 <!--
@@ -690,7 +700,7 @@ else if (CommentUtils.isStartMatchWithInlineCommentedCDATA(trimmedContent) &&
                    _outputWriter.write(CDATA_COMMENT_START);
                 }
 
-                _outputWriter.write(content);
+                buffer.writeTo(_outputWriter);
 
                 if (_cdataOpen)
                 {
@@ -704,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);
     }
     
 
@@ -780,18 +790,17 @@ public void writeAttribute(String name, Object value, String componentPropertyNa
         // Fast path: the overwhelming majority of attribute values are already Strings,
         // so handle them first and skip both the Boolean instanceof check and the
         // (no-op for String) toString() virtual dispatch.
-        if (value instanceof String)
+        if (value instanceof String strValue)
         {
-            String strValue = (String) value;
             _currentWriter.write(' ');
             _currentWriter.write(name);
             _currentWriter.write("=\"");
             HTMLEncoder.encode(_currentWriter, strValue, false, false, !_isUTF8);
             _currentWriter.write('"');
         }
-        else if (value instanceof Boolean)
+        else if (value instanceof Boolean boolean1)
         {
-            if (((Boolean) value).booleanValue())
+            if (boolean1.booleanValue())
             {
                 // name as value for XHTML compatibility
                 _currentWriter.write(' ');
@@ -852,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);
         }
@@ -881,8 +890,7 @@ public void writeText(Object value, String componentPropertyName) throws IOExcep
 
         closeStartTagIfNecessary();
 
-        // Avoid a virtual dispatch when the value is already a String (the common case).
-        String strValue = value instanceof String ? (String) value : 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 27103ac9c..821a223f1 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 &amp;nbsp;
+     * or newlines to &lt;br/&gt; — 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 &nbsp; inside a textarea
+        Assertions.assertFalse(output.contains("&nbsp;"),
+                "textarea: spaces must not be encoded as &nbsp;");
+        // 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("&nbsp;"),
+                "TEXTAREA (uppercase): spaces must not be encoded as &nbsp;");
+    }
+
+    /**
+     * 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
      *