RFR: 8270160: Remove redundant bounds check from AbstractStringBuilder.charAt()

Сергей Цыпанов github.com+10835776+stsypanov at openjdk.java.net
Fri Jul 9 11:58:03 UTC 2021


`AbstractStringBuilder.charAt(int)` does bounds check before calling `charAt()` (for non-latin Strings):

@Override
public char charAt(int index) {
    checkIndex(index, count);
    if (isLatin1()) {
        return (char)(value[index] & 0xff);
    }
    return StringUTF16.charAt(value, index);
}

This can be improved by removing bounds check from ASB.charAt() in favour of one in String*.charAt(). This gives slight improvement:

before
Benchmark                           Mode  Cnt  Score   Error  Units
StringBuilderCharAtBenchmark.latin  avgt   50  2,768 ± 0,004  ns/op
StringBuilderCharAtBenchmark.utf    avgt   50  2,778 ± 0,014  ns/op

after
Benchmark                           Mode  Cnt  Score   Error  Units
StringBuilderCharAtBenchmark.latin  avgt   50  2,434 ± 0,004  ns/op
StringBuilderCharAtBenchmark.utf    avgt   50  2,631 ± 0,004  ns/op

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

Commit messages:
 - 8270160 Remove redundant bounds check from AbstractStringBuilder.charAt()

Changes: https://git.openjdk.java.net/jdk/pull/4738/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=4738&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8270160
  Stats: 10 lines in 2 files changed: 1 ins; 5 del; 4 mod
  Patch: https://git.openjdk.java.net/jdk/pull/4738.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/4738/head:pull/4738

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


More information about the core-libs-dev mailing list