[REBOL] Re: Buffers
From: larry:ecotope at: 13-Feb-2001 17:17
Hi Paul
The basic answer to your question is that it helps the interpreter be more
efficient.
>> rebol/stats ;requires experimental builds
== 4047992
>> s: make string! 10000
== ""
>> rebol/stats
== 4058232
>> 4058232 - 4047992
== 10240
So the interpreter has actually pre-allocated 10000 bytes of (presumably
consecutive) memory for the string, even though the string is still empty.
Therefore when appending to the string the preallocated memory can be used
without having to dynamically reallocate memory. When you say
s: make string! 0
or
s: "hello"
the interpreter only preallocates a small amount of memory. If you keep
appending to the string it will eventually be filled. At that point the
interpreter will have allocate another block of memory and move the data.
This reallocation takes extra time.
Regardless of the amount of memory preallocated, a series can be grown
indefinitely (up to available memory).
For best efficiency when dealing with long series, it is best to preallocate
the memory when possible. The same reasoning applies to all series
datatypes.
HTH
-Larry