1. references and passing by value

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.

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?

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.

cheers, kobi

new topic     » topic index » view message » categorize

2. Re: references and passing by value

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 message » categorize

3. Re: references and passing by value

great, thanks smile

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

4. Re: references and passing by value

kobi said...

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.

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?

It depends on how you have designed the linked list. If the entire list is contained in a single sequence, then each modification to it may involve copying data - removing elements from the front or back of the sequence will not cause data copying, and sometimes there is enough 'free space' in a sequence to avoid some data copying when adding elements.

Note that when a sequence is passed to a routine, even though the semantics is pass-by-value, at the low-level, only a pointer to the sequence is actually passed. And if the sequence is never modified by the routine, a copy of it is not taken.

Have a look at the std/eumem.e module. It emulates traditional RAM access with a malloc()/free() pair of functions. With this, one can build a linked list (or any other sort of linked structure) without having to force copies of sequences to be made. You can emulate pass-by-reference with this module.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu