You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2012/01/16 23:16:08 UTC

svn commit: r1232196 - /myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/tag/MetaRulesetImpl.java

Author: lu4242
Date: Mon Jan 16 22:16:07 2012
New Revision: 1232196

URL: http://svn.apache.org/viewvc?rev=1232196&view=rev
Log:
MYFACES-3425 [perf] enhance code applying best practices (reduce expected size for MetaRulesetImpl)

Modified:
    myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/tag/MetaRulesetImpl.java

Modified: myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/tag/MetaRulesetImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/tag/MetaRulesetImpl.java?rev=1232196&r1=1232195&r2=1232196&view=diff
==============================================================================
--- myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/tag/MetaRulesetImpl.java (original)
+++ myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/tag/MetaRulesetImpl.java Mon Jan 16 22:16:07 2012
@@ -59,7 +59,7 @@ public final class MetaRulesetImpl exten
      * most certainly cause a memory leak! Furthermore we can manually cleanup the Map when
      * the webapp is undeployed just by removing the Map for the current ClassLoader. 
      */
-    private final static WeakHashMap<ClassLoader, Map<String, MetadataTarget>> _metadata
+    private volatile static WeakHashMap<ClassLoader, Map<String, MetadataTarget>> metadata
             = new WeakHashMap<ClassLoader, Map<String, MetadataTarget>>();
 
     /**
@@ -67,7 +67,7 @@ public final class MetaRulesetImpl exten
      */
     public static void clearMetadataTargetCache()
     {
-        _metadata.remove(ClassUtils.getContextClassLoader());
+        metadata.remove(ClassUtils.getContextClassLoader());
     }
 
     private static Map<String, MetadataTarget> getMetaData()
@@ -75,25 +75,31 @@ public final class MetaRulesetImpl exten
         ClassLoader cl = ClassUtils.getContextClassLoader();
         
         Map<String, MetadataTarget> metadata = (Map<String, MetadataTarget>)
-                _metadata.get(cl);
+                MetaRulesetImpl.metadata.get(cl);
 
         if (metadata == null)
         {
             // Ensure thread-safe put over _metadata, and only create one map
             // per classloader to hold metadata.
-            synchronized (_metadata)
+            synchronized (MetaRulesetImpl.metadata)
             {
-                metadata = (Map<String, MetadataTarget>) _metadata.get(cl);
-                if (metadata == null)
-                {
-                    metadata = new HashMap<String, MetadataTarget>();
-                    _metadata.put(cl, metadata);
-                }
+                metadata = createMetaData(cl, metadata);
             }
         }
 
         return metadata;
     }
+    
+    private static Map<String, MetadataTarget> createMetaData(ClassLoader cl, Map<String, MetadataTarget> metadata)
+    {
+        metadata = (Map<String, MetadataTarget>) MetaRulesetImpl.metadata.get(cl);
+        if (metadata == null)
+        {
+            metadata = new HashMap<String, MetadataTarget>();
+            MetaRulesetImpl.metadata.put(cl, metadata);
+        }
+        return metadata;
+    }
 
     private final Map<String, TagAttribute> _attributes;
 
@@ -109,12 +115,19 @@ public final class MetaRulesetImpl exten
     {
         _tag = tag;
         _type = type;
-        _attributes = new HashMap<String, TagAttribute>();
-        _mappers = new ArrayList<Metadata>();
-        _rules = new ArrayList<MetaRule>();
+        TagAttribute[] allAttributes = _tag.getAttributes().getAll();
+        // This map is proportional to the number of attributes defined, and usually
+        // the properties with alias are very few, so set an initial size close to
+        // the number of attributes is ok.
+        int initialSize = allAttributes.length > 0 ? (allAttributes.length * 4 + 3) / 3 : 4;
+        _attributes = new HashMap<String, TagAttribute>(initialSize);
+        _mappers = new ArrayList<Metadata>(initialSize);
+        // Usually ComponentTagHandlerDelegate has 5 rules at max
+        // and CompositeComponentResourceTagHandler 6, so 8 is a good number
+        _rules = new ArrayList<MetaRule>(8); 
 
         // setup attributes
-        for (TagAttribute attribute : _tag.getAttributes().getAll())
+        for (TagAttribute attribute : allAttributes)
         {
             _attributes.put(attribute.getLocalName(), attribute);
         }