RFR: 8254807: Optimize startsWith() for String.substring()

Xin Liu xliu at openjdk.java.net
Mon Nov 2 06:49:55 UTC 2020


On Sat, 31 Oct 2020 14:48:46 GMT, Claes Redestad <redestad at openjdk.org> wrote:

>> The optimization transforms code from s=substring(base, beg, end); s.startsWith(prefix)
>> to substring(base, beg, end) | base.startsWith(prefix, beg). 
>> 
>> it reduces uses of substring. hopefully c2 optimizer can remove the used substring.
>
> test/micro/org/openjdk/bench/vm/compiler/SubstringAndStartsWith.java line 45:
> 
>> 43: @State(Scope.Benchmark)
>> 44: public class SubstringAndStartsWith {
>> 45:     @Param({"1", "8", "32", "128", "256", "512"})
> 
> Does each param value pull its weight? I'd consider cutting down the default list to 2-3 variants (you can always specify more values on the command line et.c)

to be honest, I don't know the average length of substring in Java.   I add so many parameters to show this kind of pattern allocates and copies strings with  O(n) complexity, which n is length of substring.

to take your advice, I think I can model them in 3 representative lengths. how about this?
1.  small 4  like  a variable name
2.  medium size = 24 like a url or a filepath 
3. long string = 256   like a human-readable message

-------------

PR: https://git.openjdk.java.net/jdk/pull/974


More information about the hotspot-compiler-dev mailing list