8306374: (bf) Improve performance of DirectCharBuffer::append(CharSequence[,int,int])

Reviewed-by: liach, alanb
This commit is contained in:
Brian Burkhalter 2023-04-26 16:37:18 +00:00
parent a18191fee8
commit d0e8aec041
6 changed files with 142 additions and 19 deletions

View file

@ -446,6 +446,69 @@ class Direct$Type$Buffer$RW$$BO$
// --- Methods to support CharSequence ---
#if[rw]
private static final int APPEND_BUF_SIZE = 1024;
private $Type$Buffer appendChars(CharSequence csq, int start, int end) {
Objects.checkFromToIndex(start, end, csq.length());
int pos = position();
int lim = limit();
int rem = (pos <= lim) ? lim - pos : 0;
int length = end - start;
if (length > rem)
throw new BufferOverflowException();
char[] buf = new char[Math.min(APPEND_BUF_SIZE, length)];
int index = pos;
while (start < end) {
int count = end - start;
if (count > buf.length)
count = buf.length;
if (csq instanceof String str) {
str.getChars(start, start + count, buf, 0);
} else if (csq instanceof StringBuilder sb) {
sb.getChars(start, start + count, buf, 0);
} else if (csq instanceof StringBuffer sb) {
sb.getChars(start, start + count, buf, 0);
}
putArray(index, buf, 0, count);
start += count;
index += count;
}
position(pos + length);
return this;
}
#end[rw]
public $Type$Buffer append(CharSequence csq) {
#if[rw]
if (csq instanceof StringBuilder)
return appendChars(csq, 0, csq.length());
return super.append(csq);
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
public $Type$Buffer append(CharSequence csq, int start, int end) {
#if[rw]
if (csq instanceof String || csq instanceof StringBuffer ||
csq instanceof StringBuilder)
return appendChars(csq, start, end);
return super.append(csq, start, end);
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
public CharBuffer subSequence(int start, int end) {
int pos = position();
int lim = limit();

View file

@ -1319,7 +1319,7 @@ public abstract sealed class $Type$Buffer
return put(index, src, 0, src.length);
}
private $Type$Buffer putArray(int index, $type$[] src, int offset, int length) {
$Type$Buffer putArray(int index, $type$[] src, int offset, int length) {
#if[rw]
if (
#if[char]