JLS string concatenation clarification
Vicente-Arturo Romero-Zaldivar
vicente.romero at oracle.com
Tue Feb 11 08:46:26 PST 2014
Hi Sergey,
It's hard to make sure that any change to the current "pattern"
generated by javac will be beneficial or not in terms of performance.
I have assigned those bugs to myself but as they are not high priority I
can't make any promise.
Thanks,
Vicente
On 07/02/14 21:28, Sergey Bylokhov wrote:
> Hi, Vicente.
> For a long time I ask a question related to the implementation of
> concatenations of the string in javac:
> 1 Why "" + expression is compiled to
> new StringBuilder()).append("").append(expression).toString()
> instead of String.valueOf(expression)? see JDK-4947460
I have assigned it to myself.
> 2 Why expression1 + expression2 is compiled to
> new
> StringBuilder()).append(expression1).append(expression2).toString()
> instead of new
> StringBuilder(expression1)).append(expression2).toString()? see
> JDK-4059189
Same.
> 3 Why JDK-6709423 is closed as not a defect, it could at least save a
> few bytes in the class files. Generated code currently is not perfect:
> s += a;
> s += b;
> is compiled to:
> s = new StringBuilder()).append(s).append(a).toString();
> s = new StringBuilder()).append(s).append(b).toString();
> instead of new StringBuilder(s).append(a).append(b).toString();
I have reopened it.
>
> I suppose, these optimizations will not affect a JIT (will not make
> result worse) and will save the size of class file.
>
> On 29.01.2014 1:00, Vicente-Arturo Romero-Zaldivar wrote:
>> Hi Alex,
>>
>> On 27/01/14 19:29, Alex Buckley wrote:
>>> Hi Vicente,
>>>
>>> Generated code. Also, I realize that s+"" may generate different
>>> code than ""+s - please indicate if that's the case.
>>
>> Yes the generated code is slightly different. But at the end javac
>> only indicates what should be done. A string concatenation implies
>> the creation of a StringBuilder with calls to it's append() method.
>> Later the VM should these instructions only as indications.
>>
>> Vicente
>>
>>>
>>> Alex
>>>
>>> On 1/27/2014 9:33 AM, Vicente-Arturo Romero-Zaldivar wrote:
>>>> Hi Alex,
>>>>
>>>> Are you interested in the generated code or in the compiler's internal
>>>> representation?
>>>>
>>>> Thanks,
>>>> Vicente
>>>>
>>>> On 22/01/14 22:14, Alex Buckley wrote:
>>>>> To compiler-dev and others,
>>>>>
>>>>> Since the original question asked when the JLS allows compilers to
>>>>> deviate from "always" creating a new String object, I would like to
>>>>> know more about the following case:
>>>>>
>>>>> On 1/21/2014 4:45 PM, Alex Buckley wrote:
>>>>>> - "Semi" constant expressions like s+"". Concatenating the empty
>>>>>> string
>>>>>> literal "" with a String expression is mentioned in
>>>>>> https://bugs.openjdk.java.net/browse/JDK-4036535. The expectation
>>>>>> (per
>>>>>> JLS1) that a new String object is always created in this case is
>>>>>> misplaced (per the JDK's actual behavior). But, the JLS never
>>>>>> clarified
>>>>>> this.
>>>>>
>>>>> If compiler authors can confirm (on-list or privately) that indeed
>>>>> "concatenation with empty string does not create the new instance of
>>>>> String", then I will clarify the JLS.
>>>>>
>>>>> Alex
>>>>
>>
>
>
More information about the compiler-dev
mailing list