[Soot-list] StringBuffer or StringBuilder when performing string concatenation?

Dacong Yan tonywinslow1986 at gmail.com
Sat May 1 15:02:43 EDT 2010


Hi Jinping,

It seems that you were generating the jimple code
directly from your source code. I guess older versions
of soot (maybe latest one as well) would use StringBuffer
because they don't know about StringBuilder(1.5 or
later). If you generate jimple code from bytecode, then
StringBuilder would be used in place of StringBuffer.

Tony

On Sat, May 1, 2010 at 2:44 PM, Jinping Zhu <jnuzhjp at gmail.com> wrote:
> Hi, Eric:
>      When I tried to examine the jimple code of java string concatenation
> through "+", as to code below:
>     public void experiment(boolean condition){
>      String str = "demo";
>      str = str + "start";
>      if(condition) {
>       str = str + "condition true";
>      } else{
>       str = str + "condition false";
>      }
>      str = str + "end";
>     }
> it produced jimp like this:
>     public void experiment(boolean)
>     {
>         Test this;
>         boolean condition;
>         java.lang.String str, temp$1, temp$3, temp$5;
>         java.lang.StringBuffer temp$0, temp$2, temp$4, temp$6;
>         this := @this;
>         condition := @parameter0;
>         str = "demo";
>         temp$0 = new java.lang.StringBuffer;
>         specialinvoke temp$0.<init>();
>         temp$0.append(str);
>         temp$0.append("start");
>         temp$1 = temp$0.toString();
>         if condition == 0 goto label0;
>         temp$2 = new java.lang.StringBuffer;
>         specialinvoke temp$2.<init>();
>         temp$2.append(temp$1);
>         temp$2.append("condition true");
>         temp$3 = temp$2.toString();
>         str = temp$3;
>         goto label1;
>      label0:
>         temp$4 = new java.lang.StringBuffer;
>         specialinvoke temp$4.<init>();
>         temp$4.append(temp$1);
>         temp$4.append("condition false");
>         temp$5 = temp$4.toString();
>         str = temp$5;
>      label1:
>         temp$6 = new java.lang.StringBuffer;
>         specialinvoke temp$6.<init>();
>         temp$6.append(str);
>         temp$6.append("end");
>         temp$6.toString();
>         return;
>     }
> It's obviously that string concatenation through "+" will be replaced a
> sequence of code like creating a new StringBuffer and utilizing its append
> to concatenate the pieces, in order to improve performance.
> Well, the problem was that when I try to find out any expression
> like" <java.lang.StringBuffer: java.lang.StringBuffer
> append(java.lang.String)>" by using the equals() of String. The result
> turned out none of the above expressions found while actually there were
> serveral.
> When I dubugged the program, I discovered that object actually was
> StringBuilder instead of StringBuffer, so the signature became
> <java.lang.StringBuilder: java.lang.StringBuilder
> append(java.lang.String)>.So by comparing the signature I could not find any
> matches.
> Any explanation or suggestion will be greatly appreciated.
> Kelvin
>
> _______________________________________________
> Soot-list mailing list
> Soot-list at sable.mcgill.ca
> http://mailman.cs.mcgill.ca/mailman/listinfo/soot-list
>
>


More information about the Soot-list mailing list