RFR: 8357289: Break down the String constructor into smaller methods [v6]

Chen Liang liach at openjdk.org
Mon May 26 14:58:00 UTC 2025


On Mon, 26 May 2025 13:23:07 GMT, Shaojin Wen <swen at openjdk.org> wrote:

>> Through JVM Option +PrintInlining, we found that String has a constructor codeSize of 852, which is too large. This caused failed to inline.
>> 
>> The following is the output information of PrintInlining:
>> 
>>                 @ 9   java.lang.String::<init> (12 bytes)   inline (hot)
>> !m                 @ 1   java.nio.charset.Charset::defaultCharset (52 bytes)   inline (hot)
>> !                  @ 8   java.lang.String::<init> (852 bytes)   failed to inline: hot method too big
>> 
>> 
>> In Java code, the big method that cannot be inlined is the following constructor
>> 
>> 
>> String(Charset charset, byte[] bytes, int offset, int length) {}
>> 
>> The above String constructor is too large; break it down into smaller methods with a codeSize under 325 to allow them to be inlined by the C2.
>
> Shaojin Wen has updated the pull request incrementally with one additional commit since the last revision:
> 
>   from @liach

src/java.base/share/classes/java/lang/String.java line 661:

> 659:             // ascii
> 660:             if (ad.isASCIICompatible() && !StringCoding.hasNegatives(bytes, offset, length)) {
> 661:                 if (COMPACT_STRINGS) {

Replace this with `return iso88591(bytes, offset, length)`;

src/java.base/share/classes/java/lang/String.java line 673:

> 671:                     value = new byte[length];
> 672:                     ad.decodeToLatin1(bytes, offset, length, value);
> 673:                     coder = LATIN1;

With the other optimizations, you can make this `return new String(value, LATIN1)` and remove the `value` and `coder` local variable declarations.

src/java.base/share/classes/java/lang/String.java line 680:

> 678:                     char[] ca = new char[en];
> 679:                     int clen = ad.decode(bytes, offset, length, ca);
> 680:                     if (COMPACT_STRINGS) {

This can be `return new String(ca, 0, cLen, null);` too,

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

PR Review Comment: https://git.openjdk.org/jdk/pull/25290#discussion_r2107508429
PR Review Comment: https://git.openjdk.org/jdk/pull/25290#discussion_r2107510678
PR Review Comment: https://git.openjdk.org/jdk/pull/25290#discussion_r2107506207


More information about the core-libs-dev mailing list