Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When was Javac StringBuilder/StringBuffer optimization introduced?

I know that Javac compiler is able to transform String concatenation + using StringBuilder/StringBuffer, and I'm curious to know starting from which version this change was introduced?

I'm using this sample code:

public class Main {   public static void main(String[] args) {       String a = args[0];       String s = "a";       s = s + a;       s = s + "b";       s = s + "c";       s = s + "d";       s = s + "e";       System.out.println(s);   } } 

So far I've tried with javac 1.8.0_121, javac 1.6.0_20, javac 1.5.0_22 and java 1.4.2_19.

Here is a sample of the bytecode I see using javap -c from 1.4.2_19:

6:  astore_2 7:  new #3; //class StringBuffer 10: dup 11: invokespecial   #4; //Method java/lang/StringBuffer."<init>":()V 14: aload_2 15: invokevirtual   #5; //Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer; 18: aload_1 19: invokevirtual   #5; //Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer; 22: invokevirtual   #6; //Method java/lang/StringBuffer.toString:()Ljava/lang/String; 

All 4 versions seems to be using the StringBuilder/StringBuffer optimization, so I'm curious to know starting from which Javac version this change was introduced?

like image 437
Nicolas C Avatar asked Feb 27 '17 14:02

Nicolas C


People also ask

When was StringBuilder introduced?

The StringBuilder class was introduced in Java 1.5v after StringBuffer class having an additional functionality of non-synchronized methods so that multiple threads can be allowed to use StringBuilder objects.

Why StringBuffer and StringBuilder classes are introduced in Java?

The StringBuffer and StringBuilder classes are used when there is a necessity to make a lot of modifications to Strings of characters. Unlike Strings, objects of type StringBuffer and String builder can be modified over and over again without leaving behind a lot of new unused objects.

Which is more time efficient StringBuilder or StringBuffer?

StringBuilder is non-synchronized i.e. not thread safe. It means two threads can call the methods of StringBuilder simultaneously. StringBuffer is less efficient than StringBuilder. StringBuilder is more efficient than StringBuffer.

How much faster is StringBuilder than StringBuffer?

So the figure displayed for StringBuilder is actually time it took to run stringbuffer AND stringbuilder test. Add this line and you'll see that StringBuilder IS faster about TWO TIMES.


2 Answers

Here's a quote from the language specification from version 1:

An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class (§20.13) or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.

Back at the time, they had StringBuffer instead of StringBuilder.

Also a quote from StringBuffer of JDK1.0.2:

This Class is a growable buffer for characters. It is mainly used to create Strings. The compiler uses it to implement the "+" operator.

like image 96
M A Avatar answered Oct 14 '22 12:10

M A


I have looked up the Java Language Specification, First Edition (from 1996). Not an easy find, but here it is. The passage on concatenation optimization was there even then:

An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class (§20.13) or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.

The specification pertained to StringBuffer then, but StringBuilder (which current JLS wording refers to) might be deemed better performing because its methods are not synchronized.

This, however, does not mean that one should rely on the optimization as always being in place. String concatenation in loops will not get optimized, for example.

like image 25
lukeg Avatar answered Oct 14 '22 14:10

lukeg