String.contains(CharSequence) calls toString on argument

Claes Redestad claes.redestad at oracle.com
Fri Mar 20 11:34:06 UTC 2015


Hi!

While enabling use of CharSequence would seem desirable, there are
some drawbacks that needs to be kept in mind:

- String.indexOf(String) and friends are typically intrinsified on 
String in
ways that might not port over well to other implementations of
CharSequence - there might not even be a performance benefit in
avoiding the allocation for some cases

- Even if the intrinsic behavior can be retained, widening to take
CharSequence opens up for profile pollution, which might inadvertently
cause regressions in existing code

Adding new methods alongside ones taking String could arguably
avoid these pitfalls, so I'm not saying it's entirely futile, but it 
might be
hard to ensure and prove that it adds enough performance benefits to
be worth the effort and increased maintenance burden.

Thanks!

/Claes

On 03/20/2015 10:34 AM, Tomasz Kowalczewski wrote:
> Hello!
>
> Current implementation of String.contains that accepts CharSequence calls
> toString on it and passes resulting string to indexOf(String). This IMO
> defeats the purpose of using CharSequences (that is to have a mutable
> character buffer and not allocate unnecessary objects).
>
> Is changing this a desirable development? It seems pretty straightforward
> to port indexOf(String) to use CharSequence.
>
> If all you need is patch then I can work on it (I have signed OCA) just
> wanted to make sure it is not a futile work.
>




More information about the core-libs-dev mailing list