On Wed, Dec 2, 2009 at 3:04 AM, ziyu_huang <
ziyu4...@gmail.com> wrote:
> Don't you see I mention there is something missing like Java's
> StringBuffer?
[...]
> So where is mutable string? will there be one ? this is basic thing
> that a modern language should provide.
[...]
This is important.
The use case here is incrementally building up a large string. For
instance you are building up an HTML table from a database table. The
code isn't complicated, but there is a lot of
myHtml += anotherPiece;
going on.
With immutable strings as Go has, every time you do this you recopy
the string. This is a O(n*n) algorithm. There are several possible
improvements over this naive strategy.
1. Mutable strings. When you create a string, allocate a fixed
fraction more space than is needed. Then when appending you only have
to reallocate if you're going to run out of buffer. Building a long
string this way is O(n). Perl uses this strategy for all strings.
2. Have a special data type for lots of pieces of string that will be
joined later. There are several ways of doing it under the hood that
are O(n). This is what Java's StringBuffer does.
3. Create a vector of strings. Push on more strings as they come.
Join them at the end. This is theoretically O(n) but building a long
string this way provides an interesting stress test for the garbage
collector that can do bad things. I've used this strategy in
JavaScript.
4. Do the same as 3 except with a multi-level join strategy. For
instance in building a table, you could incrementally add each field
in the table, join each row once it is complete, then join the rows
once the table is complete. I've been forced to this strategy in
JavaScript when the previous one performed badly. (This was some
years ago, modern browsers likely do better now.)
I think that it is imperative in the long run for Go to make at least
one of #1, #2 or #3 perform well. To the extent that there is a
currently a plan, it is likely to make #3 work well. However I
believe that #1 and #2 both can offer better performance and memory
characteristics.
Cheers,
Ben