From 54a8cd0e435604a7b4273850d0bdb4bd1bc1a46b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Fri, 18 Jan 2013 11:25:45 +0200 Subject: [PATCH] Cache state type property definitions (#10781) Change-Id: I148c44e98c0a745b1674fbe5da5412845d33cc3d --- server/src/com/vaadin/server/JsonCodec.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/server/src/com/vaadin/server/JsonCodec.java b/server/src/com/vaadin/server/JsonCodec.java index b9cdcf5a87..9a70efab28 100644 --- a/server/src/com/vaadin/server/JsonCodec.java +++ b/server/src/com/vaadin/server/JsonCodec.java @@ -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, Collection> typePropertyCache = new ConcurrentHashMap, Collection>(); + private static Map, String> typeToTransportType = new HashMap, String>(); /** @@ -682,11 +692,18 @@ public class JsonCodec implements Serializable { public static Collection getProperties(Class type) throws IntrospectionException { + Collection cachedProperties = typePropertyCache.get(type); + if (cachedProperties != null) { + return cachedProperties; + } Collection properties = new ArrayList(); 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; } -- 2.39.5