From 355b2427118dbb94aee678019e5ef051bc01b802 Mon Sep 17 00:00:00 2001 From: Andrew John Hughes Date: Sun, 15 Jun 2008 21:33:56 +0000 Subject: 2008-06-15 Andrew John Hughes 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. --- gnu/java/lang/CPStringBuilder.java | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'gnu/java/lang/CPStringBuilder.java') 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 StringBuilder. This will * ensure that an expensive growing operation will not occur until either * minimumCapacity is reached or the array has been allocated. - * The buffer is grown to the larger of minimumCapacity and + * The buffer is grown to either minimumCapacity * 2, if + * the array has been allocated or the larger of minimumCapacity and * capacity() * 2 + 2, 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); } } -- cgit v1.2.1