2 ms·
The problem with TCO is that it makes it impossible to get accurate stack traces for a function. If you optimize away the stack frame, there's no way to tell t
by nostrademons 17y ago
The problem with TCO is that it makes it impossible to get accurate stack traces for a function. If you optimize away the stack frame, there's no way to tell the user "this is what was running when the error occurred." You get odd behavior where a function may appear to call another function which never appears in its program text, because the intervening stack frame has been optimized away.
Stack traces are part of the Python language (besides looking pretty, the traceback module lets you inspect them), so it'd be really hard for a conforming implementation to include TCO - at least the type that actually has tail recursion run in constant space.
Java faces a similar problem with HotSpot's inlining, which is why you occasionally get <compiled code> in a Java stack trace instead a line and file number. They seem to have some hack in to avoid eliding the stack frame entirely though. This is probably what I'd do if I were designing language: push a bare-bones stack frame with just the debug info, but pop all the unused temporary working storage. Or punt on general TCO and just optimize tail-recursion - isn't that what Python was proposing to do with the @tailcall decorator anyway?
The languages I know of with TCO - gcc and Scheme - have really terrible debugging support. IMNSHO, I think that giving up debugging info for TCO is a poor tradeoff.