RFR: 8333893: Optimization for StringBuilder append boolean & null [v4]

Shaojin Wen duke at openjdk.org
Wed Jun 12 06:19:15 UTC 2024


On Tue, 11 Jun 2024 11:35:28 GMT, Shaojin Wen <duke at openjdk.org> wrote:

>> After PR https://github.com/openjdk/jdk/pull/16245, C2 optimizes stores into primitive arrays by combining values ​​into larger stores.
>> 
>> This PR rewrites the code of appendNull and append(boolean) methods so that these two methods can be optimized by C2.
>
> Shaojin Wen has updated the pull request incrementally with one additional commit since the last revision:
> 
>   revert

In the case of isLatin1(), it can also be merged. I also found that false will merge the last four characters "alse" instead of the first four characters "fals".

below stand-alone reproducer:

* JavaCode

public class SBTraceStoreTest {
    static final StringBuilder buf = new StringBuilder(1024);

    static int append(boolean utf16, boolean appendNull) {
        buf.delete(0, buf.length());
        if (utf16) {
            buf.append('\u4e2d');
        }
        for (int i = 0; i < 10; i++) {
            if (appendNull) {
                buf.append((String) null)
                        .append((String) null);
            } else {
                buf.append(true)
                        .append(false);
            }
        }
        return buf.length();
    }

    public static void appendTest(boolean utf16, boolean appendNull) {
        for (int j = 0; j < 5; j++) {
            long start = System.currentTimeMillis();
            for (int i = 0; i < 100_000; ++i) {
                append(utf16, appendNull);
            }
            long millis = System.currentTimeMillis() - start;
            System.out.println("SBTraceStoreTest-" + (appendNull ? "appendNull" : "appendBool")
                    + (utf16 ? "UTF16" : "Latin1")
                    + " millis : " + millis);
        }
    }

    public static void main(String[] args) throws Exception {
        String test = System.getProperty("test");
        if (test == null) {
            test = "appendBoolUTF16";
        }
        switch (test) {
            case "appendNullLatin1":
                appendTest(false, true);
                break;
            case "appendNullUTF16":
                appendTest(true, true);
                break;
            case "appendBool":
                appendTest(false, false);
                appendTest(false, false);
                break;
            case "appendBoolLatin1":
                appendTest(false, false);
                break;
            case "appendBoolUTF16":
            default:
                appendTest(true, false);
                break;
        }
    }
}


* Produce

export JAVA_HOME=/Users/wenshao/Work/git/jdk/build/macosx-aarch64-server-fastdebug/jdk
javac SBTraceStoreTest.java
java -XX:+TraceMergeStores -Dtest=appendBoolLatin1 SBTraceStoreTest

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

PR Comment: https://git.openjdk.org/jdk/pull/19626#issuecomment-2162188635


More information about the core-libs-dev mailing list