Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Buffers

 [1/6] from: ptretter:norcom2000 at: 13-Feb-2001 17:58


Why do some people set a word at predefined levels and what is the operational aspects of for example: buffer: make string! 60 vs. buffer: make string! 0 Does this automatically exand to a predined if set at 0 or why set it to anything other than 0? Curious Paul Tretter

 [2/6] 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

 [3/6] from: brett::codeconscious::com at: 14-Feb-2001 12:23


I believe it preallocates memory thus avoiding the costs of automatic expansion of storage. Try this: REBOL [ Author: "Brett Handley" Title: "Memory allocation test" ] max-count: 100000 prealloc-results: copy [] noalloc-results: copy [] for test 1 5 1 [ a: now/time/precise test-string: make string! 0 for i 1 max-count 1 [insert tail test-string "BlahBlah"] b: now/time/precise append noalloc-results probe subtract b a a: now/time/precise test-string: make string! 500000 for i 1 max-count 1 [insert tail test-string "BlahBlah"] b: now/time/precise append prealloc-results probe subtract b a ] print "Unallocated Results" print mold noalloc-results print "Preallocated Results" print mold prealloc-results Brett.

 [4/6] from: ptretter:norcom2000 at: 13-Feb-2001 19:36


Thanks Brett and Larry for replying. I now have a better understanding of this stuff. I luv this list. Paul Tretter

 [5/6] from: larry:ecotope at: 13-Feb-2001 18:04


Hi Brett Nicely made example of the benefits. They are even more striking if the line test-string: make string! 500000 is changed to test-string: make string! 800000 to allow for 100000 copies of the 8 char string "BlahBlah". Preallocation reduces the time by more than a factor of three on my box using latest Core exper build. Cheers -Larry

 [6/6] from: brett:codeconscious at: 15-Feb-2001 14:58


> test-string: make string! 800000 > > to allow for 100000 copies of the 8 char string "BlahBlah".
Eheh. That helps doesn't it? Ta.
> Preallocation reduces the time by more than a factor of three on my box > using latest Core exper build. > > Cheers > -Larry
2.7 times on my box after the change. Brett.