RFR: 8273100: Improve AbstractStringBuilder.append(String) when using CompactStrings [v3]

Сергей Цыпанов github.com+10835776+stsypanov at openjdk.java.net
Mon Aug 30 13:38:29 UTC 2021


On Mon, 30 Aug 2021 11:49:49 GMT, Claes Redestad <redestad at openjdk.org> wrote:

>> Refactor to improve inlining, which helps some microbenchmarks exer StringBuilder.append(String)
>
> Claes Redestad has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Simplify and call getBytes(String, int, byte) when possible

src/java.base/share/classes/java/lang/AbstractStringBuilder.java line 1714:

> 1712: 
> 1713:     private void inflateIfNeededFor(String input) {
> 1714:         if (COMPACT_STRINGS && (coder != input.coder())) {

I'm not completely sure whether it's a good idea in terms of maintability, but I think this can be simplified a bit more. Currently in both `String` and `ASB` we have implementation of `coder()` very much alike:

// ASB
final byte getCoder() {
    return COMPACT_STRINGS ? coder : UTF16;
}

//String
byte getCoder() {
    return COMPACT_STRINGS ? coder : UTF16;
}

Here we have this condition

if (COMPACT_STRINGS && (coder != input.getCoder())) {}

where the right operand of `&&` is evaluated only when `COMPACT_STRINGS` is `true` and hence it always returns the value of `coder` field. This means we can reference it directly as

if (COMPACT_STRINGS && (coder != input.coder)) {}

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

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


More information about the core-libs-dev mailing list