From: Leif Åstrand Date: Wed, 8 Jul 2015 07:44:35 +0000 (+0300) Subject: Cache connector state types (#18437) X-Git-Tag: 7.6.0.alpha3~3^2~11 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2564627fed188600e8e0925a9865481db39ffee7;p=vaadin-framework.git Cache connector state types (#18437) Reduces time spent in findStateType() for the "40 layouts" action in BasicPerformanceTest from around 2 ms to around 0.2 ms. This improves the total performance of the action by about 5%. Change-Id: I4f979827b2da0d4db87e201fa78421e5551a4113 --- diff --git a/server/src/com/vaadin/server/AbstractClientConnector.java b/server/src/com/vaadin/server/AbstractClientConnector.java index 0655b482ed..b6bcebd167 100644 --- a/server/src/com/vaadin/server/AbstractClientConnector.java +++ b/server/src/com/vaadin/server/AbstractClientConnector.java @@ -30,6 +30,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; +import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Logger; import com.vaadin.event.EventRouter; @@ -91,6 +92,8 @@ public abstract class AbstractClientConnector implements ClientConnector, private ErrorHandler errorHandler = null; + private static final ConcurrentHashMap, Class> stateTypeCache = new ConcurrentHashMap, Class>(); + @Override public void addAttachListener(AttachListener listener) { addListener(AttachEvent.ATTACH_EVENT_IDENTIFIER, AttachEvent.class, @@ -296,7 +299,12 @@ public abstract class AbstractClientConnector implements ClientConnector, // Lazy load because finding type can be expensive because of the // exceptions flying around if (stateType == null) { - stateType = findStateType(); + // Cache because we don't need to do this once per instance + stateType = stateTypeCache.get(this.getClass()); + if (stateType == null) { + stateType = findStateType(); + stateTypeCache.put(this.getClass(), stateType); + } } return stateType;