We were profiling our turbine application the other day and one thing that bubbled up to the top was the JavaBaseFactory.getAssembler() class. This class iterates over all of the packages where Assemblers may be found and retrieves a Class instance when it encounters a hit. But it does this for every call to the getAssembler method, and we found the resulting time spent in Class.forName() to be very high. I have placed a simple synchronized map in our version of the class that is used to cache previously found Assemblers and is queried before going through the package loop. This lowered the time we spend in Class.forName() substantially.
First of all, is this not a good thing to do?
And second, if this *is* a good thing to do, here is the patch if you are interested...
Index: src/java/org/apache/turbine/services/assemblerbroker/util/java/ JavaBaseFactory.java
===================================================================
RCS file: /home/cvsroot/turbine/src/java/org/apache/turbine/services/ assemblerbroker/util/java/JavaBaseFactory.java,v
retrieving revision 1.1.1.2
diff -u -r1.1.1.2 JavaBaseFactory.java
--- src/java/org/apache/turbine/services/assemblerbroker/util/java/ JavaBaseFactory.java 20 Apr 2004 23:59:23 -0000 1.1.1.2
+++ src/java/org/apache/turbine/services/assemblerbroker/util/java/ JavaBaseFactory.java 21 Apr 2004 00:06:03 -0000
@@ -16,7 +16,10 @@
* limitations under the License.
*/
+import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; +import java.util.Map; import java.util.Vector;
import org.apache.commons.lang.StringUtils;
@@ -47,6 +50,9 @@
/** Logging */
protected Log log = LogFactory.getLog(this.getClass());+ /** A cache for previously obtained assemblers */
+ private Map assemblerCache = Collections.synchronizedMap(new HashMap());
+
static
{
ObjectUtils.addOnce(packages, GenericLoader.getBasePackage());
@@ -67,7 +73,8 @@
if (StringUtils.isNotEmpty(name))
{
- for (Iterator it = packages.iterator(); it.hasNext();)
+ assembler = (Assembler) assemblerCache.get(name);
+ for (Iterator it = packages.iterator(); assembler == null && it.hasNext();)
{
StringBuffer className = new StringBuffer();
@@ -83,6 +90,7 @@
{
Class servClass = Class.forName(className.toString());
assembler = (Assembler) servClass.newInstance();
+ assemblerCache.put(name, assembler);
break; // for()
}
catch (ClassNotFoundException cnfe)
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
