8305811: (bf) Improve performance of CharBuffer::append(CharSequence[,int,int])

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2023-04-17 20:17:23 +00:00
parent 525a91e3fa
commit 8858d54342
3 changed files with 205 additions and 1 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -278,6 +278,60 @@ class Heap$Type$Buffer$RW$
#if[char]
//
// Use getChars() to load chars directly into the heap buffer array.
// For a String or StringBuffer source this improves performance if
// a proper subsequence is being appended as copying to a new intermediate
// String object is avoided. For a StringBuilder where either a subsequence
// or the full sequence of chars is being appended, copying the chars to
// an intermedite String in StringBuilder::toString is avoided.
//
private $Type$Buffer appendChars(CharSequence csq, int start, int end) {
checkSession();
int length = end - start;
int pos = position();
int lim = limit();
int rem = (pos <= lim) ? lim - pos : 0;
if (length > rem)
throw new BufferOverflowException();
if (csq instanceof String str) {
str.getChars(start, end, hb, ix(pos));
} else if (csq instanceof StringBuilder sb) {
sb.getChars(start, end, hb, ix(pos));
} else if (csq instanceof StringBuffer sb) {
sb.getChars(start, end, hb, ix(pos));
}
position(pos + length);
return this;
}
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 $Type$Buffer put(String src, int start, int end) {
#if[rw]
checkSession();