You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by de...@apache.org on 2017/09/17 12:30:01 UTC
[myfaces-trinidad] 04/04: TRINIDAD-1702 decrease memory of
FileSystemStyleCache by reusing CSSStyle objects for branch
1.2.12.1.1-branch
This is an automated email from the ASF dual-hosted git repository.
deki pushed a commit to branch 1.2.12.1.1-branch
in repository https://gitbox.apache.org/repos/asf/myfaces-trinidad.git
commit 802c2f8fe9ecc457cd984d58ace3300c97622942
Author: Jeanne Waldman <jw...@apache.org>
AuthorDate: Wed Mar 3 19:26:32 2010 +0000
TRINIDAD-1702 decrease memory of FileSystemStyleCache by reusing CSSStyle objects
for branch 1.2.12.1.1-branch
---
.../style/cache/FileSystemStyleCache.java | 64 +++++++++++++++++++++-
1 file changed, 61 insertions(+), 3 deletions(-)
diff --git a/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/style/cache/FileSystemStyleCache.java b/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/style/cache/FileSystemStyleCache.java
index fbbad0b..dfd4a6b 100644
--- a/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/style/cache/FileSystemStyleCache.java
+++ b/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/style/cache/FileSystemStyleCache.java
@@ -1514,8 +1514,66 @@ public class FileSystemStyleCache implements StyleProvider
if (name != null && value != null)
styleProperties.put(name, value);
}
+
+ // 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);
+ 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;
+ }
+ else
+ {
+ return cachedStyle;
+ }
+ }
+
+ /**
+ * A StyleKey object is used as a key into a map so that we can share CSSStyle objects
+ * if they are equal and they have the same hashCode.
+ */
+ private static class StyleKey
+ {
+ public StyleKey(Map<String, String> styleProperties)
+ {
+ _styleProperties = styleProperties;
+ }
+
+ @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 new CSSStyle(styleProperties);
+ }
+ return hash;
+ }
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (this == obj)
+ return true;
+ if (!(obj instanceof StyleKey))
+ return false;
+
+ // obj at this point must be a StyleKey
+ StyleKey test = (StyleKey)obj;
+ return test._styleProperties.equals(this._styleProperties);
+ }
+
+ Map<String, String> _styleProperties;
}
@@ -1524,6 +1582,7 @@ public class FileSystemStyleCache implements StyleProvider
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
@@ -1553,7 +1612,7 @@ public class FileSystemStyleCache implements StyleProvider
}
File outputFile = _getOutputFile(_baseFilename, _files.size() + 1);
- // We never want to do anything other then read it or delete it:
+ // We never want to do anything other than read it or delete it:
outputFile.setReadOnly();
_files.add(outputFile);
@@ -1628,7 +1687,6 @@ public class FileSystemStyleCache implements StyleProvider
* names do not contain html, whereas our internal style selector
* names may. We write out the shortened version of the mapped
* selector names to the css file.
- * jmw.
* @todo Need to find a better spot for this, like the skin?
*/
private static final Map<String, String> _STYLE_KEY_MAP;
--
To stop receiving notification emails like this one, please contact
"commits@myfaces.apache.org" <co...@myfaces.apache.org>.