8224986: (str) optimize StringBuilder.append(CharSequence, int, int) for String arguments

Reviewed-by: jlaskey, rriggs
This commit is contained in:
Claes Redestad 2019-05-31 12:20:21 +02:00
parent 25b1131e33
commit fd7c38aa35
3 changed files with 138 additions and 6 deletions

View file

@ -26,6 +26,7 @@
package java.lang;
import jdk.internal.math.FloatingDecimal;
import java.util.Arrays;
import java.util.Spliterator;
import java.util.stream.IntStream;
@ -685,10 +686,15 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
checkRange(start, end, s.length());
int len = end - start;
ensureCapacityInternal(count + len);
appendChars(s, start, end);
if (s instanceof String) {
appendChars((String)s, start, end);
} else {
appendChars(s, start, end);
}
return this;
}
/**
* Appends the string representation of the {@code char} array
* argument to this sequence.
@ -1743,6 +1749,35 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
this.count = count + end - off;
}
private final void appendChars(String s, int off, int end) {
if (isLatin1()) {
if (s.isLatin1()) {
System.arraycopy(s.value(), off, this.value, this.count, end - off);
} else {
// We might need to inflate, but do it as late as possible since
// the range of characters we're copying might all be latin1
byte[] val = this.value;
for (int i = off, j = count; i < end; i++) {
char c = s.charAt(i);
if (StringLatin1.canEncode(c)) {
val[j++] = (byte) c;
} else {
count = j;
inflate();
System.arraycopy(s.value(), i << UTF16, this.value, j << UTF16, (end - i) << UTF16);
count += end - i;
return;
}
}
}
} else if (s.isLatin1()) {
StringUTF16.putCharsSB(this.value, this.count, s, off, end);
} else { // both UTF16
System.arraycopy(s.value(), off << UTF16, this.value, this.count << UTF16, (end - off) << UTF16);
}
count += end - off;
}
private final void appendChars(CharSequence s, int off, int end) {
if (isLatin1()) {
byte[] val = this.value;