dev@javaserverfaces.java.net

Re: [REVIEW] Cleanup of some identified hotspots

From: Ryan Lubke <Ryan.Lubke_at_Sun.COM>
Date: Tue, 31 Oct 2006 11:20:04 -0800

Michael Youngstrom wrote:
> r=youngm
>
> However could:
>
> +
> + if (isXhtml) {
> + String prefixName = attrName;
> + if (Arrays.binarySearch(XHTML_PREFIX_ATTRIBUTES,
> + attrName) > -1) {
> + prefixName = XHTML_ATTR_PREFIX + attrName;
> }
> + writer.writeAttribute(prefixName, value, attrName);
> + } else {
> + writer.writeAttribute(attrName, value, attrName);
> }
>
> be replaced with:
>
> + writer.writeAttribute(prefixAttribute(attrName, writer), value,
> attrName)
>
> Otherwise looks fine.
I've added prefixAttribute(String, boolean) and then modified the code
you pointed out to:

   writer.writeAttribute(prefixAttribute(attrName, isXhtml), value,
attrName)

This avoids calling .equals() in a loop when the value isn't going to
change for
that invocation.

>
> Mike
>
> On 10/31/06, Ryan Lubke <Ryan.Lubke_at_sun.com> wrote:
>>
>>
>>
>> Clean up some jprofiler hotspots
>>
>>
>> SECTION: Modified Files
>> ----------------------------
>> M src/com/sun/faces/io/FastStringWriter.java
>> - call builder.append() instead of calling
>> write(String, int, int) to avoid unecessary
>> substring
>>
>> M src/com/sun/faces/renderkit/RenderKitUtils.java
>> M src/com/sun/faces/renderkit/html_basic/MessageRenderer.java
>> M src/com/sun/faces/renderkit/html_basic/OutputMessageRenderer.java
>> M src/com/sun/faces/renderkit/html_basic/TextRenderer.java
>> - Remove RenderKitUtils.hasPassThroughAttributes()
>> Since hasPassThroughAttributes() could potentially run through
>> the entire list of attributes, and if one is found at the end
>> of the list, we'd end up iterating through the list again
>> at render time. Instead, just call renderPassThroughAttributes
>> and render any we find.
>> - Update the Message, OutputMessage, and TestRenderers to poll
>> the attributes that determine if a span should be rendered
>> directly since it's a small subset of the full list to save
>> a few cycles.
>>
>>
>>
>>
>> SECTION: Diffs
>> ----------------------------
>> Index: src/com/sun/faces/io/FastStringWriter.java
>> ===================================================================
>> RCS file:
>> /cvs/javaserverfaces-sources/jsf-ri/src/com/sun/faces/io/FastStringWriter.java,v
>>
>> retrieving revision 1.4
>> diff -u -r1.4 FastStringWriter.java
>> --- src/com/sun/faces/io/FastStringWriter.java 22 May 2006 17:16:04
>> -0000 1.4
>> +++ src/com/sun/faces/io/FastStringWriter.java 31 Oct 2006 17:31:02
>> -0000
>> @@ -112,7 +112,7 @@
>> * @param str String to be written
>> */
>> public void write(String str) {
>> - write(str, 0, str.length());
>> + builder.append(str);
>> }
>>
>> /**
>> Index: src/com/sun/faces/renderkit/RenderKitUtils.java
>> ===================================================================
>> RCS file:
>> /cvs/javaserverfaces-sources/jsf-ri/src/com/sun/faces/renderkit/RenderKitUtils.java,v
>>
>> retrieving revision 1.33
>> diff -u -r1.33 RenderKitUtils.java
>> --- src/com/sun/faces/renderkit/RenderKitUtils.java 23 Oct 2006
>> 22:04:11 -0000 1.33
>> +++ src/com/sun/faces/renderkit/RenderKitUtils.java 31 Oct 2006
>> 17:31:03 -0000
>> @@ -455,41 +455,51 @@
>>
>> MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "excludes"));
>> }
>>
>> - if (hasPassThruAttributes(component)) {
>> + Map<String, Object> attrMap = component.getAttributes();
>>
>> - Map<String, Object> attrMap = component.getAttributes();
>> -
>> - if (excludes.length > 0) {
>> - Arrays.sort(excludes);
>> + if (excludes.length > 0) {
>> + Arrays.sort(excludes);
>> + }
>> + boolean isXhtml =
>> writer.getContentType().equals(RIConstants.XHTML_CONTENT_TYPE);
>> + for (String attrName : PASSTHROUGH_ATTRIBUTES) {
>> + if (excludes.length > 0
>> + && Arrays.binarySearch(excludes, attrName) > -1) {
>> + continue;
>> }
>> - for (String attrName : PASSTHROUGH_ATTRIBUTES) {
>> - if (excludes.length > 0
>> - && Arrays.binarySearch(excludes, attrName) > -1) {
>> - continue;
>> + Object value =
>> + attrMap.get(attrName);
>> + if (value != null && shouldRenderAttribute(value)) {
>> + if (context == null) {
>> + context = FacesContext.getCurrentInstance();
>> }
>> - Object value =
>> - attrMap.get(attrName);
>> - if (value != null && shouldRenderAttribute(value)) {
>> - if (context == null) {
>> - context = FacesContext.getCurrentInstance();
>> - }
>> -
>> - boolean isXhtml =
>> writer.getContentType().equals(RIConstants.XHTML_CONTENT_TYPE);
>> - if (isXhtml) {
>> - String prefixName = attrName;
>> - if
>> (Arrays.binarySearch(XHTML_PREFIX_ATTRIBUTES,
>> - attrName) > -1) {
>> - prefixName = XHTML_ATTR_PREFIX + attrName;
>> - }
>> - writer.writeAttribute(prefixName, value,
>> attrName);
>> - } else {
>> - writer.writeAttribute(attrName, value,
>> attrName);
>> +
>> + if (isXhtml) {
>> + String prefixName = attrName;
>> + if (Arrays.binarySearch(XHTML_PREFIX_ATTRIBUTES,
>> + attrName) > -1) {
>> + prefixName = XHTML_ATTR_PREFIX + attrName;
>> }
>> + writer.writeAttribute(prefixName, value, attrName);
>> + } else {
>> + writer.writeAttribute(attrName, value, attrName);
>> }
>> -
>> }
>> +
>> }
>> + }
>> +
>>
>> + public static String prefixAttribute(final String attrName,
>> + final ResponseWriter writer) {
>> + if
>> (RIConstants.XHTML_CONTENT_TYPE.equals(writer.getContentType())) {
>> + if (Arrays.binarySearch(XHTML_PREFIX_ATTRIBUTES,
>> attrName) > -1) {
>> + return XHTML_ATTR_PREFIX + attrName;
>> + } else {
>> + return attrName;
>> + }
>> + } else {
>> + return attrName;
>> + }
>> }
>>
>>
>> Index: src/com/sun/faces/renderkit/html_basic/MessageRenderer.java
>> ===================================================================
>> RCS file:
>> /cvs/javaserverfaces-sources/jsf-ri/src/com/sun/faces/renderkit/html_basic/MessageRenderer.java,v
>>
>> retrieving revision 1.65
>> diff -u -r1.65 MessageRenderer.java
>> --- src/com/sun/faces/renderkit/html_basic/MessageRenderer.java 9 Oct
>> 2006 17:28:30 -0000 1.65
>> +++ src/com/sun/faces/renderkit/html_basic/MessageRenderer.java 31
>> Oct 2006 17:31:03 -0000
>> @@ -209,9 +209,11 @@
>> component.getAttributes().get("fatalClass");
>> }
>>
>> - String
>> - style = (String) component.getAttributes().get("style"),
>> - styleClass = (String)
>> component.getAttributes().get("styleClass");
>> + String style = (String) component.getAttributes().get("style");
>> + String styleClass = (String)
>> component.getAttributes().get("styleClass");
>> + String dir = (String) component.getAttributes().get("dir");
>> + String lang = (String) component.getAttributes().get("lang");
>> + String title = (String) component.getAttributes().get("title");
>>
>> // if we have style and severityStyle
>> if ((style != null) && (severityStyle != null)) {
>> @@ -238,9 +240,12 @@
>> //Done intializing local variables. Move on to rendering.
>>
>> boolean wroteSpan = false;
>> - if (styleClass != null || style != null ||
>> - shouldWriteIdAttribute(component) ||
>> - RenderKitUtils.hasPassThruAttributes(component)) {
>> + if (styleClass != null
>> + || style != null
>> + || dir != null
>> + || lang != null
>> + || title != null
>> + || shouldWriteIdAttribute(component)) {
>> writer.startElement("span", component);
>> writeIdAttributeIfNecessary(context, writer, component);
>>
>> @@ -251,11 +256,15 @@
>> if (styleClass != null) {
>> writer.writeAttribute("class", styleClass,
>> "styleClass");
>> }
>> - // style is rendered as a passthru attribute
>> - RenderKitUtils.renderPassThruAttributes(context,
>> - writer,
>> - component,
>> - new String[]
>> {"style"});
>> + if (dir != null) {
>> + writer.writeAttribute("dir", dir, "dir");
>> + }
>> + if (lang != null) {
>> +
>> writer.writeAttribute(RenderKitUtils.prefixAttribute(lang, writer),
>> + lang,
>> + "lang");
>> + }
>> +
>> }
>>
>> Object tooltip = component.getAttributes().get("tooltip");
>> @@ -270,8 +279,7 @@
>>
>> if (!wroteSpan) {
>> writer.startElement("span", component);
>> - }
>> - String title = (String)
>> component.getAttributes().get("title");
>> + }
>> if (title == null || title.length() == 0) {
>> writer.writeAttribute("title", summary, "title");
>> }
>> Index: src/com/sun/faces/renderkit/html_basic/OutputMessageRenderer.java
>> ===================================================================
>> RCS file:
>> /cvs/javaserverfaces-sources/jsf-ri/src/com/sun/faces/renderkit/html_basic/OutputMessageRenderer.java,v
>>
>> retrieving revision 1.28
>> diff -u -r1.28 OutputMessageRenderer.java
>> ---
>> src/com/sun/faces/renderkit/html_basic/OutputMessageRenderer.java 1
>> Sep 2006 17:30:54 -0000 1.28
>> +++
>> src/com/sun/faces/renderkit/html_basic/OutputMessageRenderer.java
>> 31 Oct 2006 17:31:03 -0000
>> @@ -89,10 +89,12 @@
>> "Begin encoding component " +
>> component.getId());
>> }
>>
>> - String
>> - currentValue = null,
>> - style = (String) component.getAttributes().get("style"),
>> - styleClass = (String)
>> component.getAttributes().get("styleClass");
>> + String currentValue = null;
>> + String style = (String) component.getAttributes().get("style");
>> + String styleClass = (String)
>> component.getAttributes().get("styleClass");
>> + String lang = (String) component.getAttributes().get("lang");
>> + String dir = (String) component.getAttributes().get("dir");
>> + String title = (String) component.getAttributes().get("title");
>>
>> ResponseWriter writer = context.getResponseWriter();
>> assert(writer != null);
>> @@ -147,18 +149,33 @@
>> }
>>
>> boolean wroteSpan = false;
>> - if (null != styleClass || null != style ||
>> - RenderKitUtils.hasPassThruAttributes(component) ||
>> - shouldWriteIdAttribute(component)) {
>> + if (styleClass != null
>> + || style != null
>> + || dir != null
>> + || lang != null
>> + || title != null
>> + || shouldWriteIdAttribute(component)) {
>> writer.startElement("span", component);
>> writeIdAttributeIfNecessary(context, writer, component);
>> wroteSpan = true;
>>
>> + if (style != null) {
>> + writer.writeAttribute("style", style, "style");
>> + }
>> if (null != styleClass) {
>> writer.writeAttribute("class", styleClass,
>> "styleClass");
>> }
>> - // style is rendered as a passthru attribute
>> - RenderKitUtils.renderPassThruAttributes(context, writer,
>> component);
>> + if (dir != null) {
>> + writer.writeAttribute("dir", dir, "dir");
>> + }
>> + if (lang != null) {
>> +
>> writer.writeAttribute(RenderKitUtils.prefixAttribute(lang, writer),
>> + lang,
>> + "lang");
>> + }
>> + if (title != null) {
>> + writer.writeAttribute("title", title, "title");
>> + }
>> }
>> Boolean escape = Boolean.TRUE;
>> Object val = component.getAttributes().get("escape");
>> Index: src/com/sun/faces/renderkit/html_basic/TextRenderer.java
>> ===================================================================
>> RCS file:
>> /cvs/javaserverfaces-sources/jsf-ri/src/com/sun/faces/renderkit/html_basic/TextRenderer.java,v
>>
>> retrieving revision 1.77
>> diff -u -r1.77 TextRenderer.java
>> --- src/com/sun/faces/renderkit/html_basic/TextRenderer.java 1 Sep
>> 2006 17:30:53 -0000 1.77
>> +++ src/com/sun/faces/renderkit/html_basic/TextRenderer.java 31
>> Oct 2006 17:31:04 -0000
>> @@ -82,9 +82,12 @@
>> shouldWriteIdAttribute = false,
>> isOutput = false;
>>
>> - String
>> - style = (String) component.getAttributes().get("style"),
>> - styleClass = (String)
>> component.getAttributes().get("styleClass");
>> + String style = (String) component.getAttributes().get("style");
>> + String styleClass = (String)
>> component.getAttributes().get("styleClass");
>> + String dir = (String) component.getAttributes().get("dir");
>> + String lang = (String) component.getAttributes().get("lang");
>> + String title = (String) component.getAttributes().get("title");
>> +
>> if (component instanceof UIInput) {
>> writer.startElement("input", component);
>> writeIdAttributeIfNecessary(context, writer, component);
>> @@ -120,18 +123,32 @@
>> writer.endElement("input");
>>
>> } else if (isOutput = (component instanceof UIOutput)) {
>> - if (null != styleClass || null != style ||
>> - RenderKitUtils.hasPassThruAttributes(component) ||
>> - (shouldWriteIdAttribute =
>> shouldWriteIdAttribute(component))) {
>> + shouldWriteIdAttribute = shouldWriteIdAttribute(component);
>> + if (styleClass != null
>> + || style != null
>> + || dir != null
>> + || lang != null
>> + || title != null
>> + || shouldWriteIdAttribute) {
>> writer.startElement("span", component);
>> writeIdAttributeIfNecessary(context, writer,
>> component);
>> + if (style != null) {
>> + writer.writeAttribute("style", style, "style");
>> + }
>> if (null != styleClass) {
>> writer.writeAttribute("class", styleClass,
>> "styleClass");
>> }
>> - // style is rendered as a passthru attribute
>> - RenderKitUtils
>> - .renderPassThruAttributes(context, writer,
>> component);
>> -
>> + if (dir != null) {
>> + writer.writeAttribute("dir", dir, "dir");
>> + }
>> + if (lang != null) {
>> +
>> writer.writeAttribute(RenderKitUtils.prefixAttribute(lang, writer),
>> + lang,
>> + "lang");
>> + }
>> + if (title != null) {
>> + writer.writeAttribute("title", title, "title");
>> + }
>> }
>> if (currentValue != null) {
>> Object val = null;
>> @@ -154,9 +171,12 @@
>> }
>> }
>> }
>> - if (isOutput && (null != styleClass || null != style ||
>> -
>> RenderKitUtils.hasPassThruAttributes(component) ||
>> - shouldWriteIdAttribute)) {
>> + if (isOutput && (styleClass != null
>> + || style != null
>> + || dir != null
>> + || lang != null
>> + || title != null
>> + || shouldWriteIdAttribute)) {
>> writer.endElement("span");
>> }
>>
>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe_at_javaserverfaces.dev.java.net
>> For additional commands, e-mail: dev-help_at_javaserverfaces.dev.java.net
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_javaserverfaces.dev.java.net
> For additional commands, e-mail: dev-help_at_javaserverfaces.dev.java.net
>