]> source.dussan.org Git - vaadin-framework.git/commitdiff
Cache state type property definitions (#10781)
authorLeif Åstrand <leif@vaadin.com>
Fri, 18 Jan 2013 09:25:45 +0000 (11:25 +0200)
committerLeif Åstrand <leif@vaadin.com>
Fri, 18 Jan 2013 09:26:11 +0000 (11:26 +0200)
Change-Id: I148c44e98c0a745b1674fbe5da5412845d33cc3d

server/src/com/vaadin/server/JsonCodec.java

index b9cdcf5a87cbca26d8d656ceb72b79daa9ae9d8b..9a70efab28e154b011347203e809cdf681fc8bb2 100644 (file)
@@ -38,6 +38,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 
 import org.json.JSONArray;
 import org.json.JSONException;
@@ -158,6 +160,14 @@ public class JsonCodec implements Serializable {
 
     }
 
+    /**
+     * Cache the collection of bean properties for a given type to avoid doing a
+     * quite expensive lookup multiple times. Will be used from any thread that
+     * happens to process Vaadin requests, so it must be protected from
+     * corruption caused by concurrent access.
+     */
+    private static ConcurrentMap<Class<?>, Collection<BeanProperty>> typePropertyCache = new ConcurrentHashMap<Class<?>, Collection<BeanProperty>>();
+
     private static Map<Class<?>, String> typeToTransportType = new HashMap<Class<?>, String>();
 
     /**
@@ -682,11 +692,18 @@ public class JsonCodec implements Serializable {
 
     public static Collection<BeanProperty> getProperties(Class<?> type)
             throws IntrospectionException {
+        Collection<BeanProperty> cachedProperties = typePropertyCache.get(type);
+        if (cachedProperties != null) {
+            return cachedProperties;
+        }
         Collection<BeanProperty> properties = new ArrayList<BeanProperty>();
 
         properties.addAll(MethodProperty.find(type));
         properties.addAll(FieldProperty.find(type));
 
+        // Doesn't matter if the same calculation is done multiple times from
+        // different threads, so there's no need to do e.g. putIfAbsent
+        typePropertyCache.put(type, properties);
         return properties;
     }