RFR: JDK-8302323 Add repeat methods to StringBuilder/StringBuffer [v2]
Tagir F. Valeev
tvaleev at openjdk.org
Tue Feb 28 07:48:10 UTC 2023
On Mon, 27 Feb 2023 13:20:48 GMT, Jim Laskey <jlaskey at 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
More information about the core-libs-dev
mailing list