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/03/07 22:26:40 UTC

svn commit: r1298129 - in /myfaces/core/branches/2.0.x: api/src/main/java/javax/faces/component/ impl/src/main/java/org/apache/myfaces/context/ impl/src/main/java/org/apache/myfaces/view/facelets/impl/

Author: lu4242
Date: Wed Mar  7 21:26:40 2012
New Revision: 1298129

URL: http://svn.apache.org/viewvc?rev=1298129&view=rev
Log:
MYFACES-3480 [perf] Use lazy init for HashMap/HastSet where possible (Thanks to Martin Koci for provide this patch)

Modified:
    myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/UIViewRoot.java
    myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java
    myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/context/RequestViewContext.java
    myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/impl/FaceletCompositionContextImpl.java
    myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/impl/TemplateContextImpl.java

Modified: myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/UIViewRoot.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/UIViewRoot.java?rev=1298129&r1=1298128&r2=1298129&view=diff
==============================================================================
--- myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/UIViewRoot.java (original)
+++ myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/UIViewRoot.java Wed Mar  7 21:26:40 2012
@@ -109,7 +109,7 @@ public class UIViewRoot extends UICompon
     // Tracks success in the beforePhase. Listeners that threw an exception
     // in beforePhase or were never called, because a previous listener threw
     // an exception, should not have their afterPhase method called
-    private transient Map<PhaseId, boolean[]> listenerSuccessMap = new HashMap<PhaseId, boolean[]>();
+    private transient Map<PhaseId, boolean[]> listenerSuccessMap;
     
     private static final String JAVAX_FACES_LOCATION_PREFIX = "javax_faces_location_";
     private static final String JAVAX_FACES_LOCATION_HEAD = "javax_faces_location_head";
@@ -122,8 +122,6 @@ public class UIViewRoot extends UICompon
     public UIViewRoot()
     {
         setRendererType(null);
-        
-        _systemEventListeners = new HashMap<Class<? extends SystemEvent>, List<SystemEventListener>>();
     }
 
     /**
@@ -835,12 +833,12 @@ public class UIViewRoot extends UICompon
             if (beforePhase)
             {
                 beforePhaseSuccess = new boolean[listenerCount];
-                listenerSuccessMap.put(phaseId, beforePhaseSuccess);
+                _getListenerSuccessMap().put(phaseId, beforePhaseSuccess);
             }
             else
             {
                 // afterPhase - get beforePhaseSuccess from the Map
-                beforePhaseSuccess = listenerSuccessMap.get(phaseId);
+                beforePhaseSuccess = _getListenerSuccessMap().get(phaseId);
                 if (beforePhaseSuccess == null)
                 {
                     // no Map available - assume that everything went well
@@ -1240,7 +1238,10 @@ public class UIViewRoot extends UICompon
     public List<SystemEventListener> getViewListenersForEventClass(Class<? extends SystemEvent> systemEvent)
     {
         checkNull (systemEvent, "systemEvent");
-        
+        if (_systemEventListeners == null)
+        {
+            return null;
+        }
         return _systemEventListeners.get (systemEvent);
     }
     
@@ -1252,6 +1253,11 @@ public class UIViewRoot extends UICompon
         checkNull (systemEvent, "systemEvent");
         checkNull (listener, "listener");
         
+        if (_systemEventListeners == null)
+        {
+            _systemEventListeners = new HashMap<Class<? extends SystemEvent>, List<SystemEventListener>>();
+        }
+        
         listeners = _systemEventListeners.get (systemEvent);
         
         if (listeners == null)
@@ -1272,6 +1278,11 @@ public class UIViewRoot extends UICompon
         checkNull (systemEvent, "systemEvent");
         checkNull (listener, "listener");
         
+        if (_systemEventListeners == null)
+        {
+            return;
+        }
+        
         listeners = _systemEventListeners.get (systemEvent);
         
         if (listeners != null)
@@ -1392,6 +1403,16 @@ public class UIViewRoot extends UICompon
         return logger;
     }
 
+    private Map<PhaseId, boolean[]> _getListenerSuccessMap()
+    {
+        // lazy init: 
+        if (listenerSuccessMap == null)
+        {
+            listenerSuccessMap = new HashMap<PhaseId, boolean[]>();
+        }
+        return listenerSuccessMap;
+    }
+
     private static interface PhaseProcessor
     {
         public void process(FacesContext context, UIViewRoot root);

Modified: myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java?rev=1298129&r1=1298128&r2=1298129&view=diff
==============================================================================
--- myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java (original)
+++ myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java Wed Mar  7 21:26:40 2012
@@ -218,7 +218,7 @@ class _ComponentAttributesMap implements
      */
     public Collection<Object> values()
     {
-        return getUnderlyingMap().values();
+        return getUnderlyingMap(true).values();
     }
 
     /**
@@ -238,7 +238,7 @@ class _ComponentAttributesMap implements
      */
     public Set<Map.Entry<String, Object>> entrySet()
     {
-        return getUnderlyingMap().entrySet();
+        return getUnderlyingMap(true).entrySet();
     }
 
     /**
@@ -247,7 +247,7 @@ class _ComponentAttributesMap implements
      */
     public Set<String> keySet()
     {
-        return getUnderlyingMap().keySet();
+        return getUnderlyingMap(true).keySet();
     }
 
     /**
@@ -604,8 +604,27 @@ class _ComponentAttributesMap implements
      */
     Map<String, Object> getUnderlyingMap()
     {
-        Map _attributes = (Map<String, Object>) _component.getStateHelper().get(UIComponentBase.PropertyKeys.attributesMap);
-        return _attributes == null ? Collections.EMPTY_MAP : _attributes; 
+        StateHelper stateHelper = _component.getStateHelper(false);
+        Map attributes = null;
+        if (stateHelper != null)
+        {
+            attributes = (Map<String, Object>) stateHelper.get(UIComponentBase.PropertyKeys.attributesMap);
+        }
+        return attributes == null ? Collections.EMPTY_MAP : attributes;
+    }
+    
+    Map<String, Object> getUnderlyingMap(boolean create)
+    {
+        if (create)
+        {
+            Map attributes
+                    = (Map<String, Object>) _component.getStateHelper().get(UIComponentBase.PropertyKeys.attributesMap);
+            return attributes == null ? Collections.EMPTY_MAP : attributes;
+        }
+        else
+        {
+            return getUnderlyingMap();
+        }
     }
 
     /**

Modified: myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/context/RequestViewContext.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/context/RequestViewContext.java?rev=1298129&r1=1298128&r2=1298129&view=diff
==============================================================================
--- myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/context/RequestViewContext.java (original)
+++ myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/context/RequestViewContext.java Wed Mar  7 21:26:40 2012
@@ -37,8 +37,9 @@ public class RequestViewContext
 
     public static final String VIEW_CONTEXT_KEY = "oam.VIEW_CONTEXT";
     
-    private Map<ResourceDependency, Boolean> addedResources = new HashMap<ResourceDependency,Boolean>();
+    private Map<ResourceDependency, Boolean> addedResources;
     
+    // No lazy init: every view has one (UIView.class) or more classes to process   
     private Map<Class<?>, Boolean> processedClasses = new HashMap<Class<?>,Boolean>();
     
     private Map<String, Boolean> renderTargetMap = null;
@@ -81,11 +82,19 @@ public class RequestViewContext
 
     public boolean isResourceDependencyAlreadyProcessed(ResourceDependency dependency)
     {
+        if (addedResources == null)
+        {
+            return false;
+        }
         return addedResources.containsKey(dependency); 
     }
     
     public void setResourceDependencyAsProcessed(ResourceDependency dependency)
     {
+        if (addedResources == null)
+        {
+            addedResources = new HashMap<ResourceDependency,Boolean>();
+        }
         addedResources.put(dependency, true);
     }
 

Modified: myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/impl/FaceletCompositionContextImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/impl/FaceletCompositionContextImpl.java?rev=1298129&r1=1298128&r2=1298129&view=diff
==============================================================================
--- myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/impl/FaceletCompositionContextImpl.java (original)
+++ myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/impl/FaceletCompositionContextImpl.java Wed Mar  7 21:26:40 2012
@@ -113,11 +113,11 @@ public class FaceletCompositionContextIm
     
     private int _deletionLevel;
     
-    private final Map<UIComponent, List<AttachedObjectHandler>> _attachedObjectHandlers;
+    private Map<UIComponent, List<AttachedObjectHandler>> _attachedObjectHandlers;
     
-    private final Map<UIComponent, Map<String, Object> > _methodExpressionsTargeted;
+    private Map<UIComponent, Map<String, Object> > _methodExpressionsTargeted;
     
-    private final Map<UIComponent, Map<String, Boolean> > _compositeComponentAttributesMarked;
+    private Map<UIComponent, Map<String, Boolean> > _compositeComponentAttributesMarked;
 
     private static final String VIEWROOT_FACELET_ID = "oam.VIEW_ROOT";
     
@@ -134,10 +134,7 @@ public class FaceletCompositionContextIm
         super();
         _factory = factory;
         _facesContext = facesContext;
-        _attachedObjectHandlers = new HashMap<UIComponent, List<AttachedObjectHandler>>();
         _componentsMarkedForDeletion = new ArrayList<Map<String,UIComponent>>();
-        _methodExpressionsTargeted = new HashMap<UIComponent, Map<String, Object>>();
-        _compositeComponentAttributesMarked = new HashMap<UIComponent, Map<String, Boolean>>();
         _deletionLevel = -1;
         _sectionUniqueIdCounter = new SectionUniqueIdCounter();
         //Cached at facelet view
@@ -545,7 +542,15 @@ public class FaceletCompositionContextIm
     @Override
     public void addAttachedObjectHandler(UIComponent compositeComponentParent, AttachedObjectHandler handler)
     {
-        List<AttachedObjectHandler> list = _attachedObjectHandlers.get(compositeComponentParent);
+        List<AttachedObjectHandler> list = null;
+        if (_attachedObjectHandlers == null)
+        {
+            _attachedObjectHandlers = new HashMap<UIComponent, List<AttachedObjectHandler>>();
+        }
+        else
+        {
+            list = _attachedObjectHandlers.get(compositeComponentParent);
+        }
 
         if (list == null)
         {
@@ -559,19 +564,35 @@ public class FaceletCompositionContextIm
     @Override
     public void removeAttachedObjectHandlers(UIComponent compositeComponentParent)
     {
+        if (_attachedObjectHandlers == null)
+        {
+            return;
+        }
         _attachedObjectHandlers.remove(compositeComponentParent);
     }
 
     @Override
     public List<AttachedObjectHandler> getAttachedObjectHandlers(UIComponent compositeComponentParent)
     {
+        if (_attachedObjectHandlers == null)
+        {
+            return null;
+        }
         return _attachedObjectHandlers.get(compositeComponentParent);
     }
     
     @Override
     public void addMethodExpressionTargeted(UIComponent targetedComponent, String attributeName, Object backingValue)
     {
-        Map<String, Object> map = _methodExpressionsTargeted.get(targetedComponent);
+        Map<String, Object> map = null;
+        if (_methodExpressionsTargeted == null)
+        {
+            _methodExpressionsTargeted = new HashMap<UIComponent, Map<String, Object>>();
+        }
+        else
+        {
+            map = _methodExpressionsTargeted.get(targetedComponent);
+        }
 
         if (map == null)
         {
@@ -584,6 +605,10 @@ public class FaceletCompositionContextIm
 
     public boolean isMethodExpressionAttributeApplied(UIComponent compositeComponentParent, String attributeName)
     {
+        if (_compositeComponentAttributesMarked == null)
+        {
+            return false;
+        }
         Map<String, Boolean> map = _compositeComponentAttributesMarked.get(compositeComponentParent);
         if (map == null)
         {
@@ -595,7 +620,16 @@ public class FaceletCompositionContextIm
     
     public void markMethodExpressionAttribute(UIComponent compositeComponentParent, String attributeName)
     {
-        Map<String, Boolean> map = _compositeComponentAttributesMarked.get(compositeComponentParent);
+        Map<String, Boolean> map = null;
+        if (_compositeComponentAttributesMarked == null)
+        {
+            _compositeComponentAttributesMarked = new HashMap<UIComponent, Map<String, Boolean>>(); 
+        }
+        else
+        {
+            map = _compositeComponentAttributesMarked.get(compositeComponentParent);
+        }
+        
         if (map == null)
         {
             map = new HashMap<String, Boolean>(8);
@@ -607,6 +641,10 @@ public class FaceletCompositionContextIm
     
     public void clearMethodExpressionAttribute(UIComponent compositeComponentParent, String attributeName)
     {
+        if (_compositeComponentAttributesMarked == null)
+        {
+            return;
+        }
         Map<String, Boolean> map = _compositeComponentAttributesMarked.get(compositeComponentParent);
         if (map == null)
         {
@@ -620,6 +658,10 @@ public class FaceletCompositionContextIm
     @Override
     public Object removeMethodExpressionTargeted(UIComponent targetedComponent, String attributeName)
     {
+        if (_methodExpressionsTargeted == null)
+        {
+            return null;
+        }
         Map<String, Object> map = _methodExpressionsTargeted.get(targetedComponent);
         if (map != null)
         {

Modified: myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/impl/TemplateContextImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/impl/TemplateContextImpl.java?rev=1298129&r1=1298128&r2=1298129&view=diff
==============================================================================
--- myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/impl/TemplateContextImpl.java (original)
+++ myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/impl/TemplateContextImpl.java Wed Mar  7 21:26:40 2012
@@ -107,7 +107,8 @@ public class TemplateContextImpl extends
     }
     
     @Override
-    public void extendClient(final AbstractFaceletContext actx, final AbstractFacelet owner, final TemplateClient client)
+    public void extendClient(final AbstractFaceletContext actx, final AbstractFacelet owner,
+                             final TemplateClient client)
     {
         _clients.addLast(new TemplateManagerImpl(owner, client, false, actx.getPageContext()));
         _lastClient = _clients.getLast();
@@ -124,7 +125,9 @@ public class TemplateContextImpl extends
         {
             client = itr.next();
             if (client.equals(owner))
+            {
                 continue;
+            }
             found = client.apply(ctx, parent, name);
         }
         return found;
@@ -138,7 +141,7 @@ public class TemplateContextImpl extends
 
         private final boolean _root;
 
-        private final Set<String> _names = new HashSet<String>();
+        private Set<String> _names;
         
         private final PageContext _pageContext;
         
@@ -162,12 +165,16 @@ public class TemplateContextImpl extends
             {
                 return false;
             }
-            if (this._names.contains(testName))
+            if (this._names != null && this._names.contains(testName))
             {
                 return false;
             }
             else
             {
+                if (this._names == null)
+                {
+                    this._names =  new HashSet<String>();
+                }
                 this._names.add(testName);
                 boolean found = false;
                 AbstractFaceletContext actx = new DefaultFaceletContext(
@@ -206,8 +213,6 @@ public class TemplateContextImpl extends
 
         public boolean equals(Object o)
         {
-            // System.out.println(this.owner.getAlias() + " == " +
-            // ((DefaultFacelet) o).getAlias());
             if (this._owner != null)
             {
                 return this._owner == o || this._target == o;
@@ -218,6 +223,14 @@ public class TemplateContextImpl extends
             }
         }
 
+        @Override
+        public int hashCode()
+        {
+            int result = _owner != null ? _owner.hashCode() : 0;
+            result = 31 * result + (_target != null ? _target.hashCode() : 0);
+            return result;
+        }
+
         public boolean isRoot()
         {
             return this._root;
@@ -255,7 +268,8 @@ public class TemplateContextImpl extends
     }
 
     @Override
-    public void setParameter(String key, ValueExpression value) {
+    public void setParameter(String key, ValueExpression value)
+    {
         if (_lastClient != null)
         {
             _lastClient.getParametersMap().put(key, value);
@@ -263,7 +277,8 @@ public class TemplateContextImpl extends
     }
 
     @Override
-    public boolean isParameterEmpty() {
+    public boolean isParameterEmpty()
+    {
         TemplateManagerImpl client;
         Iterator<TemplateManagerImpl> itr = _clients.iterator();
         while (itr.hasNext())
@@ -282,7 +297,8 @@ public class TemplateContextImpl extends
         return new TemplateClientAttributeMap();
     }
 
-    private final class TemplateClientAttributeMap extends AbstractAttributeMap<ValueExpression> {
+    private final class TemplateClientAttributeMap extends AbstractAttributeMap<ValueExpression>
+    {
 
         public TemplateClientAttributeMap()
         {
@@ -377,8 +393,8 @@ public class TemplateContextImpl extends
     public static final class InitialTemplateClient implements TemplateClient
     {
         public boolean apply(FaceletContext ctx, UIComponent parent, String name)
-                throws IOException, FacesException, FaceletException,
-                ELException {
+                throws IOException, FacesException, FaceletException, ELException
+        {
             return false;
         }
     }