You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by jw...@apache.org on 2010/04/23 18:54:10 UTC
svn commit: r937373 -
/myfaces/trinidad/branches/1.2.12.3-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/style/cache/FileSystemStyleCache.java
Author: jwaldman
Date: Fri Apr 23 16:54:10 2010
New Revision: 937373
URL: http://svn.apache.org/viewvc?rev=937373&view=rev
Log:
TRINIDAD-1782 Live mmeory from style sheet cache
1. change _styleNodeToStyleMap from being a class variable to a method variable.
2. cache the calculated has code to avoid expensive computation.
this is for 1.2.12.3-branch
Modified:
myfaces/trinidad/branches/1.2.12.3-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/style/cache/FileSystemStyleCache.java
Modified: myfaces/trinidad/branches/1.2.12.3-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/style/cache/FileSystemStyleCache.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/branches/1.2.12.3-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/style/cache/FileSystemStyleCache.java?rev=937373&r1=937372&r2=937373&view=diff
==============================================================================
--- myfaces/trinidad/branches/1.2.12.3-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/style/cache/FileSystemStyleCache.java (original)
+++ myfaces/trinidad/branches/1.2.12.3-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/style/cache/FileSystemStyleCache.java Fri Apr 23 16:54:10 2010
@@ -1407,7 +1407,7 @@ public class FileSystemStyleCache implem
* @param resolvedStyles
* @param namespacePrefixArray an array of namespace prefixes that are used in the custom css
* skinning selectors, like "af" in af|inputText.
- * @param afSelectoMap a map from one selector to another (like af|panelHeader::link maps to
+ * @param afSelectorMap a map from one selector to another (like af|panelHeader::link maps to
* af|panelHeader A
* @param shortStyleClassMap a map from the non-compressed styleclass
* to a compressed styleclass.
@@ -1427,7 +1427,11 @@ public class FileSystemStyleCache implem
_compress = compress;
// create a Selector->Style map right here (aggressively versus lazily)
ConcurrentMap<Selector, Style> resolvedSelectorStyleMap = null;
-
+ /* This is used so that we can re-use Style objects if the property name and value
+ * is the same. Instead of creating a Style object for every property, we only
+ * create it for unique property name/values, and we store these in the map.
+ * Then we pull them out of the map to create a Style object. */
+ Map<StyleKey, Style> styleNodeToStyleMap = new ConcurrentHashMap<StyleKey, Style>();
// Loop through each StyleNode and use it to add to the StyleMap.
for (int i=0; i < resolvedStyles.length; i++)
@@ -1435,7 +1439,7 @@ public class FileSystemStyleCache implem
String selector = resolvedStyles[i].getSelector();
if (selector != null)
{
- Style style = _convertStyleNodeToStyle(resolvedStyles[i]);
+ Style style = _convertStyleNodeToStyle(resolvedStyles[i], styleNodeToStyleMap);
if (resolvedSelectorStyleMap == null)
resolvedSelectorStyleMap = new ConcurrentHashMap<Selector, Style>();
resolvedSelectorStyleMap.put(Selector.createSelector(selector), style);
@@ -1525,9 +1529,15 @@ public class FileSystemStyleCache implem
* should already be resolved (included selectors have been merged in)
* so that all the css properties are there.
* @param styleNode
- * @return A Style object created from the information in the styleNode.
+ * @param styleNodeToStyleMap A Map holding StyleKey objects to Style objects. This is
+ * used so that we can reuse CSSStyle objects if they have the same list of style property
+ * names and values.
+ * @return A Style object created from the information in the styleNode. We reuse
+ * Style objects if the properties are the same.
*/
- public Style _convertStyleNodeToStyle(StyleNode styleNode)
+ public Style _convertStyleNodeToStyle(
+ StyleNode styleNode,
+ Map<StyleKey, Style> styleNodeToStyleMap)
{
Map<String, String> styleProperties = new ConcurrentHashMap<String, String>();
// Add in the properties for the style
@@ -1542,17 +1552,17 @@ public class FileSystemStyleCache implem
styleProperties.put(name, value);
}
- // To save memory, we reuse CSSStyle objects if
+ // To save memory, we reuse CSSStyle objects if
// they have the same list of style property names and values.
// StyleKey is the key into the StyleKey, CSSStyle map.
StyleKey key = new StyleKey(styleProperties);
- Style cachedStyle = _styleNodeToStyleMap.get(key);
+ Style cachedStyle = styleNodeToStyleMap.get(key);
if (cachedStyle == null)
{
// no match is cached yet, so create a new CSSStyle and cache in the map.
Style style = new CSSStyle(styleProperties);
- _styleNodeToStyleMap.put(key, style);
- return style;
+ styleNodeToStyleMap.put(key, style);
+ return style;
}
else
{
@@ -1569,25 +1579,16 @@ public class FileSystemStyleCache implem
public StyleKey(Map<String, String> styleProperties)
{
_styleProperties = styleProperties;
+ _hashCode = _hashCode();
}
@Override
public int hashCode()
{
- int hash = 17;
- // take each style property name and value and create a hashCode from it.
- for (Map.Entry<String, String> e : _styleProperties.entrySet())
- {
- String name = e.getKey();
- hash = 37*hash + ((null == name) ? 0 : name.hashCode());
-
- String value = e.getValue();
- hash = 37*hash + ((null == value) ? 0 : value.hashCode());
-
- }
- return hash;
+ return _hashCode;
}
- @Override
+
+ @Override
public boolean equals(Object obj)
{
if (this == obj)
@@ -1600,7 +1601,29 @@ public class FileSystemStyleCache implem
return test._styleProperties.equals(this._styleProperties);
}
- Map<String, String> _styleProperties;
+ /**
+ * Private implementation of hashCode. This way we can cache the hashcode.
+ * @return
+ */
+ private int _hashCode()
+ {
+ int hash = 17;
+ // take each style property name and value and create a hashCode from it.
+ for (Map.Entry<String, String> e : _styleProperties.entrySet())
+ {
+ String name = e.getKey();
+ hash = 37*hash + ((null == name) ? 0 : name.hashCode());
+
+ String value = e.getValue();
+ hash = 37*hash + ((null == value) ? 0 : value.hashCode());
+
+ }
+ return hash;
+ }
+
+ private final Map<String, String> _styleProperties;
+ private final int _hashCode;
+
}
@@ -1609,7 +1632,6 @@ public class FileSystemStyleCache implem
private final String[] _namespacePrefixArray;
private final Map<String, String> _shortStyleClassMap;
private final boolean _compress;
- private Map<StyleKey, Style> _styleNodeToStyleMap = new ConcurrentHashMap<StyleKey, Style>();
}
private class StyleWriterFactoryImpl