[PATCH] AbstractStringBuilder.append(<StringBuilder>)
Jonathan Gibbons
jonathan.gibbons at oracle.com
Fri Jun 29 21:50:26 UTC 2018
In that specific snippet, I would expect the compiler (javac) to fold
the constants together.
-- Jon
On 06/29/2018 01:02 PM, Isaac Levy wrote:
> Would this snippet end up merging two StringBuilders?
>
> String x = "bar" + "foo";
> x += "baz" + "biz";
>
> I had trouble reading stringopts.cpp. I was thinking this append might
> be common due to implicit StringBuilder creation.
>
> Isaac
>
>
> On Fri, Jun 29, 2018 at 12:48 PM, Paul Sandoz <paul.sandoz at oracle.com> wrote:
>> Hard to reconstruct the culture memory around this. I suspect such a new method was not considered necessary when StringBuilder was added to Java 1.5 given the CharSequence accepting method, and performance was not considered a concern, and I doubt it's a concern now.
>>
>> (I don’t think there is any circularity that would result from bridge methods if such a method was added.)
>>
>> Paul.
>>
>>> On Jun 27, 2018, at 10:22 PM, Martin Buchholz <martinrb at google.com> wrote:
>>>
>>> I'm fairly sure the append(StringBuilder) overloads were left out
>>> intentionally.
>>>
>>> On Wed, Jun 27, 2018 at 8:57 PM, Isaac Levy <isaac.r.levy at gmail.com> wrote:
>>>
>>>> AbstractStringBuilder already has append(<StringBuffer>). This patch
>>>> adds append(<StringBuilder>).
>>>>
>>>> The new method improves parity between the two classes. In addition,
>>>> combining StringBuilders is presumably common. append(<CharSequence>)
>>>> has a couple insteadof checks, which this new method skips.
>>>>
>>>> -Isaac
>>>>
>>>>
>>>>
>>>>
>>>> diff --git a/src/java.base/share/classes/java/lang/
>>>> AbstractStringBuilder.java
>>>> b/src/java.base/share/classes/java/lang/AbstractStringBuilder.java
>>>> index 2ef3e53256..1fe89bb92a 100644
>>>> --- a/src/java.base/share/classes/java/lang/AbstractStringBuilder.java
>>>> +++ b/src/java.base/share/classes/java/lang/AbstractStringBuilder.java
>>>> @@ -543,6 +543,11 @@ abstract class AbstractStringBuilder implements
>>>> Appendable, CharSequence {
>>>> return this.append((AbstractStringBuilder)sb);
>>>> }
>>>>
>>>> + // Documentation in subclasses because of synchro difference
>>>> + public AbstractStringBuilder append(StringBuilder sb) {
>>>> + return this.append((AbstractStringBuilder)sb);
>>>> + }
>>>> +
>>>> /**
>>>> * @since 1.8
>>>> */
>>>> diff --git a/src/java.base/share/classes/java/lang/StringBuffer.java
>>>> b/src/java.base/share/classes/java/lang/StringBuffer.java
>>>> index e597a8112e..613ba90c25 100644
>>>> --- a/src/java.base/share/classes/java/lang/StringBuffer.java
>>>> +++ b/src/java.base/share/classes/java/lang/StringBuffer.java
>>>> @@ -313,6 +313,33 @@ import jdk.internal.HotSpotIntrinsicCandidate;
>>>> return this;
>>>> }
>>>>
>>>> + /**
>>>> + * Appends the specified {@code StringBuilder} to this sequence.
>>>> + * <p>
>>>> + * The characters of the {@code StringBuilder} argument are appended,
>>>> + * in order, to the contents of this {@code StringBuffer}, increasing
>>>> the
>>>> + * length of this {@code StringBuffer} by the length of the argument.
>>>> + * If {@code sb} is {@code null}, then the four characters
>>>> + * {@code "null"} are appended to this {@code StringBuffer}.
>>>> + * <p>
>>>> + * Let <i>n</i> be the length of the old character sequence, the one
>>>> + * contained in the {@code StringBuffer} just prior to execution of
>>>> the
>>>> + * {@code append} method. Then the character at index <i>k</i> in
>>>> + * the new character sequence is equal to the character at index
>>>> <i>k</i>
>>>> + * in the old character sequence, if <i>k</i> is less than <i>n</i>;
>>>> + * otherwise, it is equal to the character at index <i>k-n</i> in the
>>>> + * argument {@code sb}.
>>>> + * <p>
>>>> + *
>>>> + * @param sb the {@code StringBuilder} to append.
>>>> + * @return a reference to this object.
>>>> + */
>>>> + public synchronized StringBuffer append(StringBuilder sb) {
>>>> + toStringCache = null;
>>>> + super.append(sb);
>>>> + return this;
>>>> + }
>>>> +
>>>> /**
>>>> * Appends the specified {@code StringBuffer} to this sequence.
>>>> * <p>
>>>> diff --git a/src/java.base/share/classes/java/lang/StringBuilder.java
>>>> b/src/java.base/share/classes/java/lang/StringBuilder.java
>>>> index 40da2083c2..5ddd4fb5f9 100644
>>>> --- a/src/java.base/share/classes/java/lang/StringBuilder.java
>>>> +++ b/src/java.base/share/classes/java/lang/StringBuilder.java
>>>> @@ -199,6 +199,30 @@ public final class StringBuilder
>>>> return this;
>>>> }
>>>>
>>>> + /**
>>>> + * Appends the specified {@code StringBuilder} to this sequence.
>>>> + * <p>
>>>> + * The characters of the {@code StringBuilder} argument are appended,
>>>> + * in order, to this sequence, increasing the
>>>> + * length of this sequence by the length of the argument.
>>>> + * If {@code sb} is {@code null}, then the four characters
>>>> + * {@code "null"} are appended to this sequence.
>>>> + * <p>
>>>> + * Let <i>n</i> be the length of this character sequence just prior to
>>>> + * execution of the {@code append} method. Then the character at index
>>>> + * <i>k</i> in the new character sequence is equal to the character at
>>>> + * index <i>k</i> in the old character sequence, if <i>k</i> is less
>>>> than
>>>> + * <i>n</i>; otherwise, it is equal to the character at index
>>>> <i>k-n</i>
>>>> + * in the argument {@code sb}.
>>>> + *
>>>> + * @param sb the {@code StringBuilder} to append.
>>>> + * @return a reference to this object.
>>>> + */
>>>> + public StringBuilder append(StringBuilder sb) {
>>>> + super.append(sb);
>>>> + return this;
>>>> + }
>>>> +
>>>> @Override
>>>> public StringBuilder append(CharSequence s) {
>>>> super.append(s);
>>>>
More information about the core-libs-dev
mailing list