Re: Optimization

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

Robert Craig wrote:
> 
> Jason Gade wrote:
> > Does Euphoria optimize read-modify-write situation?
> > }}}
<eucode>
> > sequence foo
> > foo = bar(foo)
> > </eucode>
{{{

> 
> I've discussed this before.
> No, I don't do this optimization,
> but I have it fairly high on my list to consider.

Yeah, I know. I think I'm the one that asked before.

> The basic idea is to reduce the reference count on 
> a variable value that you know is not going to be 
> needed again. So in x = sort(x) there would only
> be one reference count on the value of x that's passed to sort().
> That way, there would be no need to copy x inside sort()
> before modifying it. In general, you could look for situations
> where a variable is going to be overwritten before it is
> read again, therefore its value is no longer needed, and the
> reference count could be decremented early, possibly eliminating
> a copy.
> 
> In addition to being a bit tricky, and a bit dangerous,
> this optimization would leave you with some important variables
> "undefined" in ex.err and the trace screen, since their 
> values might be temporarily unknown. Also, copying a sequence
> is a fairly cheap operation compared with any other operation
> you can think of that's applied to each element. e.g. one copy
> does not increase the cost of sorting by very much

This is only really of academic interest to me, but bear with me please anyway.

I see a lot of other programming languages use modify-by-reference. They take a
reference to an existing variable and modify the contents of that variable.
Euphoria does a copy-on-write instead.

In Euphoria you can get around this by using a global variable. But then each
routine needs to know the name of that variable in order to modify it. That makes
it difficult to reuse code or to write libraries that want to handle large data
objects without incurring a copy.

It might be nice there was a way to tell the interpreter that I want to modify
*this* variable instead of making a copy of it. Say, like a keyword "static" or
something. "static" is the only keyword I could think of, but I don't like that
particular word much because of its overuse in C.

I know it's a pie-in-the-sky idea, and again it is only of academic interest to
me.

> > Unless you'd actually implement a var_id() that is orthogonal with
> > routine_id()...
> 
> It's too much like pointers.

Well, Euphoria already *has* pointers in the form of routine_id(). And the fact
that you can roll your own pointers with allocate()/peek()/poke(). In C, I find
variable pointers much easier to understand than function pointers, yet Euphoria
has function pointers (that are actually easy to understand).

But pointers are the 'goto' of data structures, as they say, and they would
definitely be abused in Euphoria. As any other construct can be.

I still think that, done right, some kind of mechanism could make data
encapsulation better.

--
"Any programming problem can be solved by adding a level of indirection."
--anonymous
"Any performance problem can be solved by removing a level of indirection."
--M. Haertel
j.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu