Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #7567 > unrolled thread
| Started by | bob <bob@coolgroups.com> |
|---|---|
| First post | 2011-09-04 19:45 -0700 |
| Last post | 2011-09-06 08:53 +0200 |
| Articles | 16 on this page of 36 — 10 participants |
Back to article view | Back to comp.lang.java.programmer
StringBuilder bob <bob@coolgroups.com> - 2011-09-04 19:45 -0700
Re: StringBuilder Arne Vajhøj <arne@vajhoej.dk> - 2011-09-04 22:53 -0400
Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-05 05:27 +0200
Re: StringBuilder Arne Vajhøj <arne@vajhoej.dk> - 2011-09-05 19:34 -0400
Re: StringBuilder Wanja Gayk <brixomatic@yahoo.com> - 2011-09-11 16:35 +0200
Re: StringBuilder Arne Vajhøj <arne@vajhoej.dk> - 2011-09-11 16:48 -0400
Re: StringBuilder Wanja Gayk <brixomatic@yahoo.com> - 2011-09-17 02:54 +0200
Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-17 00:27 -0700
Re: StringBuilder Lew <lewbloch@gmail.com> - 2011-09-18 01:42 -0700
Re: StringBuilder Wanja Gayk <brixomatic@yahoo.com> - 2011-09-19 00:59 +0200
Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-19 05:07 -0700
Re: StringBuilder Jeff Higgins <jeff@invalid.invalid> - 2011-09-19 11:38 -0400
Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-05 17:01 -0700
Re: StringBuilder Stanimir Stamenkov <s7an10@netscape.net> - 2011-09-17 19:56 +0300
Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-17 20:35 +0200
Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-17 15:34 -0700
Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-18 01:33 +0200
Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-18 01:56 +0200
Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-17 20:58 -0700
Re: StringBuilder Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-09-17 22:44 -0700
Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-18 09:54 +0200
Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-18 09:59 +0200
Re: StringBuilder Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-09-18 07:28 -0700
Re: StringBuilder Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-18 08:15 -0400
Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-18 15:32 +0200
Re: StringBuilder Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-18 09:50 -0400
Re: StringBuilder Stanimir Stamenkov <s7an10@netscape.net> - 2011-09-18 17:08 +0300
Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-18 22:13 +0200
Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-18 22:29 +0200
Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-18 22:39 +0200
Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-19 09:45 -0700
Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-05 11:08 -0700
Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-05 23:54 +0200
Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-06 00:06 +0200
Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-05 17:03 -0700
Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-06 08:53 +0200
Page 2 of 2 — ← Prev page 1 [2]
| From | Jan Burse <janburse@fastmail.fm> |
|---|---|
| Date | 2011-09-18 09:54 +0200 |
| Message-ID | <j54841$92q$1@news.albasani.net> |
| In reply to | #8116 |
Peter Duniho schrieb:
> In any case, if Java does _not_ implement it that way, I suspect that's
Well, I wouldn't say its a matter of *Java*. Its a matter
of the given JDK how it is implementented. *Java* defines
the contract but there are many implementations.
From my post you can read off the following findings:
Oracle: Always does a copy (I only checked rt.jar,
not some alt-rt.jar).
OpenJDK: Always does a copy.
Harmony: Most of the time does a sharing and
flags the builder (Similarly like you
describe the .NET implementation)
But beware the list is not complete, for example I
didn't check the Apple classes.jar or IBM's rt.jar.
And also the finding only holds for JRE 1.6, and can
change at any time if the provider of the JRE decides
so. Or might be different for 32-bit and 64-bit etc..
> different decision-making process rather than ignorance. In other
> words, they have already considered whether it's a worthwhile
> optimization and decided
There is not only a plurality of *they* as can be seen
from above, but there is also a plurality what means
*worthwhile*. An implementation with a more reference
implementation character, like for example the Oracle
JRE, might take the simple route. Since the focus is
then more on functional requirements than on non-
functional requirements.
> Which strongly suggests that anyone worrying a priori
> about the performance of StringBuilder before they have
> demonstrated it's an actual bottleneck in their program
> is wasting their time.
I guess it is more about performance tuning than removing
bottlenecks. And any performance gain is only seen in
programs that make heavy use of StringBuilder. Such
measurements have already been done over and over. See
for example (not exactly measurig toString()):
+=: 546 ms
StringBuilder, default initial capacity: 30ms
StringBuilder, exact initial capacity: 10ms
http://christian.bloggingon.net/archive/2008/09/06/performance-vorteile-bei-der-verwendung-von-stringbuilder.aspx
But measurements in detail will vary depending on JDK
and machine. But whether one JDK changes the measurment
fundamentally depends very much of the available
algorithms for the functional requirements and how
these algorithms behave non-functionally.
But I would say the consumers of JDKs are we the
programmers, so it is good to measure, inspect and
debate JDKs and not blindly trust any *they*.
Bye
[toc] | [prev] | [next] | [standalone]
| From | Jan Burse <janburse@fastmail.fm> |
|---|---|
| Date | 2011-09-18 09:59 +0200 |
| Message-ID | <j548dh$92q$2@news.albasani.net> |
| In reply to | #8118 |
Jan Burse schrieb: > for example (not exactly measurig toString()): > > +=: 546 ms > StringBuilder, default initial capacity: 30ms > StringBuilder, exact initial capacity: 10ms > > http://christian.bloggingon.net/archive/2008/09/06/performance-vorteile-bei-der-verwendung-von-stringbuilder.aspx > Oops it measures somehow toString(). Since multiple toStrings() are involved in the += test case.
[toc] | [prev] | [next] | [standalone]
| From | Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> |
|---|---|
| Date | 2011-09-18 07:28 -0700 |
| Message-ID | <XOWdncsx2fSInevTnZ2dnUVZ_uWdnZ2d@posted.palinacquisition> |
| In reply to | #8118 |
On 9/18/11 12:54 AM, Jan Burse wrote: > Peter Duniho schrieb: > >> In any case, if Java does _not_ implement it that way, I suspect that's > > Well, I wouldn't say its a matter of *Java*. Its a matter > of the given JDK how it is implementented. *Java* defines > the contract but there are many implementations. It's true, I use "Java" generically here. So? > [...] > I guess it is more about performance tuning than removing > bottlenecks. And any performance gain is only seen in > programs that make heavy use of StringBuilder. Such > measurements have already been done over and over. See > for example (not exactly measurig toString()): > > +=: 546 ms > StringBuilder, default initial capacity: 30ms > StringBuilder, exact initial capacity: 10ms > > http://christian.bloggingon.net/archive/2008/09/06/performance-vorteile-bei-der-verwendung-von-stringbuilder.aspx Such measurements are meaningless out of context. The point is how much time is spent in StringBuilder _for a specific application_.
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-09-18 08:15 -0400 |
| Message-ID | <j54nc9$tgk$1@dont-email.me> |
| In reply to | #8116 |
On 9/18/2011 1:44 AM, Peter Duniho wrote:
(Amusing aside: Thunderbird's spell-checker wants to change
Peter's last name to "Whodunit.")
> On 9/17/11 3:34 PM, Roedy Green wrote:
>> [...]
>> StringBuilder composes its string in a char[]. Unfortunately it can't
>> simply plop that into a String object at the end. It has to allocate
>> yet another buffer, copy into it, and that becomes your string object.
>> [...]
>
> Are you sure that's the implementation?
>
> In .NET, there is also a String and StringBuilder class pair, and
> StringBuilder.ToString() does in fact just create a new String instance
> using the existing buffer.[...]
>
> In any case, if Java does _not_ implement it that way, I suspect that's
> out of a different decision-making process rather than ignorance. In
> other words, they have already considered whether it's a worthwhile
> optimization and decided otherwise. [...]
I don't have old Java source, but I do have an old O'Reilly
book ("Java In a Nutshell, 2nd Edition, Covers Java 1.1, by David
Flanagan") that states
The StringBuffer.toString() method does not copy the internal
array of characters; instead it shares that array with the
new String object, and makes a new copy for itself only when
further modifications are made to the StringBuffer object.
Assuming the author was correct, it follows that the implementation
changed at some later point. Presumably, that change was driven by
evidence that the "optimization" wasn't worth while.
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | Jan Burse <janburse@fastmail.fm> |
|---|---|
| Date | 2011-09-18 15:32 +0200 |
| Message-ID | <j54rss$j3q$1@news.albasani.net> |
| In reply to | #8124 |
Eric Sosman schrieb:
> I don't have old Java source, but I do have an old O'Reilly
> book ("Java In a Nutshell, 2nd Edition, Covers Java 1.1, by David
> Flanagan") that states
>
> The StringBuffer.toString() method does not copy the internal
> array of characters; instead it shares that array with the
> new String object, and makes a new copy for itself only when
> further modifications are made to the StringBuffer object.
>
> Assuming the author was correct, it follows that the implementation
> changed at some later point. Presumably, that change was driven by
> evidence that the "optimization" wasn't worth while.
I guess first of all probably a reengineering was done, and
StringBuffer was split into AbstractStringBuilder and StringBuffer.
And then StringBuilder was created. The class diagramm is basically:
AbstractStringBuilder
/ \
StringBuffer StringBuilder
Most of the StringBuffer methods are synchronized and then delegate
to the abstract super class. In StringBuilder the methods are
non-synchronized and then also delegate to the abstract super class.
The delegation instead of just using the methods from the abstract
class is needed so that the correct type is returned. For example
the AbstractStringBuilder does have an append that even returns
an AbstractStringBuilder. But it is then overridden by a method that
returns StringBuffer respectively StringBuilder.
So we see for example:
In StringBuffer:
public synchronized StringBuffer append(char c) {
super.append(c);
return this;
}
In StringBuilder:
public StringBuilder append(char c) {
super.append(c);
return this;
}
The necessary overriding capability called covariant return types is
only available since Java 1.5.
http://www.java-tips.org/java-se-tips/java.lang/covariant-return-types.html
So maybe during all this quality language reengineering the aspect
of optimization became less prominent.
Best Regards
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-09-18 09:50 -0400 |
| Message-ID | <j54svs$u9u$1@dont-email.me> |
| In reply to | #8125 |
On 9/18/2011 9:32 AM, Jan Burse wrote:
> Eric Sosman schrieb:
>> I don't have old Java source, but I do have an old O'Reilly
>> book ("Java In a Nutshell, 2nd Edition, Covers Java 1.1, by David
>> Flanagan") that states
>>
>> The StringBuffer.toString() method does not copy the internal
>> array of characters; instead it shares that array with the
>> new String object, and makes a new copy for itself only when
>> further modifications are made to the StringBuffer object.
>>
>> Assuming the author was correct, it follows that the implementation
>> changed at some later point. Presumably, that change was driven by
>> evidence that the "optimization" wasn't worth while.
>
> I guess first of all probably a reengineering was done, and
> StringBuffer was split into AbstractStringBuilder and StringBuffer.
> And then StringBuilder was created. The class diagramm is basically:
>
> AbstractStringBuilder
> / \
> StringBuffer StringBuilder
I do not know whether buffer-sharing was dropped before, after,
or with the introduction of StringBuilder.
> Most of the StringBuffer methods are synchronized and then delegate
> to the abstract super class. In StringBuilder the methods are
> non-synchronized and then also delegate to the abstract super class.
Some methods delegate, some do not. StringBuilder.toString()
in particular does not delegate to the superclass.
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | Stanimir Stamenkov <s7an10@netscape.net> |
|---|---|
| Date | 2011-09-18 17:08 +0300 |
| Message-ID | <j54u0r$54c$1@dont-email.me> |
| In reply to | #8127 |
Sun, 18 Sep 2011 09:50:03 -0400, /Eric Sosman/:
> On 9/18/2011 9:32 AM, Jan Burse wrote:
>
>> I guess first of all probably a reengineering was done, and
>> StringBuffer was split into AbstractStringBuilder and StringBuffer.
>> And then StringBuilder was created. The class diagramm is basically:
>>
>> AbstractStringBuilder
>> / \
>> StringBuffer StringBuilder
>
> I do not know whether buffer-sharing was dropped before, after,
> or with the introduction of StringBuilder.
I don't know when the implementation has changed, also, but here's
my guess on why it might have changed to make a copy. Previously
I've often used:
StringBuffer buf;
...
String str = buf.substring(0, buf.length());
to have the constructed String internal char[] always occupying
exactly the size needed to hold the text. Previously, with
StringBuffer.toString() the String could hold as much as twice
bigger char[] than needed. If one constructs long lived String
objects that way, the memory usage could grow unnecessary high.
--
Stanimir
[toc] | [prev] | [next] | [standalone]
| From | Jan Burse <janburse@fastmail.fm> |
|---|---|
| Date | 2011-09-18 22:13 +0200 |
| Message-ID | <j55jcp$9im$1@news.albasani.net> |
| In reply to | #8127 |
Eric Sosman schrieb: > I do not know whether buffer-sharing was dropped before, after, > or with the introduction of StringBuilder. The implementation advice comment was removed in 1.5 javadoc. It was still present in 1.4.1 javadoc: 1.4.1 javadoc: Converts to a string representing the data in this string buffer. A new String object is allocated and initialized to contain the character sequence currently represented by this string buffer. This String is then returned. Subsequent changes to the string buffer do not affect the contents of the String. Implementation advice: This method can be coded so as to create a new String object without allocating new memory to hold a copy of the character sequence. Instead, the string can share the memory used by the string buffer. Any subsequent operation that alters the content or capacity of the string buffer must then make a copy of the internal buffer at that time. This strategy is effective for reducing the amount of memory allocated by a string concatenation operation when it is implemented using a string buffer. http://download.oracle.com/javase/1.4.2/docs/api/java/lang/StringBuffer.html#toString%28%29 1.5 javadoc: Returns a string representing the data in this sequence. A new String object is allocated and initialized to contain the character sequence currently represented by this object. This String is then returned. Subsequent changes to this sequence do not affect the contents of the String. http://download.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuffer.html#toString%28%29 Bye
[toc] | [prev] | [next] | [standalone]
| From | Jan Burse <janburse@fastmail.fm> |
|---|---|
| Date | 2011-09-18 22:29 +0200 |
| Message-ID | <j55kaq$bi8$1@news.albasani.net> |
| In reply to | #8140 |
Jan Burse schrieb: > The implementation advice comment was removed in 1.5 javadoc. > It was still present in 1.4.1 javadoc: But I don't see the AbstractStringBuilder in the Oracle javadoc, maybe it is filtered out since it is package local(*). Or the class is specific to OpenJDK, not sure. Slowly getting confused, not feeling comfortable starting a dozen of different machines to check the JDK installation... Dammned, isn't there a website where we see such things side by side? But StringBuilder has a @since 1.5 tag, so this is the earliest moment for this class to look for a javadoc. Bye P.S.: It really seems that it is filtered out, David Flanagan was confronted with it: This is the first instance that I am aware of of a public class extending a non-public one, and it raises questions about documenting the public one. The javadoc team dealt with it by simply pretending that the non-public intermediary class does not exist. Now I have to figure out how to document this for Java in a Nutshell. http://www.davidflanagan.com/2004/10/abstractstringbuilder.html
[toc] | [prev] | [next] | [standalone]
| From | Jan Burse <janburse@fastmail.fm> |
|---|---|
| Date | 2011-09-18 22:39 +0200 |
| Message-ID | <j55kuu$cu7$1@news.albasani.net> |
| In reply to | #8143 |
Jan Burse schrieb: > P.S.: It really seems that it is filtered out, David Flanagan > was confronted with it: BTW he is moaning that AbstractStringBuilder is not public: "It seems to me that this was a real missed opportunity. If AbstractStringBuilder had been public we could write methods that would work with either a StringBuffer or a StringBuilder argument." Well we could still go to some extend with the interfaces Appendable and Charsequence. An example with a non package local abstract superclass would be Vector and ArrayList, they are also synchronized and non- synchronized implementations of a now public class AbstractList. Bye
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2011-09-19 09:45 -0700 |
| Message-ID | <c8se77t9e4dkjreqri9ntr75ogaie616mb@4ax.com> |
| In reply to | #8116 |
On Sat, 17 Sep 2011 22:44:59 -0700, Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> wrote, quoted or indirectly quoted someone who said : >In .NET, there is also a String and StringBuilder class pair, and >StringBuilder One thing that works against a Java efficient implementation is that the char array theoretically remains accessible even after toString, even though it is rarely used. The .NET may have avoided that, which would have made the optimisation more attractive. I did look into how this works, but some time ago probably around 1.4.. They may have improved the implementation. -- Roedy Green Canadian Mind Products http://mindprod.com It is a lot easier to combine things later that have been too finely categorised than to split things later whose are categories are too coarse.
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2011-09-05 11:08 -0700 |
| Message-ID | <iu3a67dg2468r8146gq9e6ssss5u08m559@4ax.com> |
| In reply to | #7567 |
On Sun, 4 Sep 2011 19:45:37 -0700 (PDT), bob <bob@coolgroups.com> wrote, quoted or indirectly quoted someone who said : >Is there any way to use StringBuilder but with + signs instead of >append? No. Java has so such syntactic sugar. However, the IntelliJ IDE will convert from + notation to sb.append notation. -- Roedy Green Canadian Mind Products http://mindprod.com The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is, the search for a superior moral justification for selfishness. ~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
[toc] | [prev] | [next] | [standalone]
| From | Jan Burse <janburse@fastmail.fm> |
|---|---|
| Date | 2011-09-05 23:54 +0200 |
| Message-ID | <j43gfg$cpe$1@news.albasani.net> |
| In reply to | #7577 |
Roedy Green schrieb:
> No. Java has so such syntactic sugar. However, the IntelliJ IDE will
> convert from + notation to sb.append notation.
Maybe the IDE provides something similar. Basically I have seeing
the IDE mock about the following:
buf.append(x+y);
It then suggests to use:
buf.append(x);
buf.append(y);
And it also mocks about the following:
StringBuffer buf=new StringBuffer();
It then suggests to use:
StringBuilder buf=new StringBuilder();
But that + is internally realized via StringBuilder is a property
of javac with target >= 1.5 and not of the IDE.
See for example:
http://caprazzi.net/posts/java-bytecode-string-concatenation-and-stringbuilder/
Bye
[toc] | [prev] | [next] | [standalone]
| From | Jan Burse <janburse@fastmail.fm> |
|---|---|
| Date | 2011-09-06 00:06 +0200 |
| Message-ID | <j43h61$fcc$1@news.albasani.net> |
| In reply to | #7585 |
Jan Burse schrieb: > But that + is internally realized via StringBuilder is a property > of javac with target >= 1.5 and not of the IDE. And -XX:+OptimizeStringConcat is present in the oracle runtime since 1.6_20. Says the following article: http://groups.google.com/group/jvm-languages/browse_thread/thread/2ad54161f793f378 So this is in the java command and not in the IDE. Bye
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2011-09-05 17:03 -0700 |
| Message-ID | <gooa67901r8jqs2s60noug5gtqd98dmdkt@4ax.com> |
| In reply to | #7585 |
On Mon, 05 Sep 2011 23:54:55 +0200, Jan Burse <janburse@fastmail.fm> wrote, quoted or indirectly quoted someone who said : >And it also mocks about the following I am not sure what you mean by "mock". Some IDEs will suggest code changes if you ask for suggestions (inspecting). IntelliJ will do that. In addition it will convert the code for you (refactoring). -- Roedy Green Canadian Mind Products http://mindprod.com The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is, the search for a superior moral justification for selfishness. ~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
[toc] | [prev] | [next] | [standalone]
| From | Jan Burse <janburse@fastmail.fm> |
|---|---|
| Date | 2011-09-06 08:53 +0200 |
| Message-ID | <j44g0o$80a$1@news.albasani.net> |
| In reply to | #7596 |
Roedy Green schrieb: > I am not sure what you mean by "mock". Like in mockery, but not like in mock-up.
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.java.programmer
csiph-web