classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] RFC: Adding a cache for locale information


From: Guilhem Lavaux
Subject: [cp-patches] RFC: Adding a cache for locale information
Date: Sun, 11 Dec 2005 14:20:52 +0100
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050322)

Hi,

Using ant we may have discovered that multiple creation of a SimpleDateFormat object can lead to a big slowdown. I am proposing the addition of a simple cache system. LocaleCache would cache ResourceBundle objects and some parsed strings in a WeakHashMap. If it is needed LocaleCache will create these objects and in the other case extract them directly from the cache.

This patch shows DateFormatSymbols can be easily modified to follow this scheme.

Regards,

Guilhem.

ChangeLog:

2005-12-11  Guilhem Lavaux  <address@hidden>

        * gnu/java/util/LocaleCache.java: New file.

        * java/text/DateFormatSymbols.java: Use the cache instead of
        reparsing the properties each time the object is created.
Index: gnu/java/util/LocaleCache.java
===================================================================
RCS file: gnu/java/util/LocaleCache.java
diff -N gnu/java/util/LocaleCache.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ gnu/java/util/LocaleCache.java      11 Dec 2005 13:19:25 -0000
@@ -0,0 +1,144 @@
+/* gnu.java.util.LocaleCache
+   Copyright (C) 2003 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.java.util;
+
+import java.util.WeakHashMap;
+import java.util.ResourceBundle;
+import java.util.Locale;
+import java.util.MissingResourceException;
+
+public class LocaleCache
+{
+  static private LocaleCache singleton = new LocaleCache();
+
+  private WeakHashMap cache;
+  
+  private LocaleCache()
+  {
+    cache = new WeakHashMap();
+  }
+  
+  static public LocaleCache getCache()
+  {
+    return singleton;
+  }
+  
+  private String[] getStringArray(Locale locale, String key)
+  {
+    return getLocaleInformationBundle(locale).getString(key).split("\u00ae");
+  }
+  
+  public String[] getCacheStringArray(Locale locale, String key)
+  {
+    String localeStr = locale.toString();
+    
+    if ("".equals(localeStr))
+      return getStringArray(locale, key);
+    
+    String keyStr = localeStr + "+key+" + key;
+    String[] array = (String[])cache.get(keyStr);
+    
+    if (array == null)
+      {
+       array = getStringArray(locale, key);
+       cache.put(keyStr, array);
+      }
+    
+    return array;
+  }
+  
+  private String[][] getZoneStrings(Locale locale)
+  {
+    ResourceBundle res = getLocaleInformationBundle(locale);
+    try
+      {
+       int index = 0;
+       String data = res.getString("zoneStrings");
+       String[] zones = data.split("\u00a9");
+       String[][] array = new String[zones.length][];
+       for (int a = 0; a < zones.length; ++a)
+         array[a] = zones[a].split("\u00ae");
+       return array;
+      }
+    catch (MissingResourceException e)
+      {
+       return new String[0][];
+      }
+  }
+  
+  public String[][] getCacheZoneStrings(Locale locale)
+  {
+    String localeStr = locale.toString();
+    
+    if ("".equals(localeStr))
+      return getZoneStrings(locale);
+    
+    String keyStr = localeStr + "+zone_strings";
+    String[][] array = (String[][])cache.get(keyStr);
+    
+    if (array == null)
+      {
+       array = getZoneStrings(locale);
+       cache.put(keyStr, array);
+      }
+    
+    return array;
+  }
+  
+  public ResourceBundle getLocaleInformationBundle(Locale loc)
+  {
+    String localeStr = loc.toString();
+    
+    if ("".equals(localeStr))
+      return ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
+                                     loc, ClassLoader.getSystemClassLoader());
+    
+    ResourceBundle res;
+    
+    res = (ResourceBundle)cache.get(localeStr);
+    if (res == null)
+      {
+       res = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",     
                                      
+                                      loc, ClassLoader.getSystemClassLoader());
+       cache.put(localeStr, res);
+      }
+    
+    return res;
+  }
+  
+}
Index: java/text/DateFormatSymbols.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/text/DateFormatSymbols.java,v
retrieving revision 1.19
diff -u -r1.19 DateFormatSymbols.java
--- java/text/DateFormatSymbols.java    23 Jul 2005 20:25:15 -0000      1.19
+++ java/text/DateFormatSymbols.java    11 Dec 2005 13:19:25 -0000
@@ -41,6 +41,7 @@
 import java.util.Locale;
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
+import gnu.java.util.LocaleCache;
 
 /**
  * This class acts as container for locale specific date/time formatting
@@ -120,18 +121,17 @@
    */
   public DateFormatSymbols (Locale locale) throws MissingResourceException
   {
-    ResourceBundle res
-      = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation", locale,
-                                ClassLoader.getSystemClassLoader());
+      LocaleCache cache = LocaleCache.getCache();
+    ResourceBundle res = cache.getLocaleInformationBundle(locale);
 
-    ampms = getStringArray(res, "ampms");
-    eras = getStringArray(res, "eras");
+    ampms = cache.getCacheStringArray(locale, "ampms");
+    eras = cache.getCacheStringArray(locale, "eras");
     localPatternChars = res.getString("localPatternChars");
-    months = getStringArray(res, "months");
-    shortMonths = getStringArray(res, "shortMonths");
-    shortWeekdays = getStringArray(res, "shortWeekdays");
-    weekdays = getStringArray(res, "weekdays");
-    zoneStrings = getZoneStrings(res);
+    months = cache.getCacheStringArray(locale, "months");
+    shortMonths = cache.getCacheStringArray(locale, "shortMonths");
+    shortWeekdays = cache.getCacheStringArray(locale, "shortWeekdays");
+    weekdays = cache.getCacheStringArray(locale, "weekdays");
+    zoneStrings = cache.getCacheZoneStrings(locale);
     dateFormats = formatsForKey(res, "DateFormat");
     timeFormats = formatsForKey(res, "TimeFormat");
   }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]