Re: references and passing by value

new topic     » goto parent     » topic index » view thread      » older message » newer message
kobi said...

uh, one more question for you euphoria gurus

let's say I have a linked list. now I want to remove one cell in the middle of this list,
so it would be joined with the next next cell.

Hmm...I wonder if you really have linked list. smile In euphoria, this would be somewhat difficult, since we don't have pointers. You can simulate them with native euphoria objects (see std/eumem.e). I'll assume that you really just have a sequence with stuff in there.

kobi said...

since euphoria passes data by value, what will the behaviour look like in the low level parts?
will it copy "all the rest" in the list, or is it as fast as a reference change, pointer change?

For sequences and atoms, euphoria uses reference counts and copy on write. At a low level, a sequence is a little bit of metadata, plus a C array of euphoria objects (i.e., pointer sized integers). If you remove an element from the array, then the elements after the removed element get shifted up. If there are other references to the sequence, then the whole sequence gets copied, minus the removed element. Otherwise, we just have to move the euphoria objects after the removed element up.

kobi said...

I'm trying to understand the mechanisms and semantics in order to know better the consequences of every action.
Maybe it's unnecessary? I may write a data structure, or something with more "objects" as my next euphoria script.

Unless you're doing something like this in an inner loop, or with a really large sequence, you probably won't notice the performance hit.

The biggest thing to remember is to use the new (in 4.0) built-in sequence operations (e.g., remove()). These are generally faster (sometimes by a lot) than the old versions using slices.

If you find that your sequence operations are a bottleneck, one way to speed them up is to not manipulate sub-sequences in place, but in their own variable:

sequence element = my_big_sequence[x]  -- just copies a pointer and adds a reference 
 
my_big_sequence[x] = 0 -- this prevents extra refcounts and copy on writes! 
 
-- ...manipulate element 
 
my_big_sequence[x] = element  -- copies a pointer and adds a reference 

But again, I wouldn't worry too much about this sort of optimization until you find that your code is slower than you like.

Matt

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

Search



Quick Links

User menu

Not signed in.

Misc Menu