On Mon, 27 Feb 2023 13:20:48 GMT, Jim Laskey <jlas...@openjdk.org> wrote:
>> src/java.base/share/classes/java/lang/AbstractStringBuilder.java line 1903: >> >>> 1901: int total = count * length; >>> 1902: int limit = offset + total; >>> 1903: ensureCapacityInternal(limit); >> >> If I understand correctly, this may cause double reallocation, if inflation >> is necessary (string to spend is utf16 while current coder is latin1). Can >> this be optimized, at least for String/ASB inputs? Sorry if I'm missing >> something. > > Inflation of a buffer can only happen once. Adding the first copy (append) > causing expansion (reallocatio ) and then expanding again to handle the > remaining copies is something that happens frequently. Better to expand once > for all cases and take the one time hit for transition from Latin1 to UTF16. > Make sense? I see. Well it's possible to create `ensureCapacityAndCoderInternal(capacity, coder)`, which handles both at once. It can be useful in other places as well. Probably this should be considered as a separate enhancement, outside of this PR. ------------- PR: https://git.openjdk.org/jdk/pull/12728