summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew John Hughes <gnu_andrew@member.fsf.org>2008-06-15 21:33:56 +0000
committerAndrew John Hughes <gnu_andrew@member.fsf.org>2008-06-15 21:33:56 +0000
commit355b2427118dbb94aee678019e5ef051bc01b802 (patch)
treea827745b2a1ddc13b71977dc0ff33ada09891810
parent9adb2969647066922075a0deab27dcc02d9e53f9 (diff)
downloadclasspath-355b2427118dbb94aee678019e5ef051bc01b802.tar.gz
2008-06-15 Andrew John Hughes <gnu_andrew@member.fsf.org>
PR classpath/36477: * gnu/java/lang/CPStringBuilder.java, (setLength(int)): Don't ensure capacity when new length is 0. (ensureCapacity(int)): Allocate double the minimum capacity rather than double the array length when allocating a new array after a write.
-rw-r--r--ChangeLog10
-rw-r--r--gnu/java/lang/CPStringBuilder.java21
2 files changed, 25 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index fa8caa73f..1638505c5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2008-06-15 Andrew John Hughes <gnu_andrew@member.fsf.org>
+
+ PR classpath/36477:
+ * gnu/java/lang/CPStringBuilder.java,
+ (setLength(int)): Don't ensure capacity when
+ new length is 0.
+ (ensureCapacity(int)): Allocate double the minimum
+ capacity rather than double the array length when
+ allocating a new array after a write.
+
2008-06-15 Mario Torre <neugens@aicas.com>
* gnu/java/awt/java2d/RasterGraphics.java: remove unused import that cause
diff --git a/gnu/java/lang/CPStringBuilder.java b/gnu/java/lang/CPStringBuilder.java
index 688f36dbc..27e7d2cc7 100644
--- a/gnu/java/lang/CPStringBuilder.java
+++ b/gnu/java/lang/CPStringBuilder.java
@@ -204,8 +204,11 @@ public final class CPStringBuilder
int valueLength = value.length;
/* Always call ensureCapacity in order to preserve
- copy-on-write semantics. */
- ensureCapacity(newLength);
+ copy-on-write semantics, except when the position
+ is simply being reset
+ */
+ if (newLength > 0)
+ ensureCapacity(newLength);
if (newLength < valueLength)
{
@@ -1036,18 +1039,24 @@ public final class CPStringBuilder
* Increase the capacity of this <code>StringBuilder</code>. This will
* ensure that an expensive growing operation will not occur until either
* <code>minimumCapacity</code> is reached or the array has been allocated.
- * The buffer is grown to the larger of <code>minimumCapacity</code> and
+ * The buffer is grown to either <code>minimumCapacity * 2</code>, if
+ * the array has been allocated or the larger of <code>minimumCapacity</code> and
* <code>capacity() * 2 + 2</code>, if it is not already large enough.
*
* @param minimumCapacity the new capacity
- * @see #capacity()
+ * @see #length()
*/
public void ensureCapacity(int minimumCapacity)
{
if (allocated || minimumCapacity > value.length)
{
- int max = value.length * 2 + 2;
- minimumCapacity = (minimumCapacity < max ? max : minimumCapacity);
+ if (minimumCapacity > value.length)
+ {
+ int max = value.length * 2 + 2;
+ minimumCapacity = (minimumCapacity < max ? max : minimumCapacity);
+ }
+ else
+ minimumCapacity *= 2;
allocateArray(minimumCapacity);
}
}