classpath
[Top][All Lists]
Advanced

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

Re: Patch: remove redundant loop in StringBuffer


From: Anthony Green
Subject: Re: Patch: remove redundant loop in StringBuffer
Date: Tue, 24 Feb 2004 09:26:06 -0800

On Tue, 2004-02-24 at 08:25, Tom Tromey wrote:
> Suppose you do:

Oh, right - because we're growing the string, and not just the
underlying array.

Ok - here's a revised patch that eliminates the redundant loop in the
most common case.


2004-02-24  Anthony Green  <address@hidden>

        * java/lang/StringBuffer.java: No need to NULL out remainder of
        buffer since ensureCapacity_unsynchronized will have done this for
        us.


Index: java/lang/StringBuffer.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/StringBuffer.java,v
retrieving revision 1.25
diff -c -p -r1.25 StringBuffer.java
*** java/lang/StringBuffer.java 18 Oct 2003 08:45:37 -0000      1.25
--- java/lang/StringBuffer.java 24 Feb 2004 17:20:43 -0000
***************
*** 1,5 ****
  /* StringBuffer.java -- Growable strings
!    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, 
Inc.
  
  This file is part of GNU Classpath.
  
--- 1,6 ----
  /* StringBuffer.java -- Growable strings
!    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
!    Free Software Foundation, Inc.
  
  This file is part of GNU Classpath.
  
*************** public final class StringBuffer implemen
*** 205,214 ****
      if (newLength < 0)
        throw new StringIndexOutOfBoundsException(newLength);
  
      ensureCapacity_unsynchronized(newLength);
!     while (count < newLength)
!       value[count++] = '\0';
!     count = newLength;
    }
  
    /**
--- 206,231 ----
      if (newLength < 0)
        throw new StringIndexOutOfBoundsException(newLength);
  
+     int valueLength = value.length;
+ 
+     /* Always call ensureCapacity_unsynchronized in order to preserve
+        copy-on-write semantics.  */
      ensureCapacity_unsynchronized(newLength);
! 
!     if (newLength < valueLength)
!       {
!         /* If the StringBuffer's value just grew, then we know that
!            value is newly allocated and the region between count and
!            newLength is filled with '\0'.  */
!       count = newLength;
!       }
!     else
!       {
!       /* The StringBuffer's value didn't grow.  However,
!          we should clear out any cruft that may exist.  */
!       while (count < newLength)
!           value[count++] = '\0';
!       }
    }
  
    /**


-- 
Anthony Green <address@hidden>
Red Hat, Inc.





reply via email to

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