[TRIVIAL] Fast-path for String.subsring(n,n)

Claes Redestad claes.redestad at oracle.com
Wed Feb 26 13:19:26 UTC 2020


On 2020-02-26 11:01, Сергей Цыпанов wrote:
> Currently we have
> 
> public static String stripLeading(byte[] value) {
>    int left = indexOfNonWhitespace(value);
>    if (left == value.length) {
>      return "";
>    }
>    return (left != 0) ? newString(value, left, value.length - left) : null;
> }
> 
> With the patch we change behaviour of this method for the case when value.length == 0:
> 
> public static String stripLeading(byte[] value) {
>    int left = indexOfNonWhitespace(value);
>    return (left != 0) ? newString(value, left, value.length - left) : null;
> }
> 
> Unlike original method this code returns null instead of "" for empty array.
> This does not affect caller (String.stripLeading()) i. e. visible behaviour
> remains the same for String user, but is it OK in general?

One observable difference on the public API I think will happen here is
that new String("").stripLeading() == "" will change from true to false,
since the null return from the inner method mean we return the argument.

 From a compatibility point of view I think this should be fine, as
the identity of the returned empty string isn't specified. I don't think
a CSR is required, but bringing it up since others think otherwise.

Overall I like how the patch now cleans things up a bit on top of
(likely) optimizing a few edge cases, and can volunteer to sponsor it if
no one else has spoken up already.

Doing some quick performance testing and possibly adding a
microbenchmark before push would be good.

Thanks!

/Claes


More information about the core-libs-dev mailing list