Re: Constructive criticism
- Posted by jimcbrown (admin) Mar 08, 2009
- 1011 views
Your example is quite typical EU code:
- Relies on a global variable (I refer to lifetime here, not visibility!) "stacks", because of lack of PBR
This is really a matter of style. (Although I agree that PBR would be nice to have in any case.)
- Because of this a "delete" operation is needed that the user code needs to call when done with a stack; otherwise the result is a memory leak! This should be totally unnecessary in a language with GC because the stack is cleary not an external resource.
Agreed. A generic solution to this is still being discussed for 4.0 - this has turned into a huge problem in 4.0
- Common subexpression elimination is impossible to do efficiently in EU. Lets try with this code:
public procedure rot(integer stack) object temp validate(stack) if length(stacks[stack]) > 2 then temp = stacks[stack][$] stacks[stack][$] = stacks[stack][$-1] stacks[stack][$-1] = stacks[stack][$-2] stacks[stack][$-2] = temp end if end procedure
Hm, it sucks to refer to "stacks[stack]" all the time,
Hmm, you would prefer:
#define STACKS stacks[stack]
?
[quote Critic] but avoiding this introduces unnecessary sequence copies:
public procedure rot(integer stack) object temp validate(stack) object s = stacks[stack] -- we do not copy here: COW if length(s) > 2 then temp = s[$] s[$] = s[$-1] -- Ooops: here s is copied completely!
[quote Critic]
The parser (the bit of code that turns the euphoria source text into IL, a sort of bytecode) sees that stacks[stack] isn't used again until its completely replaced, which means that its safe to change s without separating the change from stacks[stack]. So it is smart enough to optimize the copy away.
[quote Critic]
s[$-1] = s[$-2] s[$-2] = temp stacks[stack] = s -- COW
[quote Critic]
Say what? I see only an O(1) operation occuring here.
end if end procedure
So an optimization that appears in the manual (!) makes the algorithm O(n) instead of O(1). Ah, the "efficiency" and "elegance" of Euphoria.
Concurred. ;)