On Sun, 25 Aug 2024 13:36:34 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;

This pull request has now been integrated.

Changeset: 55312e15
Author:    Shaojin Wen <s...@openjdk.org>
Committer: Chen Liang <li...@openjdk.org>
URL:       
https://git.openjdk.org/jdk/commit/55312e1549c36be46b0f3b3b40763a33311c3e25
Stats:     57 lines in 7 files changed: 39 ins; 4 del; 14 mod

8338937: Optimize the string concatenation of ClassDesc

Reviewed-by: liach

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

PR: https://git.openjdk.org/jdk/pull/20705

Reply via email to