Re: Question for Rob on memory usage
- Posted by Robert Craig <rds at RapidEuphoria.com> May 16, 2002
- 370 views
Andy Serpa writes: > Now I know if I do this: > > seq_B = seq_A > > Euphoria doesn't actually copy the sequence into a new memory location, > just points to the existing one for A unless one of them is altered. Right. > But what if I do this? > > seq_A = append(seq_A,seq_A) > > Now A has two copies of the same thing. No. I believe Euphoria will create a *new* seq_A that holds an extra element which points at the data for the *old* seq_A. > If it takes extra memory, would doing this be any better: > > seq_B = {seq_A,seq_A} In this case, seq_B will have 2 pointers to a single copy of seq_A. > If those DON'T take up any extra memory, can I also do this: > > seq_A = append(seq_A,seq_A[1]) Again, you are creating a *new* seq_A with an extra element. > I'm working on a program where having multiple copies of the same thing > at different indexes will be convienent, but I don't want to be using up > the extra memory because some of the items will be quite large. You can > assume that although the sequence may be appended to, none of the > individual elements of the sequence will be altered once created. It > will also be convenient to append to a sequence and element that occurs > earlier in the sequence. I could use some sort of pointer system and > just store indexes that refer to another sequence, but the first way is > easier if it doesn't matter. When there are 2 or more pointers to a sequence, and an element is overwritten, Euphoria will make a new copy of that sequence, but that does *not* mean that it will make a copy of the deeper nested data. Only the top-level of the sequence is copied. There is no need to copy floating-point numbers and sequences contained within the main sequence. For example, if you sort a sequence of strings, you'll end up with a different sequence of strings, but the individual strings are not copied or altered in any way. The new and old sequences point at the same strings, but the pointers are in a different order. Regards, Rob Craig Rapid Deployment Software http://www.RapidEuphoria.com