Possibly Pythonic Tail Call Optimization (TCO/TRE)

Chris Angelico rosuav at gmail.com
Thu Jul 16 13:03:48 EDT 2015


On Fri, Jul 17, 2015 at 2:50 AM, Joonas Liik <liik.joonas at gmail.com> wrote:
> Wouldn't it be possible to have like a dynamically
> sized stack so that you can grow it endlessly
> with some acceptable overhead..
>
> That would pretty much take care of the stack-overflow
> argument without many painful side effects on
> the semantics at least..

The trouble with that is that it can quickly run you out memory when
you accidentally trigger infinite recursion. A classic example is a
simple wrapper function...

def print(msg):
    print(ctime()+" "+msg)

With the recursion limit at its default of 1000, this can't do more
than 1000 iterations before the system detects that it's stuck. With
an infinite stack, this could destroy your system memory and then
finally terminate with MemoryError... if you're lucky. If you're not,
the whole system could get wedged before this gets killed. (Imagine,
for instance, a computer with a large hard disk devoted to virtual
memory. Long before that's exhausted, the system will be practically
unusable, because every little operation will require going back to
the disk.)

ChrisA



More information about the Python-list mailing list