Efficient string concatenation in Newspeak

63 views
Skip to first unread message

Rouan van Dalen

unread,
May 2, 2013, 7:38:31 AM5/2/13
to newspeak...@googlegroups.com
Hi everyone,

To do a lot of string concatenation in C#, we use the StringBuilder class.

Do we have a class in Newspeak for efficient string concatenation, or is the #, operator
already efficient?

Regards
--Rouan

Gilad Bracha

unread,
May 2, 2013, 10:12:12 AM5/2/13
to newspeak...@googlegroups.com
HI Rouan,

The problem is not whether , is efficient per se. Rather, a sequence of concatenation s1, s2, s3 causes  the contents of s1 and s2 to be copied twice  - once into s1, s2 and then again into (s1, s2), s3. Plus there's an extra allocation. I confess I have never worried about this in Smalltalk or Newspeak, as it is rare for me to write code where this in a performance critical place. In genreal, this is a high level language. I don't recall a StringBuilder equivalent.

That said, Strings are collections and you can easily build an Array out of the contents of several strings, copying each only once and then build a String out of that.  That still copies things twice. You'd need to mess with Strings internals to avoid that, but I wouldn't.

If I've overlooked something, I'm sure someone will tell me.
--
Cheers, Gilad

Frank Shearar

unread,
May 2, 2013, 10:17:18 AM5/2/13
to newspeak...@googlegroups.com
On 2 May 2013 15:12, Gilad Bracha <gbr...@gmail.com> wrote:
> HI Rouan,
>
> The problem is not whether , is efficient per se. Rather, a sequence of
> concatenation s1, s2, s3 causes the contents of s1 and s2 to be copied
> twice - once into s1, s2 and then again into (s1, s2), s3. Plus there's an
> extra allocation. I confess I have never worried about this in Smalltalk or
> Newspeak, as it is rare for me to write code where this in a performance
> critical place. In genreal, this is a high level language. I don't recall a
> StringBuilder equivalent.

Interestingly I wrote (in Smalltalk) a fold using #, just the other
day that took long enough to make SUnit tests fail. The solution was
to use a Stream and #nextPutAll: everything.

frank

Ryan Macnak

unread,
May 2, 2013, 10:29:49 AM5/2/13
to newspeak...@googlegroups.com
Right, so String streamContents: [:stm | stm nextPutAll: s1; nextPutAll: s2; ...] would be the nearest equivalent.

Gilad Bracha

unread,
May 2, 2013, 10:47:54 AM5/2/13
to newspeak...@googlegroups.com
I knew I must be overlooking something obvious. Of course streams are the solution to this (and so much more elegant than StringBuilder). So just do as Frank and Ryan suggest.
--
Cheers, Gilad

Rouan van Dalen

unread,
May 2, 2013, 10:55:07 AM5/2/13
to newspeak...@googlegroups.com
Thanks everyone,

I opted for something like:

    | str = String new writeStream. |

    str nextPutAll: ...
    str nextPutAll: ...
    str nextPutAll: ...
    ....

Regards
--Rouan

Gilad Bracha

unread,
May 2, 2013, 11:12:52 AM5/2/13
to newspeak...@googlegroups.com
That's fine as long as you remember to get the contents out of the stream str at the end.
--
Cheers, Gilad
Reply all
Reply to author
Forward
0 new messages