RFR [S] 8077242: (str) Optimize AbstractStringBuilder.append(CharSequence, int, int) for String argument

Ivan Gerasimov ivan.gerasimov at oracle.com
Wed Jul 1 21:18:33 UTC 2015

Thank you Martin for review!

On 01.07.2015 0:59, Martin Buchholz wrote:
> This looks good.
> Because we already have append(String) it *may* be a good idea to add 
> append(String, int, int).
I found only a dozen of the places in JDK where this method is 
applicable, so I thought it doesn't seem to worth a change in the API.

> See also my failure to add getChars to CharSequence itself.
> If we (you?) could make that happen, that would eliminate the need for 
> instanceof String etc.
Yes, it was my initial thought too:

I guess, it's not a problem to add default CharSequence.getChars(). The 
problem is to call it safely.
If there were a way to enforce all the derived classes except a few 
trusted stick to the default implementation, it would be possible to use 
getChars in sb.append().

For now, I limited the scope of the change, so that changing 
sb.append(str.substring(a, b)) to sb.append(str, a, b) will be at least 
no slower.

Sincerely yours,

> On Sat, Jun 27, 2015 at 7:23 AM, Ivan Gerasimov 
> <ivan.gerasimov at oracle.com <mailto:ivan.gerasimov at oracle.com>> wrote:
>     Hello!
>     AbstractStringBuilder, which is base for StringBuilder and
>     StringBuffer has a method for appending a sub-sequence of a
>     CharSequence.
>     Internally, it copies one char at a time in a loop, picking them
>     up with CharSequence.charAt() method.
>     For the case when the argument is a String, it can be done more
>     efficiently, by a call to String.getChars().
>     Since String is a final class, it should be safe to pass 'value'
>     to its method.
>     I've also found a few places in JDK where code can be done more
>     efficient, using this optimization.
>     Would you please help review this fix?
>     BUGURL: https://bugs.openjdk.java.net/browse/JDK-8077242
>     WEBREV: http://cr.openjdk.java.net/~igerasim/8077242/02/webrev/
>     <http://cr.openjdk.java.net/%7Eigerasim/8077242/02/webrev/>
>     Sincerely yours,
>     Ivan

More information about the core-libs-dev mailing list