3 ms·
I'll assume this is a good faith question and answer it by quoting the "What's new in Python 3.11?" page (linked below the quote). > During a Python function c
by giancarlostoro 5y ago
I'll assume this is a good faith question and answer it by quoting the "What's new in Python 3.11?" page (linked below the quote).
> During a Python function call, Python will call an evaluating C function to interpret that function’s code. This effectively limits pure Python recursion to what’s safe for the C stack.
> In 3.11, when CPython detects Python code calling another Python function, it sets up a new frame, and “jumps” to the new code inside the new frame. This avoids calling the C interpreting function altogether.
> Most Python function calls now consume no C stack space. This speeds up most of such calls. In simple recursive functions like fibonacci or factorial, a 1.7x speedup was observed. This also means recursive functions can recurse significantly deeper (if the user increases the recursion limit). We measured a 1-3% improvement in pyperformance.
https://docs.python.org/3.11/whatsnew/3.11.html#inlined-python-function-calls https://docs.python.org/3.11/whatsnew/3.11.html#inlined-pyth...
- SemanticStrengh 5y agoBTW It'd be nice if python implemented support for tail call recursive or even better, a growable/segmented stack in order to allow arbitrary pythons functions be stack overflow safe. e.g. https://gcc.gnu.org/wiki/SplitStacks#:~:text=Split%20Stacks%20in%20GCC&text=The%20goal%20of%20split%20stacks,as%20required%20by%20the%20program https://gcc.gnu.org/wiki/SplitStacks#:~:text=Split%20Stacks%....
- jleahy 5y agoThere was once stackless python that did this (Eve online was built in it), I don’t know if it still exists though.
- bqmjjx0kac 5y agoNeat! https://en.wikipedia.org/wiki/Stackless_Python https://en.wikipedia.org/wiki/Stackless_Python Sadly, it isn't true parallelism, more like green threads. And they didn't manage to kill the GIL.
- masklinn 5y agoIf Python doesn't depend on the C stack anymore (for most of its work), that's basically what you'll have for free. Python's own stackframes are heap-allocated and chained (so each function has its own stack, in essence), so its use of a single unified allocation space for stack is already an implementation detail of the interpreter.
- SemanticStrengh 5y agonice
- FreakLegion 5y agoWith Python it's possible to do tail-call optimization yourself. See this all-time great Stack Overflow answer and the linked repo for details (the actual code is quite short): https://stackoverflow.com/a/18506625 https://stackoverflow.com/a/18506625.