Re: question

new topic     » goto parent     » topic index » view thread      » older message » newer message

On Mon, 15 Nov 2004 19:48:46 -0800, Tone =8Akoda
<guest at RapidEuphoria.com> wrote:

>What would happen when when I would do 'tmp={}' before I modify 'data'?=

>Would there be any copying going on? Because if yes, interpreter at
>would have to do these two things: 1) delete memory of 'tmp' 2)
>allocate memory for 'data'. And these two memories would contain same
>data.
>
>This is one thing because of which I am thinking of switching to C,
>because there you have pointers and things like this are crystal clear.
LOL.
Just think of all Euphoria variables as pointers, and all data as
{refcount,data} pairs. The data can usually be modified "in situ" if
the refcount is one (some spare slack is left on sequences to allow
this), else a copy must be made.

I suppose there is a different mindset needed to think in refcounts,
but if you are claiming pointers are clear, then refcounts, once
understood, are even clearer. A refcount of zero means "deallocate
me", a refcount of 1 means "I'm all yours", and a refcount > 1 means
"make a copy and modify that". (Bet you're thinking "I knew that")

tmp={1,large}

 tmp ->seq of length 2, refcount of 1.  large now has a refcount + 1

data=tmp[2]

 data ->large. large now has a refcount + 2

tmp={}

 tmp ->seq of length 0. The previous length 2 thing now has a refcount
 of zero, so it is deallocated and the refcounts of anything it
 referred to are decremented. Hence large now has a refcount + 1
 It is important to grasp that the refcount on large has dropped
 because the {1,ptr} object's refcount dropped to zero, rather than
 directly as a result of tmp becoming {}. Note that tmp never referred
 to large, tmp referred to something which referred to large.
 In contrast, tmp2=tmp  tmp={} would not affect the refcount of large
 in any way, /not even temporarily/.

data=append(data,some)

 if large has enough "slack", and has a refcount of 1, and is not from
 the constant pool, then it can be modified directly, otherwise create
 a new (longer) top-level copy, copy the top level pointers, and
 increase the reference counts accordingly.

In the above, I've used +1, +2 on the refcount of large, as obviously
it depends on where the original came from. If it was an item from the
constant pool, then reference counts are pretty much irrelevant.

The main point is to keep the refcount of anything large at 1, at the
point you want to modify it.

Regards,
Pete

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu