[nem-en] An idea on tail recursible functions
Kamil Skalski
kamil.skalski at gmail.com
Mon Jul 3 20:30:07 CEST 2006
2006/7/3, Snaury <snaury at gmail.com>:
> I don't know if this idea already was in people minds, but anyway.
> Imagine that you have a tail recursible function local to some method,
> which you need to call twice. Like this:
>
Actually getting rid of one / two calls is not very useful. Note that
in this case local function is changed into method, but its tail
recursive calls (inside of function) are still translated to simple
jumps, which are very fast. So in your example there would be
literally TWO method invocations.
Moreover, with the -Ot switch we (should) emit the '.tail call'
opcode, which would result in not increasing the stack by even single
frame. (we do not use this opcode by default, because it is still
slower on MS.NET than regular calls).
Anyway, we still generate quite much garbage for local functions
utilizing local variables / fields. So I guess if someone would like
to contribute time to optimizing the compiler's output, then it should
rather be this garbage.
We had some ideas of automatically translating non-tail-recursive
functions into tail ones, but this is generally hard.
>
> And because constructs like this are prohibited:
>
> def a(i)
> b(i)
>
> def b(i)
> a(i - 1)
>
They are not prohibited, though the syntax is slightly different
(after other ML languages):
def a(i)
b(i)
and b(i)
a(i - 1)
a(10)
--
Kamil Skalski
http://nazgul.omega.pl
More information about the devel-en
mailing list