RFR: JDK-8302323 Add repeat methods to StringBuilder/StringBuffer [v2]
Jim Laskey
jlaskey at openjdk.org
Mon Feb 27 13:23:38 UTC 2023
On Sun, 26 Feb 2023 17:28:08 GMT, Tagir F. Valeev <tvaleev at openjdk.org> wrote:
>> Jim Laskey has updated the pull request incrementally with one additional commit since the last revision:
>>
>> PR Cleanup
>
> 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?
-------------
PR: https://git.openjdk.org/jdk/pull/12728
More information about the core-libs-dev
mailing list