On Tue, 27 Aug 2024 11:52:38 GMT, Shaojin Wen <s...@openjdk.org> wrote:

>> The string concatenation of java.base module is implemented based on 
>> StringBuilder, which will result in extra object allocation and slow 
>> performance. We can solve this problem by using String.concat method and 
>> StringConcatHelper to provide concat method.
>> 
>> for example, 
>> 
>> * use "+"
>> 
>> class ClassDesc {
>>     default String displayName() {
>>         c.displayName() + "[]".repeat(depth)
>>     }
>> }
>> 
>> 
>> bytecode:
>> 
>> 106: new           #40                 // class java/lang/StringBuilder
>> 109: dup
>> 110: invokespecial #42                 // Method 
>> java/lang/StringBuilder."<init>":()V
>> 113: aload_2
>> 114: invokeinterface #195,  1          // InterfaceMethod 
>> displayName:()Ljava/lang/String;
>> 119: invokevirtual #50                 // Method 
>> java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
>> 122: ldc           #198                // String []
>> 124: iload_1
>> 125: invokevirtual #200                // Method 
>> java/lang/String.repeat:(I)Ljava/lang/String;
>> 128: invokevirtual #50                 // Method 
>> java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
>> 131: invokevirtual #53                 // Method 
>> java/lang/StringBuilder.toString:()Ljava/lang/String;
>> 134: areturn
>> 
>> 
>> * use String#concat
>> 
>> c.displayName().concat("[]".repeat(depth))
>> 
>> 
>> bytecode:
>> 
>> 112: ldc           #198                // String []
>> 114: iload_1
>> 115: invokevirtual #200                // Method 
>> java/lang/String.repeat:(I)Ljava/lang/String;
>> 118: invokevirtual #86                 // Method 
>> java/lang/String.concat:(Ljava/lang/String;)Ljava/lang/String;
>
> Shaojin Wen has updated the pull request incrementally with three additional 
> commits since the last revision:
> 
>  - more concat
>  - Suggestions from @ExE-Boss
>  - concat Object value

Tier 1-3 tests look good.

For context, some core library developers are currently investigating if we can 
mark some classes and methods as used in early startup, after this problem was 
raised in OpenJDK committer's workshop (and encountered in the generics 
experiment work shown in JVMLS 2024); those methods can fail fast if they use 
lambdas, and we can make compiler generate regular string concat in other 
methods.

However, most of your changes here are on code used before string concat is 
ready, so this is a good improvement.

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

Marked as reviewed by liach (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/20705#pullrequestreview-2278655776
PR Comment: https://git.openjdk.org/jdk/pull/20705#issuecomment-2327592311

Reply via email to