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

[REBOL] Re: Rebol Memory Allocation Strategy?

From: joel:neely:fedex at: 22-Sep-2002 8:00

Hi, Scott, No pro, just prose... ;-) G. Scott Jones wrote:
> First script rerequests a string within the function. memory > usage fluctuates a bit but adds a fair bit of time with > numerous iterations: > > Second script only sets up the string once, memory usage > remains essentially flat, and it is four times faster than > the first. > > 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. >
IMHO "better" is a function of many things, so I prefer to use more descriptive terms most of the time, especially in cases where there are significant trade-offs. Allocating a fresh string each time is more secure, as it reduces the chances of inadvertent coupling. For example, in: a: func-returning-string-ref b: func-returning-string-ref append a "foo" if the function returns a reference to the same string (CLEARed or not), then the last expression also mutates the value of B. This *might* be what the programmer intended, but if not, it is a very fertile source of subtle bugs. IIRC, the "classic" C implementation of date formatting used a single internal buffer for the result, returning a pointer to that same buffer on every use. This was almost universally reviled as a source of needless bugs and confusion. Re-using the same string (possibly with clearing) certainly has the advantage of speed, but one must make *sure* that there's not any possibility that references from a prior evaluation are still hanging around with the expectation that their former value(s) will persist. IMHO this is a stellar example of how tweaking a lower-level bit of code for some presumably noble purpose can end up making life much more complex for later, higher-level uses of said code. I suggest that the author of a function should almost always try to maximize simplicity of use for the caller, even at the expence of making his/her own life more difficult. Complexity resembles entropy in this regard; it's much easier to open a can of worms than it is to re-can them! -jn- -- ; Joel Neely joeldotneelyatfedexdotcom REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] { | e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]