8282429: StringBuilder/StringBuffer.toString() skip compressing for UTF16 strings

Reviewed-by: djelinski, redestad
This commit is contained in:
Xin Liu 2022-04-01 04:42:03 +00:00
parent 8eff80682a
commit bab431cc12
6 changed files with 122 additions and 11 deletions

View file

@ -68,6 +68,14 @@ abstract sealed class AbstractStringBuilder implements Appendable, CharSequence
*/
byte coder;
/**
* The attribute indicates {@code value} might be compressible to LATIN1 if it is UTF16-encoded.
* An inflated byte array becomes compressible only when those non-latin1 chars are deleted.
* We simply set this attribute in all methods which may delete chars. Therefore, there are
* false positives. Subclasses and String need to handle it properly.
*/
boolean maybeLatin1;
/**
* The count is the number of characters used.
*/
@ -132,10 +140,11 @@ abstract sealed class AbstractStringBuilder implements Appendable, CharSequence
final byte initCoder;
if (COMPACT_STRINGS) {
if (seq instanceof AbstractStringBuilder) {
initCoder = ((AbstractStringBuilder)seq).getCoder();
} else if (seq instanceof String) {
initCoder = ((String)seq).coder();
if (seq instanceof AbstractStringBuilder asb) {
initCoder = asb.getCoder();
maybeLatin1 |= asb.maybeLatin1;
} else if (seq instanceof String s) {
initCoder = s.coder();
} else {
initCoder = LATIN1;
}
@ -319,6 +328,8 @@ abstract sealed class AbstractStringBuilder implements Appendable, CharSequence
} else {
StringUTF16.fillNull(value, count, newLength);
}
} else if (count > newLength) {
maybeLatin1 = true;
}
count = newLength;
}
@ -528,6 +539,7 @@ abstract sealed class AbstractStringBuilder implements Appendable, CharSequence
inflate();
}
StringUTF16.putCharSB(value, index, ch);
maybeLatin1 = true;
}
}
@ -597,6 +609,7 @@ abstract sealed class AbstractStringBuilder implements Appendable, CharSequence
inflateIfNeededFor(asb);
asb.getBytes(value, count, coder);
count += len;
maybeLatin1 |= asb.maybeLatin1;
return this;
}
@ -907,6 +920,7 @@ abstract sealed class AbstractStringBuilder implements Appendable, CharSequence
if (len > 0) {
shift(end, -len);
this.count = count - len;
maybeLatin1 = true;
}
return this;
}
@ -958,6 +972,7 @@ abstract sealed class AbstractStringBuilder implements Appendable, CharSequence
checkIndex(index, count);
shift(index + 1, -1);
count--;
maybeLatin1 = true;
return this;
}
@ -992,6 +1007,7 @@ abstract sealed class AbstractStringBuilder implements Appendable, CharSequence
shift(end, newCount - count);
this.count = newCount;
putStringAt(start, str);
maybeLatin1 = true;
return this;
}