7163865: Performance improvement for DateFormatSymbols.getZoneIndex(String)
authoryoudwei
Tue, 24 Apr 2012 21:06:16 +0800
changeset 12549 db2c42dabfa4
parent 12548 386de6da1ff0
child 12550 482c64a6f825
7163865: Performance improvement for DateFormatSymbols.getZoneIndex(String) Reviewed-by: okutsu
jdk/src/share/classes/java/text/DateFormatSymbols.java
--- a/jdk/src/share/classes/java/text/DateFormatSymbols.java	Mon Apr 23 16:34:32 2012 +0200
+++ b/jdk/src/share/classes/java/text/DateFormatSymbols.java	Tue Apr 24 21:06:16 2012 +0800
@@ -647,6 +647,8 @@
     private static final ConcurrentMap<Locale, SoftReference<DateFormatSymbols>> cachedInstances
         = new ConcurrentHashMap<Locale, SoftReference<DateFormatSymbols>>(3);
 
+    private transient int lastZoneIndex = 0;
+
     private void initializeData(Locale desiredLocale) {
         locale = desiredLocale;
 
@@ -692,12 +694,24 @@
      * the given time zone ID can't be located in the DateFormatSymbols object.
      * @see java.util.SimpleTimeZone
      */
-    final int getZoneIndex(String ID)
-    {
+    final int getZoneIndex(String ID) {
         String[][] zoneStrings = getZoneStringsWrapper();
-        for (int index=0; index<zoneStrings.length; index++)
-        {
-            if (ID.equals(zoneStrings[index][0])) return index;
+
+        /*
+         * getZoneIndex has been re-written for performance reasons. instead of
+         * traversing the zoneStrings array every time, we cache the last used zone
+         * index
+         */
+        if (lastZoneIndex < zoneStrings.length && ID.equals(zoneStrings[lastZoneIndex][0])) {
+            return lastZoneIndex;
+        }
+
+        /* slow path, search entire list */
+        for (int index = 0; index < zoneStrings.length; index++) {
+            if (ID.equals(zoneStrings[index][0])) {
+                lastZoneIndex = index;
+                return index;
+            }
         }
 
         return -1;