From: Clebert S. <csu...@jb...> - 2006-03-15 22:28:41
|
User: csuconic Date: 06/03/15 17:28:40 Modified: src/org/jboss/serial/classmetamodel ClassMetamodelFactory.java Log: Adding FastHashMap on metadata- JBSER-52 Revision Changes Path 1.33 +89 -28 jboss-serialization/src/org/jboss/serial/classmetamodel/ClassMetamodelFactory.java (In the diff below, changes in quantity of whitespace are not shown.) Index: ClassMetamodelFactory.java =================================================================== RCS file: /cvsroot/jboss/jboss-serialization/src/org/jboss/serial/classmetamodel/ClassMetamodelFactory.java,v retrieving revision 1.32 retrieving revision 1.33 diff -u -b -r1.32 -r1.33 --- ClassMetamodelFactory.java 14 Mar 2006 23:44:57 -0000 1.32 +++ ClassMetamodelFactory.java 15 Mar 2006 22:28:40 -0000 1.33 @@ -27,22 +27,63 @@ import java.io.ObjectStreamClass; import java.io.ObjectStreamField; import java.io.Serializable; +import java.lang.ref.WeakReference; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.HashMap; import java.util.Map; import java.util.WeakHashMap; +import org.hibernate.util.FastHashMap; + //import org.jboss.serial.util.PartitionedWeakHashMap; import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap; /** - * $Id: ClassMetamodelFactory.java,v 1.32 2006/03/14 23:44:57 csuconic Exp $ + * $Id: ClassMetamodelFactory.java,v 1.33 2006/03/15 22:28:40 csuconic Exp $ * @author clebert suconic */ public class ClassMetamodelFactory { + /** We are caching the getClassLoader operation, to avoid locks on the WeakHashMap */ + private static class CacheLoaderReference + { + WeakReference currentClassLoader; + WeakReference currentHashMap; + + public ClassLoader getCurrentClassLoader() + { + if (currentClassLoader==null) + return null; + else + return (ClassLoader)currentClassLoader.get(); + } + + public void setCurrentClassLoader(ClassLoader loader) + { + currentClassLoader = new WeakReference(loader); + } + + public Map getCurrentMap() + { + if (currentHashMap==null) + { + return null; + } + else + { + return (Map)(currentHashMap.get()); + } + } + + public void setCurrentMap(Map currentMap) + { + this.currentHashMap = new WeakReference(currentMap); + } + } + /** * Method for ObjectStreamClass.lookup @@ -72,6 +113,8 @@ return (Field) methodGetField.invoke(source,new Object[]{}); } + static ThreadLocal cacheLoader = new ThreadLocal(); + static { try @@ -143,23 +186,39 @@ } } - private static ConcurrentHashMap getLoaderMap(ClassLoader loader) + private static Map getLoaderMap(ClassLoader loader) { if (loader==null) { loader = systemClassLoaderIdentifier; } - ConcurrentHashMap hashMap = (ConcurrentHashMap)cache.get(loader); + CacheLoaderReference loaderReference = (CacheLoaderReference)cacheLoader.get(); + if (loaderReference==null) + { + loaderReference = new CacheLoaderReference(); + cacheLoader.set(loaderReference); + } + + if (loaderReference.getCurrentClassLoader()==loader) + { + return loaderReference.getCurrentMap(); + } + + + Map hashMap = (Map)cache.get(loader); + + ClassLoader returnLoader = null; if (hashMap==null) { - hashMap = new ConcurrentHashMap(); + hashMap = new FastHashMap(); cache.put(loader,hashMap); - return (ConcurrentHashMap)cache.get(loader); - } else - { - return hashMap; + hashMap= (Map)cache.get(loader); } + + loaderReference.setCurrentClassLoader(loader); + loaderReference.setCurrentMap(hashMap); + return hashMap; } @@ -167,10 +226,15 @@ { try { - ConcurrentHashMap loaderMap = getLoaderMap(loader); + Map loaderMap = getLoaderMap(loader); ClassMetaData classMetadata = (ClassMetaData)loaderMap.get(clazzName); if (classMetadata==null) { + synchronized (ClassMetamodelFactory.class) + { + classMetadata = (ClassMetaData)loaderMap.get(clazzName); + if (classMetadata==null) + { Class clazz = loader.loadClass(clazzName); loaderMap = getLoaderMap(clazz.getClassLoader()); classMetadata = (ClassMetaData)loaderMap.get(clazzName); @@ -184,6 +248,8 @@ } classMetadata = new ClassMetaData(clazz); loaderMap.put(clazzName,classMetadata); + } + } classMetadata = (ClassMetaData)loaderMap.get(clazzName); } @@ -212,15 +278,10 @@ { throw new NotSerializableException(clazz.getName()); } - ConcurrentHashMap loaderMap = getLoaderMap(clazz.getClassLoader()); + Map loaderMap = getLoaderMap(clazz.getClassLoader()); ClassMetaData classMetadata = (ClassMetaData)loaderMap.get(clazz.getName()); if (classMetadata==null) { - classMetadata = (ClassMetaData)loaderMap.get(clazz.getName()); - if (classMetadata !=null) - { - return classMetadata; - } classMetadata = new ClassMetaData(clazz); loaderMap.put(clazz.getName(),classMetadata); classMetadata = (ClassMetaData)loaderMap.get(clazz.getName()); |