AppendableCharSequence not to depend upon IndexOutOfBoundsException for resize
Motivation: AppendableCharSequence depends upon IndexOutOfBoundsException to trigger a resize operation under the assumption that the resize operation will be rare if the initial size guess is good. However if the initial size guess is not good then the performance will be more unpredictable and likely suffer. Modifications: - Check the position in AppendableCharSequence#append to determine if a resize is necessary Result: More predictable performance in AppendableCharSequence#append.
This commit is contained in:
parent
051e0ad4be
commit
1cc4607f07
@ -68,12 +68,12 @@ public final class AppendableCharSequence implements CharSequence, Appendable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AppendableCharSequence append(char c) {
|
public AppendableCharSequence append(char c) {
|
||||||
try {
|
if (pos == chars.length) {
|
||||||
chars[pos++] = c;
|
char[] old = chars;
|
||||||
} catch (IndexOutOfBoundsException e) {
|
chars = new char[old.length << 1];
|
||||||
expand();
|
System.arraycopy(old, 0, chars, 0, old.length);
|
||||||
chars[pos - 1] = c;
|
|
||||||
}
|
}
|
||||||
|
chars[pos++] = c;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,17 +139,6 @@ public final class AppendableCharSequence implements CharSequence, Appendable {
|
|||||||
return new String(chars, start, end - start);
|
return new String(chars, start, end - start);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void expand() {
|
|
||||||
char[] old = chars;
|
|
||||||
// double it
|
|
||||||
int len = old.length << 1;
|
|
||||||
if (len < 0) {
|
|
||||||
throw new IllegalStateException();
|
|
||||||
}
|
|
||||||
chars = new char[len];
|
|
||||||
System.arraycopy(old, 0, chars, 0, old.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static char[] expand(char[] array, int neededSpace, int size) {
|
private static char[] expand(char[] array, int neededSpace, int size) {
|
||||||
int newCapacity = array.length;
|
int newCapacity = array.length;
|
||||||
do {
|
do {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user