8058779: Faster implementation of String.replace(CharSequence, CharSequence)

Xueming Shen xueming.shen at oracle.com
Sun May 31 04:34:10 UTC 2015


On 5/30/15 7:19 PM, Ivan Gerasimov wrote:
> Hi everyone!
>
> Here's another webrev, in which replace() is implemented with 
> StringBuilder.
> On my benchmark it is almost as fast as the version backed with 
> arrays, but this variant is much shorter.
>
> Credits to Sherman for combining the general algorithm with the case 
> of empty target.
>
> Comments, further suggestions are welcome!
>
> BUGURL: https://bugs.openjdk.java.net/browse/JDK-8058779
> WEBREV: http://cr.openjdk.java.net/~igerasim/8058779/04/webrev/
>
> Sincerely yours,
> Ivan
>

This is one is much better:-) I would suggest to leave the 
"overflow-conscious" code to the StringBuilder.
The same range check is being done inside ABS every time the repl string 
is appended into the buffer,
it does not appear to be very helpful to have a redundant check here. 
And it seems this check only works
for the single appearance of target string.

2260         // overflow-conscious code
2261         if (value.length - targLen > Integer.MAX_VALUE - replValue.length) {
2262             throw new OutOfMemoryError();
2263         }




More information about the core-libs-dev mailing list