1. Question for Rob on memory usage

Hello,

Let's say:

seq_A = "This is sequence A."


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.  
That's good.

But what if I do this?

seq_A = append(seq_A,seq_A)

Now A has two copies of the same thing.  Does it take up twice as much 
memory now, or do I get the same efficiency as before?

If it takes extra memory, would doing this be any better:

seq_B = {seq_A,seq_A}

?

If those DON'T take up any extra memory, can I also do this:

seq_A = append(seq_A,seq_A[1])

?


and STILL not use up any extra? (Obviously you have to store the 
structure somewhere in memory -- I'm talking about for the elements 
themselves).

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.


????

new topic     » topic index » view message » categorize

2. Re: Question for Rob on memory usage

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

new topic     » goto parent     » topic index » view message » categorize

3. Re: Question for Rob on memory usage

Andy Serpa writes:
> So if I understand you correctly, the bottom line is I can do any of 
> that sort of stuff without incurring (i.e. doubling) extra memory usage 
> as long as I never alter the elements themselves? 

Yes.

> Basically, I'm going to be reading in the contents of text files into a 
> sequence.  Each file will equal one element (top-level) of the sequence. 
> But sometimes it will want to load in a file it already has loaded in 
> earlier in the sequence, so I want to be able to just  scan the contents 
> of the existing sequence for the filename that is being requested 
> (filename is also stored), and if it is already loaded in, then just 
> append to the sequence that same element again instead of actually 
> loading in the file again and wasting memory...

That should work fine.

Regards,
   Rob Craig
   Rapid Deployment Software
   http://www.RapidEuphoria.com

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu