Possibly Pythonic Tail Call Optimization (TCO/TRE)

Antoon Pardon antoon.pardon at rece.vub.ac.be
Wed Jul 15 04:28:26 EDT 2015


On 07/14/2015 07:48 PM, Steven D'Aprano wrote:
> On Wed, 15 Jul 2015 03:29 am, Marko Rauhamaa wrote:
>
>> Chris Angelico <rosuav at gmail.com>:
>>
>>> On Tue, Jul 14, 2015 at 11:38 PM, Marko Rauhamaa <marko at pacujo.net>
>>> wrote:
>>>> No, tail call optimization doesn't change the behavior of the
>>>> program, for the worse anyway.
>>> It does, because you lose traceback records. That's pretty significant
>>> when you come to try to debug something.
>> Doesn't count. 
> Says you.
>
>> Optimization can do weird things to code and make 
>> debugging challenging. That's why you usually tell the compiler not to
>> optimize the debug builds.
> Here's a real scenario for you to chew on: testing. Test suites should be
> considered an application where the developer is the end user, and the
> point of the application is to verify that the code passes tests, and if
> they don't, present the user (i.e. the developer) with sufficient
> information to debug the problem (i.e. full stack traces).

The stack traces in Test suites are generally pretty useless. If the code
doesn't pass a test, the stack trace you get, is the one provoked by an
assert like statement in the test code. At that moment the call to the
code to be tested is already done. So the stack trace won't provide any
information about the code just tested.

> So how do you test your optimized code?
>
> If you turn optimizations on globally, you're testing something which may or
> may not have the same behaviour as the code you wrote. Compiler bugs exist,
> so it is more important than ever to ensure that the post-optimization code
> passes the test suite. But if it fails, you no longer get the stack traces
> you need to debug the problem.

You don't need those stack traces to debug the problem. The only thing useful
they generally contain is the line that failed. Traceback records of the moment
some subcalculation in some subcall went wrong are not available. So TCO will
not be a real burden here.

-- 
Antoon Pardon




More information about the Python-list mailing list