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

Claes Redestad redestad at openjdk.java.net
Sat Oct 31 15:01:54 UTC 2020


On Sat, 31 Oct 2020 05:22:39 GMT, Xin Liu <xliu 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.

Some comments and nits on the microbenchmark.

A general comment is that I think it would be good to add variants exercising UTF16 Strings: one where `sample` has some UTF-16 chars, and one where both `sample` and `prefix` do (latin-1 `sample` and UTF-16 `prefix` could be interesting too, to ensure this variant shortcuts quickly).

Should the `prefix` be something a bit more complex than a single char string? `startsWith("a", off)` is a case that'd be tempting to optimize down to `charAt(off) == 'a'` and then this micro might no longer do what it intends to do.

test/micro/org/openjdk/bench/vm/compiler/SubstringAndStartsWith.java line 44:

> 42: @Measurement(iterations = 20, time = 200, timeUnit = TimeUnit.MILLISECONDS)
> 43: @State(Scope.Benchmark)
> 44: public class SubstringAndStartsWith {

I'd put this micro in org.openjdk.bench.java.lang and call it SubstringStartsWith

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)

test/micro/org/openjdk/bench/vm/compiler/SubstringAndStartsWith.java line 68:

> 66:         // compare prefix length with the length of substring
> 67:         if (prefix.length() > substrLength) return false;
> 68:         return sample.startsWith(prefix, substrLength); // substrLength here is actually the beginIdex of substring

Suggestion:

        return sample.startsWith(prefix, substrLength); // substrLength here is actually the beginIndex of substring

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

Changes requested by redestad (Reviewer).

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


More information about the hotspot-compiler-dev mailing list