-
Python famously does not have tail call optimization as Guido van Rossum is against it. Since Mojo aims to be fully compatible with Python, what would its compiler do when it encountered an opportunity for this optimization? Would it check if the code is pure Python and then avoid performing TCO but perform it when it encounters Mojo-specific code? Would it provide a decorator like |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Yes, we will support tail call optimization and it will probably be enabled by default. If needed, we can add a decorator to disable it though. |
Beta Was this translation helpful? Give feedback.
-
It's been about 8 months since I said that. I've learned a lot since then. Cringing at myself suggesting thunks, trampolines and CPS be added as standard library features to a PL that already has first class functions aside, I have some more ideas for Mojo supporting tail call optimization. Mojo already supports1 tail recursion optimization (tail calls to the same function are replaced with jump instructions and reuse the stack frame), but it's not clear whether it supports tail call optimization (tail calls to any function are replaced with jump instructions and reuse the stack frame). These may not be exactly the right terms but they convey the difference I'm trying to get across. Generalizing tail call optimization to any function means that patterns like Continuation Passing Style2 and Finite State Machines3 can easily be expressed. I'm also still convinced that it's better to syntactically guarantee the optimization so programmers don't have to second guess themselves (without this, an especially big footgun would be tail calls that implicitly use the # I'm relying on TCO here. If you are modifying this function, make sure it still performs TCO or the stack will blow up This can be done in two ways:
I also hope Mojo's support for TCO isn't as restrictive as
Additional Reading:
Footnotes |
Beta Was this translation helpful? Give feedback.
Yes, we will support tail call optimization and it will probably be enabled by default. If needed, we can add a decorator to disable it though.