[REBOL] Re: Rebol Memory Allocation Strategy?
From: gscottjones:mchsi at: 21-Sep-2002 18:58
Hi, Tim,
From: "Tim Johnson"
> I need clarification on rebol memory allocation. I
> programmed in C and Asm for years so am cognizant of
> architectural approaches to memory allocation (well, sort
> of). Let's say we have a function
>
> my-f: func[][str: make string! 8192 ; untested code
> ; appends much data to str....
> ]
>
> and that function is called any number of times.
>
> Now, does that mean that rebol has to allocate 8092 bytes
> of memory every time that 'my-f is called?
>
> ==>>If that is so then would not the following be more
> efficient?
>
> my-obj: make object![ ; untested code
> str: make string! 8192
> _my-f: func[][
> clear str
> ; appends much data to string...
> ] ; end function
> ] ;end object
>
> Would not 'str be allocated just once and that would
> be during evaluation of my-obj?
>
> Then 'my-f would only have to reset the "internal data
> pointer" which is trivial in terms of overhead.
> (presuming my-f would be 'happy' with 8192 bytes)
>
> Comments, nags criticisms appreciated..
I'm hardly an expert on anything. I wrote some little scripts just for fun,
testing memory useage and speed.
First script rerequests a string within the function. memory usage
fluctuates a bit but adds a fair bit of time with numerous iterations:
;#######First script
t: now/time/precise
reform [to-integer system/stats / 1024 "KB"]
my-f: func[][
str: make string! 8192
loop 20000 [append str "akslkjslkjslskjlsj"]
print reform [to-integer system/stats / 1024 "KB"]
]
loop 100 [my-f]
print now/time/precise - t
;#######
Second script only sets up the string once, memory usage remains essentially
flat, and it is four times faster than the first.
;########second script
t: now/time/precise
reform [to-integer system/stats / 1024 "KB"]
str: make string! 8192
my-f: func[][
clear str
loop 20000 [append str "akslkjslkjslskjlsj"]
print reform [to-integer system/stats / 1024 "KB"]
]
loop 100 [my-f]
print now/time/precise - t
;########
These scripts probably mean little, but they confirm the idea that it is
probably better to do less make-ing and more clear-ing. Maybe a real pro
can comment using my feeble scripts.
:-)
Hope this doesn't muddy the waters further.
--Scott Jones