From bdf2ced9051950168dda357983e58f234363ef28 Mon Sep 17 00:00:00 2001 From: Anthony Green Date: Sun, 9 Apr 2000 04:52:43 +0000 Subject: StringBuffer.java (ensureCapacity): Don't call Math::max. 2000-04-08 Anthony Green * java/lang/StringBuffer.java (ensureCapacity): Don't call Math::max. (ensureCapacity_unsynchronized): New private method. (append): Use ensureCapacity_unsynchronized. From-SVN: r33036 --- libjava/ChangeLog | 6 ++++++ libjava/java/lang/StringBuffer.java | 38 ++++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 9 deletions(-) (limited to 'libjava') diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 9040d6d..e0a3a74 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,9 @@ +2000-04-08 Anthony Green + + * java/lang/StringBuffer.java (ensureCapacity): Don't call Math::max. + (ensureCapacity_unsynchronized): New private method. + (append): Use ensureCapacity_unsynchronized. + 2000-04-08 Tom Tromey * Makefile.in: Rebuilt. diff --git a/libjava/java/lang/StringBuffer.java b/libjava/java/lang/StringBuffer.java index 7f6e42e..4034e79 100644 --- a/libjava/java/lang/StringBuffer.java +++ b/libjava/java/lang/StringBuffer.java @@ -28,7 +28,7 @@ public final class StringBuffer implements Serializable public synchronized StringBuffer append (char ch) { - ensureCapacity (count + 1); + ensureCapacity_unsynchronized (count + 1); value[count++] = ch; return this; } @@ -63,7 +63,7 @@ public final class StringBuffer implements Serializable if (str == null) str = "null"; int len = str.length(); - ensureCapacity (count + len); + ensureCapacity_unsynchronized (count + len); str.getChars(0, len, value, count); count += len; return this; @@ -76,7 +76,7 @@ public final class StringBuffer implements Serializable public synchronized StringBuffer append (char[] data, int offset, int count) { - ensureCapacity (this.count + count); + ensureCapacity_unsynchronized (this.count + count); System.arraycopy(data, offset, value, this.count, count); this.count += count; return this; @@ -104,7 +104,27 @@ public final class StringBuffer implements Serializable int max = (minimumCapacity > value.length ? value.length*2+2 : value.length); - minimumCapacity = Math.max(minimumCapacity, max); + minimumCapacity = (minimumCapacity < max ? max : minimumCapacity); + char[] nb = new char[minimumCapacity]; + System.arraycopy(value, 0, nb, 0, count); + value = nb; + shared = false; + } + } + + // ensureCapacity is used by several synchronized methods in StringBuffer. + // There's no need to synchronize again. + private void ensureCapacity_unsynchronized (int minimumCapacity) + { + if (shared || minimumCapacity > value.length) + { + // We don't want to make a larger vector when `shared' is + // set. If we do, then setLength becomes very inefficient + // when repeatedly reusing a StringBuffer in a loop. + int max = (minimumCapacity > value.length + ? value.length*2+2 + : value.length); + minimumCapacity = (minimumCapacity < max ? max : minimumCapacity); char[] nb = new char[minimumCapacity]; System.arraycopy(value, 0, nb, 0, count); value = nb; @@ -132,7 +152,7 @@ public final class StringBuffer implements Serializable { if (offset < 0 || offset > count) throw new StringIndexOutOfBoundsException (offset); - ensureCapacity (count+1); + ensureCapacity_unsynchronized (count+1); System.arraycopy(value, offset, value, offset+1, count-offset); value[offset] = ch; count++; @@ -172,7 +192,7 @@ public final class StringBuffer implements Serializable if (str == null) str = "null"; int len = str.length(); - ensureCapacity(count+len); + ensureCapacity_unsynchronized (count+len); System.arraycopy(value, offset, value, offset+len, count-offset); str.getChars(0, len, value, offset); count += len; @@ -184,7 +204,7 @@ public final class StringBuffer implements Serializable if (offset < 0 || offset > count) throw new StringIndexOutOfBoundsException (offset); int len = data.length; - ensureCapacity (count+len); + ensureCapacity_unsynchronized (count+len); System.arraycopy(value, offset, value, offset+len, count-offset); System.arraycopy(data, 0, value, offset, len); count += len; @@ -212,7 +232,7 @@ public final class StringBuffer implements Serializable if (index < 0 || index >= count) throw new StringIndexOutOfBoundsException (index); // Call ensureCapacity to enforce copy-on-write. - ensureCapacity (count); + ensureCapacity_unsynchronized (count); value[index] = ch; } @@ -221,7 +241,7 @@ public final class StringBuffer implements Serializable if (newLength < 0) throw new StringIndexOutOfBoundsException (newLength); - ensureCapacity (newLength); + ensureCapacity_unsynchronized (newLength); for (int i = count; i < newLength; ++i) value[i] = '\0'; count = newLength; -- cgit v1.1