classpath
[Top][All Lists]
Advanced

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

ResourceBundle optimisation


From: Nicolas Geoffray
Subject: ResourceBundle optimisation
Date: Wed, 06 Jul 2005 17:42:06 +0200
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050509)

Hi everyone,

I've been doing benchmarks beween my vm and another one launching Tomcat and noticed that the startup spent a long time trying to find Resouces with getBundle. The thing is, ResourceBundle uses a cache for the resources it founds, but i think it's not perfectly used.

When getBundle is entered, it first tries to find the resource in the cache. If it's not found, it calls tryBundle. Then tryBundle tries to load ressources with different locales, but never searches in the cache, allthough it could.

I made a patch for it and gained 2 minutes during startup of tomcat (using an ibook 500Mhz). It's attached.


Nicolas
--- java/util/ResourceBundle.java       2005-01-07 00:32:28.000000000 +0100
+++ java/util/ResourceBundle.java       2005-06-29 17:19:43.101862656 +0200
@@ -287,23 +287,21 @@
   private static class BundleKey
   {
     String baseName;
-    Locale locale;
     ClassLoader classLoader;
     int hashcode;
 
     BundleKey() {}
 
-    BundleKey(String s, Locale l, ClassLoader cl)
+    BundleKey(String s, ClassLoader cl)
     {
-      set(s, l, cl);
+      set(s, cl);
     }
     
-    void set(String s, Locale l, ClassLoader cl)
+    void set(String s, ClassLoader cl)
     {
       baseName = s;
-      locale = l;
       classLoader = cl;
-      hashcode = baseName.hashCode() ^ locale.hashCode() ^
+      hashcode = baseName.hashCode() ^
         classLoader.hashCode();
     }
     
@@ -319,7 +317,6 @@
       BundleKey key = (BundleKey) o;
       return hashcode == key.hashcode &&
        baseName.equals(key.baseName) &&
-        locale.equals(key.locale) &&
        classLoader.equals(key.classLoader);
     }    
   }
@@ -419,22 +416,6 @@
        lastDefaultLocale = defaultLocale;
       }
 
-    // This will throw NullPointerException if any arguments are null.
-    lookupKey.set(baseName, locale, classLoader);
-    
-    Object obj = bundleCache.get(lookupKey);
-    ResourceBundle rb = null;
-    
-    if (obj instanceof ResourceBundle)
-      {
-        return (ResourceBundle) obj;
-      }
-    else if (obj == nullEntry)
-      {
-        // Lookup has failed previously. Fall through.
-      }
-    else
-      {
        // First, look for a bundle for the specified locale. We don't want
        // the base bundle this time.
        boolean wantBase = locale.equals(defaultLocale);
@@ -445,22 +426,10 @@
        if (bundle == null && !locale.equals(defaultLocale))
          bundle = tryBundle(baseName, defaultLocale, classLoader, true);
 
-       BundleKey key = new BundleKey(baseName, locale, classLoader);
-        if (bundle == null)
-         {
-           // Cache the fact that this lookup has previously failed.
-           bundleCache.put(key, nullEntry);
-         }
-       else
-         {
-            // Cache the result and return it.
-           bundleCache.put(key, bundle);
-           return bundle;
-         }
-      }
+      if(bundle == null)
+        throw new MissingResourceException("Bundle " + baseName + " not 
found", baseName, "");
 
-    throw new MissingResourceException("Bundle " + baseName + " not found",
-                                      baseName, "");
+      return bundle;
   }
 
   /**
@@ -527,9 +496,10 @@
              is = ClassLoader.getSystemResourceAsStream(resourceName);
            else
              is = classloader.getResourceAsStream(resourceName);
-           if (is != null)
+        if (is != null){
              bundle = new PropertyResourceBundle(is);
          }
+      }
        catch (IOException ex)
          {
            MissingResourceException mre = new MissingResourceException
@@ -593,12 +563,32 @@
     String bundleName = sb.toString();
     ResourceBundle first = null; // The most specialized bundle.
     ResourceBundle last = null; // The least specialized bundle.
-    
+    ResourceBundle foundBundle = null;
     while (true)
       {
-        ResourceBundle foundBundle = tryBundle(bundleName, classLoader);
+
+      lookupKey.set(bundleName, classLoader);
+
+      Object obj = bundleCache.get(lookupKey);
+
+      if (obj instanceof ResourceBundle)
+      {
+        foundBundle =  (ResourceBundle) obj;
+      }
+      else if (obj == nullEntry)
+      {
+        foundBundle = null;
+      }
+      else{
+        foundBundle = tryBundle(bundleName, classLoader);
+      }
+
+      BundleKey key = new BundleKey(bundleName, classLoader);
        if (foundBundle != null)
          {
+
+        bundleCache.put(key, foundBundle);
+
            if (first == null)
              first = foundBundle;
            if (last != null)
@@ -606,6 +596,10 @@
            foundBundle.locale = locale;
            last = foundBundle;
          }
+      else
+        // Cache the fact that this lookup has previously failed.
+        bundleCache.put(key, nullEntry);
+
        int idx = bundleName.lastIndexOf('_');
        // Try the non-localized base name only if we already have a
        // localized child bundle, or wantBase is true.

reply via email to

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