Wonder if you should not move the test into StringLatin1.newString and StringUTF16.newString to return "" if subLen [len] == 0. Then there would be an across the board benefit.
> On Feb 25, 2020, at 11:43 AM, Сергей Цыпанов <sergei.tsypa...@yandex.ru> > wrote: > > Hello, > > current implementation of String.substring(int,int) has a fast-path for the > case when beginIndex = 0 and endIndex = length. > > I think there should be similar fast-path for the case beginIndex = endIndex > (asuming both are valid): > > diff --git a/src/java.base/share/classes/java/lang/String.java > b/src/java.base/share/classes/java/lang/String.java > --- a/src/java.base/share/classes/java/lang/String.java > +++ b/src/java.base/share/classes/java/lang/String.java > @@ -1900,10 +1900,13 @@ > public String substring(int beginIndex, int endIndex) { > int length = length(); > checkBoundsBeginEnd(beginIndex, endIndex, length); > - int subLen = endIndex - beginIndex; > if (beginIndex == 0 && endIndex == length) { > return this; > } > + int subLen = endIndex - beginIndex; > + if (subLen == 0) { > + return ""; > + } > return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen) > : StringUTF16.newString(value, beginIndex, subLen); > } > > With this call to str.substring(n,n) will be allocation-free. > > Regards, > Sergey Tsypanov