Why no tailcall-optimization?

Carl Banks pavlovevidence at gmail.com
Tue Sep 23 10:38:56 CEST 2008


On Sep 22, 9:13 pm, process <circularf... at gmail.com> wrote:
> Why doesn't Python optimize tailcalls? Are there plans for it?

The main technical difficulty is that the compiler has to know whether
the function returns a tail call or not at compile time.

But because Python is fully dynamic with regard to type, the compiler
never(**) knows anything about any object other than "it's an
object".  The compiler doesn't even know if the object is callable.

Probably it would be possible to achieve this optimization without
involving the compiler, but it'd be at cost of great complexity and
probably would negatively affect ordinary function calls (which are
already slow enough).

(BTW, if you're just talking about converting simple tail-recursive
functions, and not about general tail-call optimization, then I'd
suggest a third-party package would be better for that, since it would
be a pretty complex thing that would benefit only a tiny fraction of
users.)


Carl Banks


(**) Actually the compiler can do some compile-time constant
expression folding, but that's about it. Otherwise it's object
agnostic.




More information about the Python-list mailing list