classpath-patches
[Top][All Lists]
Advanced

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

Re: [cp-patches] Patch: use StringBuilder for reading/writing property f


From: Mark Wielaard
Subject: Re: [cp-patches] Patch: use StringBuilder for reading/writing property files
Date: Sat, 12 Nov 2005 03:23:50 +0100

On Wed, 2005-11-02 at 11:31 +0100, Christian Thalinger wrote:
> On Tue, 2005-11-01 at 22:16 +0100, Mark Wielaard wrote:
> > That seems like an significant improvement. Could you test with a larger
> > dataset (or just run the test in a loop 10 times)? I still haven't tried
> > with gcj. If someone could test, that would be super. I believe gcj has
> > really slow monitor enter and exit support so the speedup should be
> > significant there. But without testing...
> 
> In a 10 times loop (fastest of 3 runs):
> 
> w/o: 13.92user
> w/:  10.14user

And I finally tested against gcj.
Before: 0m3.765s
After:  0m2.370s
(Best of 3 for reading all classpath property files 4 times)

So this seems like a nice optimization and I am checking it in as:

2005-11-11  Mark Wielaard  <address@hidden>
            Anthony Green  <address@hidden>

    * java/util/Properties.java (load): Short-circuit parsing when key or
    value doesn't contain escape character. Use StringBuilder instead of
    StringBuffer.
    (store): Use StringBuilder instead of StringBuffer.
    (formatForOutput): Likewise.

Cheers,

Mark
Index: java/util/Properties.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/util/Properties.java,v
retrieving revision 1.34
diff -u -r1.34 Properties.java
--- java/util/Properties.java   23 Oct 2005 21:56:31 -0000      1.34
+++ java/util/Properties.java   30 Oct 2005 11:09:05 -0000
@@ -219,12 +219,15 @@
 
         // The characters up to the next Whitespace, ':', or '='
         // describe the key.  But look for escape sequences.
-        StringBuffer key = new StringBuffer();
+       // Try to short-circuit when there is no escape char.
+       int start = pos;
+       boolean needsEscape = line.indexOf('\\', pos) != -1;
+        StringBuilder key = needsEscape ? new StringBuilder() : null;
         while (pos < line.length()
                && ! Character.isWhitespace(c = line.charAt(pos++))
                && c != '=' && c != ':')
           {
-            if (c == '\\')
+            if (needsEscape && c == '\\')
               {
                 if (pos == line.length())
                   {
@@ -268,11 +271,20 @@
                       }
                   }
               }
-            else
+            else if (needsEscape)
               key.append(c);
           }
 
         boolean isDelim = (c == ':' || c == '=');
+
+       String keyString;
+       if (needsEscape)
+         keyString = key.toString();
+       else if (isDelim || Character.isWhitespace(c))
+         keyString = line.substring(start, pos - 1);
+       else
+         keyString = line.substring(start, pos);
+
         while (pos < line.length()
                && Character.isWhitespace(c = line.charAt(pos)))
           pos++;
@@ -285,7 +297,15 @@
               pos++;
           }
 
-        StringBuffer element = new StringBuffer(line.length() - pos);
+       // Short-circuit if no escape chars found.
+       if (!needsEscape)
+         {
+           put(keyString, line.substring(pos));
+           continue;
+         }
+
+       // Escape char found so iterate through the rest of the line.
+        StringBuilder element = new StringBuilder(line.length() - pos);
         while (pos < line.length())
           {
             c = line.charAt(pos++);
@@ -341,7 +361,7 @@
             else
               element.append(c);
           }
-        put(key.toString(), element.toString());
+        put(keyString, element.toString());
       }
   }
 
@@ -405,7 +425,7 @@
     
     Iterator iter = entrySet ().iterator ();
     int i = size ();
-    StringBuffer s = new StringBuffer (); // Reuse the same buffer.
+    StringBuilder s = new StringBuilder (); // Reuse the same buffer.
     while (--i >= 0)
       {
         Map.Entry entry = (Map.Entry) iter.next ();
@@ -548,7 +568,7 @@
    *        leading spaces must be escaped for the value
    * @see #store(OutputStream, String)
    */
-  private void formatForOutput(String str, StringBuffer buffer, boolean key)
+  private void formatForOutput(String str, StringBuilder buffer, boolean key)
   {
     if (key)
       {

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

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