1. Garbage and macros

I heard mention of garbage collection recently. Does Euphoria have a fully
automatic garbage collection system like Java ? If I read a whole file into a
sequence, should I null the sequence after I have finished ?

I had an idea yesterday, which may not be original, or may even be old stock.
If it is, use whatever form of DELETE button you have available.

In some assemblers you have the option of macros. These are like subroutines
except that they are inserted into the code at compile-time ie they run
inline. This of course saves the time of a subroutine call, and also saves
repeating code. This could be extremely handy in Euphoria, for example if you
use the same short piece of code several times in a prog, and call it a large
number of times eg

macro stuff(integer a)
    return power(a+1,2)-3
end macro

sequence temp
temp={}

for a=1 to 1000000
    temp=temp&stuff(a)
end for

The other *real* advantage of macros is that they could allow conditional
compilation.

if condition then
    macro stuff(integer a)
        return power(a+1,2)-3
    end macro
else
    macro stuff(integer a)
        return a       --This compiles to nothing
    end macro
end if

Then when you run your for-loop, you have saved 1000000 wasted function calls.

Just an idea.

Daniel Johnson
Les Mailles Telesoft

new topic     » topic index » view message » categorize

2. Re: Garbage and macros

> The other *real* advantage of macros is that they could allow conditional
> compilation.

And, in a loop, it would also save the overhead of making however many
function calls it needs to make because the code is inline. I like this
idea a lot, myself.

Waiting for the Unix version ... smile

--
Cameron Kaiser * spectre at sserv.com * http://www.sserv.com/
--
Visit the leading Internet starting point today!
http://www.sserv.com/
--

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

3. Re: Garbage and macros

Daniel Johnson writes:
> I heard mention of garbage collection recently. Does Euphoria
> have a fully automatic garbage collection system like Java ?

Yes, although Euphoria and Java implement garbage collection
quite differently, and Euphoria's data structures are fundamentally
more flexible than Java's, which are more similar to those in C++.
In Java you must allocate space using the "new" operator.
In Euphoria it's completely automatic.

> If I read a whole file into a sequence, should I null the sequence
> after I have finished ?

If you need to save space, it may help to set a large sequence
to {} when you no longer need it's value, but remember that
Euphoria will automatically free all the private variables of a routine
when that routine returns. Whenever you assign a new value to
a variable, any space used by the previous value is recycled.

> In some assemblers you have the option of macros.
> These are like subroutines except that they are
> inserted into the code at compile-time ie they run
> inline. This of course saves the time of a subroutine call,
> and also saves repeating code. This could be
> extremely handy in Euphoria, for example if you
> use the same short piece of code several times
> in a prog, and call it a large number of times eg

Rather than adding a new language keyword or feature,
I'd prefer to build a smarter optimizer, that will automatically
in-line small routines when converting source code to
intermediate code.

> The other *real* advantage of macros is that they could allow
> conditional compilation.
> if condition then
>    macro stuff(integer a)
>        return power(a+1,2)-3
>    end macro
> else
>    macro stuff(integer a)
>        return a       --This compiles to nothing
>    end macro
> end if

Again, I'd rather have the optimizer avoid generating
intermediate code, when it's obvious at compile-time that
an if-else condition is going to always be true or always be false.

> Then when you run your for-loop, you have saved 1000000 wasted
> function calls.

The overhead of making a call depends on how many arguments
you are passing etc. The overhead of a 0-argument procedure call
is less than the cost of two integer multiplies (on my machine).
A 1-argument call is cheaper than 3 multiplies. If you are doing
very little inside a routine, and you are calling it a lot,  you'll see a
significant speedup by in-lining the code. This is just as true
in C or any other language. I don't believe that procedure/function
calls are particularly expensive in Euphoria.

Regards,
     Rob Craig
     Rapid Deployment Software

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

4. Re: Garbage and macros

> If you need to save space, it may help to set a large sequence
>  to {} when you no longer need it's value, but remember that
>  Euphoria will automatically free all the private variables of a routine
>  when that routine returns. Whenever you assign a new value to
>  a variable, any space used by the previous value is recycled.

If it is in the main program code, would it be unrealistic to expect the
interpreter to figure out that the variable is not used again ? It would be
nice to have memory matters taken completely off our hands, as this is one of
the things Euphoria boasts about eg dynamic resizing and reformatting.

Daniel

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

5. Re: Garbage and macros

>Rather than adding a new language keyword or feature,
>I'd prefer to build a smarter optimizer, that will automatically
>in-line small routines when converting source code to
>intermediate code.

Of all the suggestions that fly past, this is one I'd really like
to see happen.  :-.)    <- Cindy Crawford smiley.

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

6. Re: Garbage and macros

Rob Craig wrote:
>> If you need to save space, it may help to set a large sequence
>>  to {} when you no longer need it's value, but remember that
>>  Euphoria will automatically free all the private variables of a routine
>>  when that routine returns. Whenever you assign a new value to
>>  a variable, any space used by the previous value is recycled.

Daniel Johnson asks:
> If it is in the main program code, would it be unrealistic to
> expect the interpreter to figure out that the variable is
> not used again ? It would be
> nice to have memory matters taken completely off our hands,
> as this is one of the things Euphoria boasts about
> eg dynamic resizing and reformatting.

It's theoretically impossible for any language compiler to
*always* know if a variable will be used again, but it is
possible in certain *limited* cases, using something called
"data flow" analysis. I implemented data flow analysis
in another compiler I developed a several years ago, but I'm not
sure if or when I will do it for Euphoria.

Regards,
     Rob Craig
     Rapid Deployment Software

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

Search



Quick Links

User menu

Not signed in.

Misc Menu