CharSequence.subSequence optimizations

Aleksey Shipilev aleksey.shipilev at
Thu Oct 29 00:48:54 UTC 2015

On 10/29/2015 02:35 AM, Pavel Rappo wrote:
> While working with an API that extensively uses java.lang.CharSequence, I've
> noticed most JDK implementations of CharSequence do not optimize for corner
> cases in CharSequence.subSequence(int start, int end). Namely,
>     1. start == end (an empty subsequence)

Yeah, returning a canonical "empty" sequence in this case is probably
okay. But, I have doubts about it, because you'd want to match the exact
implementation type, to keep the type profiles happy.

>     2. start == 0 && end == CharSequence.this.length() (the same sequence)

Nope. Doesn't that break when CharSequence implementation is mutable?
E.g. with StringBuilder, you cannot return "this" when "end ==
StringBuilder.length()" at the time of .subSequence() call.


More information about the core-libs-dev mailing list