You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by gc...@apache.org on 2008/01/19 01:19:51 UTC

svn commit: r613322 - in /myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main: java-templates/org/apache/myfaces/trinidad/component/ java/org/apache/myfaces/trinidad/component/

Author: gcrawford
Date: Fri Jan 18 16:19:50 2008
New Revision: 613322

URL: http://svn.apache.org/viewvc?rev=613322&view=rev
Log:
Issue 906 optimize getClientId - use per thread shared StringBuilder

In getClientId/getContainerClientId we create a new stringBuilder.

However when creating a client id we only use one stringBuilder at a time, so have a single instance of a StringBuilder saved on thread local.

Have a utility to get this instance, and any time a user gets the instance, set the length of the stringBuilder to 0. 

Modified:
    myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java-templates/org/apache/myfaces/trinidad/component/UIXDecorateCollectionTemplate.java
    myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXCollection.java
    myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXComponentBase.java

Modified: myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java-templates/org/apache/myfaces/trinidad/component/UIXDecorateCollectionTemplate.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java-templates/org/apache/myfaces/trinidad/component/UIXDecorateCollectionTemplate.java?rev=613322&r1=613321&r2=613322&view=diff
==============================================================================
--- myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java-templates/org/apache/myfaces/trinidad/component/UIXDecorateCollectionTemplate.java (original)
+++ myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java-templates/org/apache/myfaces/trinidad/component/UIXDecorateCollectionTemplate.java Fri Jan 18 16:19:50 2008
@@ -6,9 +6,9 @@
  *  to you under the Apache License, Version 2.0 (the
  *  "License"); you may not use this file except in compliance
  *  with the License.  You may obtain a copy of the License at
- * 
+ *
  *  http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  *  Unless required by applicable law or agreed to in writing,
  *  software distributed under the License is distributed on an
  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -27,7 +27,7 @@
  * <p>
  * @version $Name:  $ ($Revision$) $Date$
  */
-public abstract class UIXDecorateCollectionTemplate extends UIXComponentBase 
+public abstract class UIXDecorateCollectionTemplate extends UIXComponentBase
         implements NamingContainer
 {
   /**
@@ -39,13 +39,13 @@
   {
     return _currencyString;
   }
-  
+
   /**
    * Sets the currency String for this decorate collection. The decorator renders
    * aggregated components that are not in the component tree. If any of the aggregated
    * component is a naming container (for e.g. menubar), this method allows the currency to
    * be set to that naming container so that it can successfully decode its children.
-   * 
+   *
    * @param currency the currency to be established
    * @see #getCurrencyString
    */
@@ -53,7 +53,7 @@
   {
     _currencyString = currency;
   }
-  
+
   /**
    * Gets the client-id of this component, without any NamingContainers.
    * This id changes depending on the currency Object.
@@ -70,7 +70,9 @@
     String key = getCurrencyString();
     if (key != null)
     {
-      id += NamingContainer.SEPARATOR_CHAR + key;
+      StringBuilder bld = __getSharedStringBuilder();
+      bld.append(id).append(NamingContainer.SEPARATOR_CHAR).append(key);
+      id = bld.toString();
     }
 
     return id;

Modified: myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXCollection.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXCollection.java?rev=613322&r1=613321&r2=613322&view=diff
==============================================================================
--- myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXCollection.java (original)
+++ myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXCollection.java Fri Jan 18 16:19:50 2008
@@ -669,7 +669,7 @@
     String key = getClientRowKey();
     if (key != null)
     {
-      StringBuilder bld = new StringBuilder(id.length() + 1 + key.length());
+      StringBuilder bld = __getSharedStringBuilder();
       bld.append(id).append(NamingContainer.SEPARATOR_CHAR).append(key);
       id = bld.toString();
     }

Modified: myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXComponentBase.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXComponentBase.java?rev=613322&r1=613321&r2=613322&view=diff
==============================================================================
--- myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXComponentBase.java (original)
+++ myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXComponentBase.java Fri Jan 18 16:19:50 2008
@@ -294,6 +294,7 @@
   // ------------------------------------------------------------- Properties
 
 
+
   @Override
   public String getClientId(FacesContext context)
   {
@@ -318,7 +319,7 @@
       if (containerComponent instanceof NamingContainer)
       {
         String contClientId = containerComponent.getContainerClientId(context);
-        StringBuilder bld = new StringBuilder(contClientId.length() + 1 + clientId.length());
+        StringBuilder bld = __getSharedStringBuilder();
         bld.append(contClientId).append(NamingContainer.SEPARATOR_CHAR).append(clientId);
         clientId = bld.toString();
         break;
@@ -1260,6 +1261,56 @@
     }
   }
 
+
+  /**
+   * <p>
+   * This gets a single threadlocal shared stringbuilder instance, each time you call
+   * __getSharedStringBuilder it sets the length of the stringBuilder instance to 0.
+   * </p><p>
+   * This allows you to use the same StringBuilder instance over and over.
+   * You must call toString on the instance before calling __getSharedStringBuilder again.
+   * </p>
+   * Example that works
+   * <pre><code>
+   * StringBuilder sb1 = __getSharedStringBuilder();
+   * sb1.append(a).append(b);
+   * String c = sb1.toString();
+   *
+   * StringBuilder sb2 = __getSharedStringBuilder();
+   * sb2.append(b).append(a);
+   * String d = sb2.toString();
+   * </code></pre>
+   * <br><br>
+   * Example that doesn't work, you must call toString on sb1 before
+   * calling __getSharedStringBuilder again.
+   * <pre><code>
+   * StringBuilder sb1 = __getSharedStringBuilder();
+   * StringBuilder sb2 = __getSharedStringBuilder();
+   *
+   * sb1.append(a).append(b);
+   * String c = sb1.toString();
+   *
+   * sb2.append(b).append(a);
+   * String d = sb2.toString();
+   * </code></pre>
+   *
+   */
+  static StringBuilder __getSharedStringBuilder()
+  {
+    StringBuilder sb = _STRING_BUILDER.get();
+
+    if (sb == null)
+    {
+      sb = new StringBuilder();
+      _STRING_BUILDER.set(sb);
+    }
+
+    // clear out the stringBuilder by setting the length to 0
+    sb.setLength(0);
+
+    return sb;
+  }
+
   /**
    * render a component. this is called by renderers whose
    * getRendersChildren() return true.
@@ -1391,6 +1442,10 @@
 
   private static final Iterator<UIComponent> _EMPTY_UICOMPONENT_ITERATOR =
     new EmptyIterator<UIComponent>();
+
+
+  static private final ThreadLocal<StringBuilder> _STRING_BUILDER =
+                                                        new ThreadLocal<StringBuilder>();
 
   static private FacesBean.Type _createType()
   {