RFR: 8355177: Speed up StringBuilder::append(char[]) via UTF16::compress & Unsafe::copyMemory

Shaojin Wen swen at openjdk.org
Mon Apr 21 10:18:35 UTC 2025


On Mon, 21 Apr 2025 07:00:36 GMT, Shaojin Wen <swen at openjdk.org> wrote:

> In BufferedReader.readLine and other similar scenarios, we need to use StringBuilder.append(char[]) to build the string.
> 
> For these scenarios, we can use the intrinsic method StringUTF16.compress and Unsafe.copyMemory instead of the character copy of the char-by-char loop to improve the speed.

Below are the performance numbers on a MacBookPro M1 Max, showing a 112% speed increase when coder = LATIN1 and a 44.19% speed increase when coder = UTF16.

# shell

git remote add wenshao git at github.com:wenshao/jdk.git
git fetch wenshao

# basline
git clone ee356d3af177877e2702db08a3b55d170a7e454c
make test TEST="micro:java.lang.StringBuilders.appendWithCharArray"

# current
git clone cd5137097b4a7be370cf60df9aa5000203ea99c0
make test TEST="micro:java.lang.StringBuilders.appendWithCharArray"


# Performance Numbrers

-Benchmark                                 Mode  Cnt   Score   Error  Units (baseline cd5137097b4)
-StringBuilders.appendWithCharArrayLatin1  avgt   15  33.039 ± 0.059  ns/op
-StringBuilders.appendWithCharArrayUTF16   avgt   15  19.977 ± 0.054  ns/op

+Benchmark                                 Mode  Cnt   Score   Error  Units (current ee356d3af17)
+StringBuilders.appendWithCharArrayLatin1  avgt   15  15.533 ± 0.039  ns/op +112.70%
+StringBuilders.appendWithCharArrayUTF16   avgt   15  13.868 ± 0.053  ns/op +44.19%

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

PR Comment: https://git.openjdk.org/jdk/pull/24773#issuecomment-2817873466


More information about the core-libs-dev mailing list