Remix.run Logo
Show HN: Tacopy – Tail Call Optimization for Python(github.com)
32 points by raaid-rt 5 days ago | 7 comments
dkersten 25 minutes ago | parent | next [-]

Once upon a time I tried to write such a decorator too in python 2.x and the byteplay bytecode disassembler library. I was trying to do the conversion at the bytecode level instead of transforming the AST. I believe I got as far as detecting simple self recursive functions, but never actually managed to implement the actual transformation.

srean 44 minutes ago | parent | prev | next [-]

> Tacopy is a Python library that provides a decorator to optimize tail-recursive functions by transforming them into iterative loops.

Can this handle mutually recursive calls ? Because those are mostly the only place I use tail calls, rest I translate to iterative loops, list comprehension, maps and reduces.

pansa2 30 minutes ago | parent [-]

> Limitations […] No mutual recursion: Only direct self-recursion is optimized

javierbg95 44 minutes ago | parent | prev | next [-]

Really cool project, fairly succinct and to the point :)

I would love to see support for arbitrarily nested functions, as it is common to wrap these into a public API function without the iteration parameters.

phplovesong an hour ago | parent | prev | next [-]

TCO can be implemented easily in non TC optimized langauges with a trampoline wrapper.

Why do i need a fully fledged library for something that is basically a few lines of code?

srean an hour ago | parent [-]

There's quite a bit of overhead.

I believe Clojure does it with trampoline as JVM does not (as far as I know) does not support tail call optimization. Ironic, given Guy Steele.

anilakar 3 hours ago | parent | prev [-]

> This eliminates the risk of stack overflow errors

When you get stack overflows anywhere from a thousand down to fifty(!) frames in the stack it's not a risk, it's an inevitability in anything more complex than a programming tutorial.

Yeah, I've been bitten by this in production. Writing the functionality in a clean iterative style was just too much of a hassle.