[
https://issues.apache.org/jira/browse/MYFACES-4244?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16570612#comment-16570612
]
ASF GitHub Bot commented on MYFACES-4244:
-----------------------------------------
pnicolucci closed pull request #10: MYFACES-4244:Use StringBuilder rather than
calling write multiple times
URL: https://github.com/apache/myfaces/pull/10
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/impl/src/main/java/org/apache/myfaces/util/CDataEndEscapeFilterWriter.java
b/impl/src/main/java/org/apache/myfaces/util/CDataEndEscapeFilterWriter.java
index 155d17882..63c3cc55c 100644
--- a/impl/src/main/java/org/apache/myfaces/util/CDataEndEscapeFilterWriter.java
+++ b/impl/src/main/java/org/apache/myfaces/util/CDataEndEscapeFilterWriter.java
@@ -59,22 +59,35 @@ public void write(int c) throws IOException
public void write(char[] cbuf, int off, int len) throws IOException
{
int index = off;
+ StringBuilder sb = null;
for (int i = 0; i < len; i++)
{
char c = cbuf[off+i];
if (c1 == ']' && c2 == ']' && c == '>')
{
- super.write(cbuf, index, i+1 - ( index - off ) );
+ if (sb == null)
+ {
+ sb = new StringBuilder(len + 16);
+ }
+ sb.append(cbuf, index, i+1 - ( index - off ));
index = off+i+1;
- out.write("<![CDATA[]]]]><![CDATA[>");
+ sb.append("<![CDATA[]]]]><![CDATA[>");
}
c1 = c2;
- c2 = (char) cbuf[off+i];
+ c2 = c;
pos++;
}
- if (index < off+len)
+ if (sb != null)
+ {
+ if (index < off+len)
+ {
+ sb.append(cbuf, index, off+len-index);
+ }
+ out.write(sb.toString());
+ }
+ else
{
- super.write(cbuf, index, off+len-index);
+ out.write(cbuf, off, len);
}
}
@@ -82,22 +95,35 @@ public void write(char[] cbuf, int off, int len) throws
IOException
public void write(String str, int off, int len) throws IOException
{
int index = off;
+ StringBuilder sb = null;
for (int i = 0; i < len; i++)
{
char c = str.charAt(off+i);
if (c1 == ']' && c2 == ']' && c == '>')
{
- super.write(str, index, i+1 - ( index - off ) );
+ if (sb == null)
+ {
+ sb = new StringBuilder(len + 16);
+ }
+ sb.append(str, index, off+i+1);
index = off+i+1;
- out.write("<![CDATA[]]]]><![CDATA[>");
+ sb.append("<![CDATA[]]]]><![CDATA[>");
}
c1 = c2;
- c2 = (char) str.charAt(off+i);
+ c2 = c;
pos++;
}
- if (index < off+len)
+ if (sb != null)
+ {
+ if (index < off+len)
+ {
+ sb.append(str, index, off+len);
+ }
+ out.write(sb.toString());
+ }
+ else
{
- super.write(str, index, off+len-index);
+ out.write(str, off, len);
}
}
}
diff --git
a/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java
b/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java
index e60eabd32..02cff0f19 100644
---
a/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java
+++
b/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java
@@ -320,7 +320,6 @@ public void startElement(String name, UIComponent
uiComponent) throws IOExceptio
}
closeStartTagIfNecessary();
- _currentWriter.write('<');
resetStartedElement();
@@ -330,6 +329,8 @@ public void startElement(String name, UIComponent
uiComponent) throws IOExceptio
_passThroughAttributesMap = (_startElementUIComponent != null) ?
_startElementUIComponent.getPassThroughAttributes(false) : null;
+ String startElementNameToWrite = name;
+
if (_passThroughAttributesMap != null)
{
Object value = _passThroughAttributesMap.get(
@@ -349,17 +350,15 @@ public void startElement(String name, UIComponent
uiComponent) throws IOExceptio
_startedChangedElements.add(elementName);
_startedElementsCount.add(0);
}
- _currentWriter.write((String) elementName);
- }
- else
- {
- _currentWriter.write(name);
+ startElementNameToWrite = elementName;
}
}
- else
- {
- _currentWriter.write(name);
- }
+
+ StringBuilder sb = new StringBuilder(startElementNameToWrite.length()
+ 1);
+ sb.append('<');
+ sb.append(startElementNameToWrite);
+
+ _currentWriter.write(sb.toString());
if (!_startedElementsCount.isEmpty())
{
@@ -805,9 +804,11 @@ else if (isStyle(name))
_isStyle = false;
}
- _currentWriter.write("</");
- _currentWriter.write(name);
- _currentWriter.write('>');
+ StringBuilder sb = new StringBuilder(name.length() + 3);
+ sb.append("</");
+ sb.append(name);
+ sb.append('>');
+ _currentWriter.write(sb.toString());
}
public void writeAttribute(String name, Object value, String
componentPropertyName) throws IOException
@@ -833,34 +834,42 @@ public void writeAttribute(String name, Object value,
String componentPropertyNa
if (((Boolean)value).booleanValue())
{
// name as value for XHTML compatibility
- _currentWriter.write(' ');
- _currentWriter.write(name);
- _currentWriter.write("=\"");
- _currentWriter.write(name);
- _currentWriter.write('"');
+ StringBuilder sb = new StringBuilder(name.length() * 2 + 4);
+ sb.append(' ');
+ sb.append(name);
+ sb.append("=\"");
+ sb.append(name);
+ sb.append('"');
+ _currentWriter.write(sb.toString());
}
}
else
{
String strValue = (value==null)?"":value.toString();
- _currentWriter.write(' ');
- _currentWriter.write(name);
- _currentWriter.write("=\"");
-
org.apache.myfaces.shared.renderkit.html.util.HTMLEncoder.encode(_currentWriter,
+ String encodedStr =
org.apache.myfaces.shared.renderkit.html.util.HTMLEncoder.encode(
strValue, false, false, !_isUTF8);
- _currentWriter.write('"');
+ StringBuilder sb = new StringBuilder(name.length() +
encodedStr.length() + 4);
+ sb.append(' ');
+ sb.append(name);
+ sb.append("=\"");
+ sb.append(encodedStr);
+ sb.append('"');
+ _currentWriter.write(sb.toString());
}
}
private void encodeAndWriteAttribute(String name, Object value) throws
IOException
{
String strValue = (value==null)?"":value.toString();
- _currentWriter.write(' ');
- _currentWriter.write(name);
- _currentWriter.write("=\"");
-
org.apache.myfaces.shared.renderkit.html.util.HTMLEncoder.encode(_currentWriter,
+ String encodedStr =
org.apache.myfaces.shared.renderkit.html.util.HTMLEncoder.encode(
strValue, false, false, !_isUTF8);
- _currentWriter.write('"');
+ StringBuilder sb = new StringBuilder(name.length() +
encodedStr.length() + 4);
+ sb.append(' ');
+ sb.append(name);
+ sb.append("=\"");
+ sb.append(encodedStr);
+ sb.append('"');
+ _currentWriter.write(sb.toString());
}
public void writeURIAttribute(String name, Object value, String
componentPropertyName) throws IOException
@@ -887,12 +896,10 @@ public void writeURIAttribute(String name, Object value,
String componentPropert
private void encodeAndWriteURIAttribute(String name, Object value) throws
IOException
{
String strValue = value.toString();
- _currentWriter.write(' ');
- _currentWriter.write(name);
- _currentWriter.write("=\"");
+ String encodedStr;
if (strValue.toLowerCase().startsWith("javascript:"))
{
-
org.apache.myfaces.shared.renderkit.html.util.HTMLEncoder.encode(_currentWriter,
+ encodedStr =
org.apache.myfaces.shared.renderkit.html.util.HTMLEncoder.encode(
strValue, false, false, !_isUTF8);
}
else
@@ -925,10 +932,16 @@ private void encodeAndWriteURIAttribute(String name,
Object value) throws IOExce
}
*/
//_writer.write(strValue);
-
org.apache.myfaces.shared.renderkit.html.util.HTMLEncoder.encodeURIAttribute(_currentWriter,
+ encodedStr =
org.apache.myfaces.shared.renderkit.html.util.HTMLEncoder.encodeURIAttribute(
strValue, _characterEncoding);
}
- _currentWriter.write('"');
+ StringBuilder sb = new StringBuilder(name.length() +
encodedStr.length() + 4);
+ sb.append(' ');
+ sb.append(name);
+ sb.append("=\"");
+ sb.append(encodedStr);
+ sb.append('"');
+ _currentWriter.write(sb.toString());
}
public void writeComment(Object value) throws IOException
@@ -939,9 +952,12 @@ public void writeComment(Object value) throws IOException
}
closeStartTagIfNecessary();
- _currentWriter.write("<!--");
- _currentWriter.write(value.toString()); //TODO: Escaping: must not
have "-->" inside!
- _currentWriter.write("-->");
+ String strValue = value.toString();
+ StringBuilder sb = new StringBuilder(strValue.length() + 7);
+ sb.append("<!--");
+ sb.append(strValue); //TODO: Escaping: must not have "-->" inside!
+ sb.append("-->");
+ _currentWriter.write(sb.toString());
}
public void writeText(Object value, String componentPropertyName) throws
IOException
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> [perf] Use StringBuilder rather than calling write many times to increase
> performance
> -------------------------------------------------------------------------------------
>
> Key: MYFACES-4244
> URL: https://issues.apache.org/jira/browse/MYFACES-4244
> Project: MyFaces Core
> Issue Type: Improvement
> Affects Versions: 2.2.12, 2.3.1
> Reporter: Paul Nicolucci
> Assignee: Paul Nicolucci
> Priority: Minor
>
> Using StringBuilder.append performs better than calling Writer.write multiple
> times because the path length for a StringBuffer.append is less than the path
> length of a Writer.write operation. By using StringBuilder, you only have to
> call the write method once instead of N number of times per method.
> The Shared StringBuilder may have some issues as well if there is any
> multi-threaded use of the Shared StringBuilder. If multiple threads could
> use a particular StringBuilder you would end up getting incorrect output.
> Another issue with the Shared StringBuilder for performance is it requires a
> call to get it which can be more expensive than just newing up a new
> StringBuilder.
> From looking at the myfaces code, the Shared StringBuilder is used only for
> special cases presently and not widely used.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)