From f4248e0caad9ca6acf99058d6ace31ba5f3281d9 Mon Sep 17 00:00:00 2001
From: Chad Metcalf <chad@cloudera.com>
Date: Tue, 15 Sep 2009 22:21:42 -0700
Subject: [PATCH] HADOOP-6133: ReflectionUtils performance regression

---
 src/core/org/apache/hadoop/conf/Configuration.java |   24 +++++++++++++++++++-
 1 files changed, 23 insertions(+), 1 deletions(-)

diff --git a/src/core/org/apache/hadoop/conf/Configuration.java b/src/core/org/apache/hadoop/conf/Configuration.java
index 34dfe1f..2681ebb 100644
--- a/src/core/org/apache/hadoop/conf/Configuration.java
+++ b/src/core/org/apache/hadoop/conf/Configuration.java
@@ -42,6 +42,8 @@ import java.util.Properties;
 import java.util.Set;
 import java.util.StringTokenizer;
 import java.util.WeakHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -165,6 +167,9 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
    */
   private static final ArrayList<String> defaultResources = 
     new ArrayList<String>();
+
+  private static final ConcurrentMap<ClassLoader, Map<String, Class<?>>>
+    CACHE_CLASSES = new ConcurrentHashMap<ClassLoader, Map<String, Class<?>>>();
   
   static{
     //print deprecation warning if hadoop-site.xml is found in classpath
@@ -759,7 +764,24 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
    * @throws ClassNotFoundException if the class is not found.
    */
   public Class<?> getClassByName(String name) throws ClassNotFoundException {
-    return Class.forName(name, true, classLoader);
+    Map<String, Class<?>> map = CACHE_CLASSES.get(classLoader);
+    if (map == null) {
+      Map<String, Class<?>> newMap = new ConcurrentHashMap<String, Class<?>>();
+      map = CACHE_CLASSES.putIfAbsent(classLoader, newMap);
+      if (map == null) {
+        map = newMap;
+      }
+    }
+
+    Class clazz = map.get(name);
+    if (clazz == null) {
+      clazz = Class.forName(name, true, classLoader);
+      if (clazz != null) {
+        map.put(name, clazz);
+      }
+    }
+
+    return clazz;
   }
 
   /** 
-- 
1.6.0.4

