Revision: 10026
Author:   [email protected]
Date:     Tue Apr 19 08:27:36 2011
Log:      Proposition: Use ConcurrentHashMap to avoid locking when using the
clientOracleCache.

Review at http://gwt-code-reviews.appspot.com/1425803

http://code.google.com/p/google-web-toolkit/source/detail?r=10026

Modified:
 /trunk/user/src/com/google/gwt/rpc/server/RpcServlet.java

=======================================
--- /trunk/user/src/com/google/gwt/rpc/server/RpcServlet.java Thu Dec 16 11:33:51 2010 +++ /trunk/user/src/com/google/gwt/rpc/server/RpcServlet.java Tue Apr 19 08:27:36 2011
@@ -33,8 +33,8 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.UnknownHostException;
-import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.zip.GZIPOutputStream;

 import javax.servlet.ServletException;
@@ -53,7 +53,7 @@
   protected static final String CLIENT_ORACLE_EXTENSION = ".gwt.rpc";
private static final boolean DUMP_PAYLOAD = Boolean.getBoolean("gwt.rpc.dumpPayload");

- private final Map<String, SoftReference<ClientOracle>> clientOracleCache = new HashMap<String, SoftReference<ClientOracle>>(); + private final Map<String, SoftReference<ClientOracle>> clientOracleCache = new ConcurrentHashMap<String, SoftReference<ClientOracle>>();

   /**
    * The implementation of the service.
@@ -98,6 +98,17 @@

     ClientOracle toReturn;

+    // Fast path if the ClientOracle is already cached.
+    if (clientOracleCache.containsKey(permutationStrongName)) {
+      toReturn = clientOracleCache.get(permutationStrongName).get();
+      if (toReturn != null) {
+        return toReturn;
+      }
+    }
+
+    /* Synchronize to make sure expensive calls are executed only once.
+       Double checked locking idiom works here because of volatiles in
+       ConcurrentHashMap.*/
     synchronized (clientOracleCache) {
       if (clientOracleCache.containsKey(permutationStrongName)) {
         toReturn = clientOracleCache.get(permutationStrongName).get();

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to