7092825: javax.crypto.Cipher.Transform.patternCache is synchronizedMap and became scalability bottleneck.
authorvaleriep
Thu, 19 Jan 2012 12:01:57 -0800
changeset 11671 60fdf1412864
parent 11531 b1f9f8d4806b
child 11672 a5fa8c844b54
7092825: javax.crypto.Cipher.Transform.patternCache is synchronizedMap and became scalability bottleneck. Summary: Changed patternCache from synchronizedMap to ConcurrentHashMap. Reviewed-by: mullan
jdk/src/share/classes/javax/crypto/Cipher.java
--- a/jdk/src/share/classes/javax/crypto/Cipher.java	Wed Jan 18 11:00:20 2012 -0800
+++ b/jdk/src/share/classes/javax/crypto/Cipher.java	Thu Jan 19 12:01:57 2012 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,6 +26,8 @@
 package javax.crypto;
 
 import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 import java.util.regex.*;
 
 
@@ -389,16 +391,15 @@
             return matches(regexp, value) ? S_YES : S_NO;
         }
 
-        // Map<String,Pattern> for previously compiled patterns
-        // XXX use ConcurrentHashMap once available
-        private final static Map<String, Pattern> patternCache =
-            Collections.synchronizedMap(new HashMap<String, Pattern>());
+        // ConcurrentMap<String,Pattern> for previously compiled patterns
+        private final static ConcurrentMap<String, Pattern> patternCache =
+            new ConcurrentHashMap<String, Pattern>();
 
         private static boolean matches(String regexp, String str) {
             Pattern pattern = patternCache.get(regexp);
             if (pattern == null) {
                 pattern = Pattern.compile(regexp);
-                patternCache.put(regexp, pattern);
+                patternCache.putIfAbsent(regexp, pattern);
             }
             return pattern.matcher(str.toUpperCase(Locale.ENGLISH)).matches();
         }