From: Tim Bradshaw
Subject: Re: how to efficiently concatenate strings?
Date: 
Message-ID: <ey3n2a0sk45.fsf@todday.aiai.ed.ac.uk>
* kp gores wrote:
> hello,
> in a loop reading a file char-by-char into current-char i do :
>        (setf current-token (concatenate 'string 
>                                         current-token
>                                         (string current-char)))

> to collect the characters into  a string current-token.

What I do is: Allocate a buffer (string) with a fill pointer, which I
think is probably long enough.  Fill it with the characters I read.
Be ready to make it bigger if need be (with adjust-array).

I think this is a reasonable technique, although it depends on getting
the initial size reasonable and being clever about how much to
increase it by (I multiply by a factor each time, usually 2), and the
final string may be too long.  Working out the complexity of it should
be doable though I've not (it might be n log n time & space?).

A sort-of alternative, if you are doing this many times, is to have
one huge buffer (or possibly a list of buffers), which you are almost
sure is big enough, and then to read into that, and do one copy out of
it into your final string.  If you do the list-of-buffers thing then
you only ever do one string copy, and if the buffer(s) are big enough
you only allocate one string.  The penalty being that you have this
possibly large set of buffers hanging around which you need to GC by
hand.

But in (almost?) all implementations I've tried, I've found that
anything like this is completely hopeless because READ-CHAR is
hopeless, so I end up doing something using READ-SEQUENCE or its
implementation-dependent equivalent on older systems, and then
snarfing characters from that buffer into mine -- in other words doing
what the C stdio library does for you.  I admit to not having tried
this for a couple of years though, so recent implementations may be
better.

--tim