Aladdin - Scala Bugtracking
[#1070] project: compiler priority: low category: bug
submitter assigned to status date submitted
Nikolay Iulian fixed 2007-04-24 17:29:34.0
subject [contrib #438] Performance optimization: java.lang.StringBuilder instead of StringBuffer
code
In looking through disassembled Scala code, it appears that even
with the -target:jvm-1.5 command line option, String's are still
built using java.lang.StringBuffer instead of java.lang.StringBuilder.
Using StringBuilder is faster than StringBuffer since it's not
synchronized:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html

object Test extends Application
{
  val r = "12345"
  val z = "abc" + r + "def"
}

generates code like this:

   23:  invokespecial   #36; //Method java/lang/StringBuffer."<init>":()V
   26:  ldc     #38; //String abc
   28:  invokevirtual   #42; //Method java/lang/StringBuffer.append:(Ljava/lang/Object;)Ljava/lang/StringBuffer;
   31:  aload_0
   32:  invokevirtual   #45; //Method r:()Ljava/lang/String;
   35:  invokevirtual   #42; //Method java/lang/StringBuffer.append:(Ljava/lang/Object;)Ljava/lang/StringBuffer;
   38:  ldc     #47; //String def
   40:  invokevirtual   #42; //Method java/lang/StringBuffer.append:(Ljava/lang/Object;)Ljava/lang/StringBuffer;
   43:  invokevirtual   #50; //Method java/lang/StringBuffer.toString:()Ljava/lang/String;

Compiling this little Java code with JDK1.6

class JavaTest {
    String f()
        {
            String r = "def";
            return "abc" + r + "def";
        }
}

shows this:

   7:   invokespecial   #4; //Method java/lang/StringBuilder."<init>":()V
   10:  ldc     #5; //String abc
   12:  invokevirtual   #6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   15:  aload_1
   16:  invokevirtual   #6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   19:  ldc     #2; //String def
   21:  invokevirtual   #6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   24:  invokevirtual   #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;

I think this should be enabled if the -target:jvm-1.5 is used, since
StringBuilder was only introduced in 1.5.

Regards,
Blair
what happened
what expected
[back to overview]
Changes of this bug report
Nikolay  edited on  2007-04-24 17:30:12.0
contribution #438
Iulian  edited on  2007-05-01 10:09:38.0