classification
Title: Remove the usage of the C stack in Python to Python calls
Type: Stage: patch review
Components: Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Christian.Tismer, Mark.Shannon, corona10, gvanrossum, kj, pablogsal, serhiy.storchaka, steve.dower, vstinner
Priority: normal Keywords: patch

Created on 2021-09-21 10:41 by pablogsal, last changed 2021-10-18 08:57 by Mark.Shannon.

Pull Requests
URL Status Linked Edit
PR 28488 merged pablogsal, 2021-09-21 10:42
PR 28836 merged pablogsal, 2021-10-09 16:07
PR 28905 merged pablogsal, 2021-10-12 16:43
PR 28937 merged Mark.Shannon, 2021-10-13 16:48
Messages (17)
msg402311 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-09-21 10:41
Removing the usage of the C stack in Python-to-Python calls will allow future optimizations in the eval loop to take place and can yield some speed ups given that we will be removing C function calls and preambles by inlining the callee in the same eval loop as the caller.
msg402429 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-09-22 12:52
AFAIK Mark Shannon proposed this idea, but it was rejected.
msg402430 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-09-22 13:04
What was rejected was https://www.python.org/dev/peps/pep-0651/ which included this idea but had a lot more stuff in it. In particular, it was rejected because it gave semantics to overflow exceptions (two exceptions were proposed), new APIs and it had lack consistent guarantees for different platforms, among other considerations.

This version is just for optimization purposes with no changes in semantics.
msg402485 - (view) Author: Christian Tismer (Christian.Tismer) * (Python committer) Date: 2021-09-23 09:39
Hey guys, you know that you are about to implement the core idea of Stackless Python, right? :-D
msg402491 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-09-23 11:05
I've trying to do this since about 2011 :)
msg402504 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2021-09-23 15:59
I fully support implementing the core idea of Stackless Python :)

I spent a whole EuroPython a couple of years back discussing the idea with (apparently) everyone except Mark.

Though I wouldn't like to lose the ability to extract the Python stack by inspecting native memory alone. That is very useful for debugging, particularly when you've only got a crash dump. So provided all the code objects are only an indirection or two away from a local (or better yet, parameter) value, it should be fine.
msg402518 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-09-23 18:01
> Though I wouldn't like to lose the ability to extract the Python stack by inspecting native memory alone.

Don't worry about it, I am personally making sure that keeps being possible. it will need some changes in the tools, but not any more that any other changes between minor versions of the interpreter
msg402546 - (view) Author: Christian Tismer (Christian.Tismer) * (Python committer) Date: 2021-09-24 09:16
FYI., in Stackless Python I built a chain of frames by a double linked list. This was the replacement for the current frame/stack mix.
Debugging was still quite easy, following this frame chain.

Note that it is a rather easy step to add the capability to squirrel the whole current chain away and replace it by another one. Add some header to such a chain, and you can call it "Tasklet".
msg402763 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-09-28 10:03
PR 28488 has no NEWS entry, or What's New entry.

However, adding multiple entries will be confusing, so that's best left until all calls to Python functions and method don't use the C stack.
msg402766 - (view) Author: Christian Tismer (Christian.Tismer) * (Python committer) Date: 2021-09-28 11:11
FWIW, getting all function to avoid the C stack will most probably take a long time, if it happens at all. Especially functions which might call into Python multiple times must be re-designed heavily, turned into multiple pieces to be in tail position. Been there, long ago... :)
msg402767 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-09-28 11:41
We are starting to optimize first the easy cases of CALL_FUNCTION and move slowly to add more and more calls into it. This approach has the advantage that allow us to take it in small increments.
msg402780 - (view) Author: Christian Tismer (Christian.Tismer) * (Python committer) Date: 2021-09-28 14:57
Very much appreciated approach. 
Too bad that things stop when people are writing extensions as usual. Or do you think we can teach them how to avoid the C stack? Anyway, good luck!
msg402785 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2021-09-28 16:09
The goal is reduced stack depth, not reframing the entire call model around not having a C stack.

We can't even reasonably rewrite getattr() without supporting callbacks from C into Python, so further generalisation is very unlikely.

But if you inspect the native stack of most Python programs, you'll see that it's mostly taken up with calls within Python code. Compressing all of those is a significant advantage, akin to inlining the Python code at compile time, even if it doesn't see "through" native calls.
msg403538 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-10-09 15:51
New changeset b4903afd4debbbd71dc49a2c8fefa74a3b6c6832 by Pablo Galindo Salgado in branch 'main':
bpo-45256: Remove the usage of the C stack in Python to Python calls (GH-28488)
https://github.com/python/cpython/commit/b4903afd4debbbd71dc49a2c8fefa74a3b6c6832
msg403543 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-10-09 16:52
New changeset 543acbce5a1e23633379a853f38dc55b12f6d931 by Pablo Galindo Salgado in branch 'main':
bpo-45256: Small cleanups for the code that inlines Python-to-Python calls in ceval.c (GH-28836)
https://github.com/python/cpython/commit/543acbce5a1e23633379a853f38dc55b12f6d931
msg403832 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-10-13 13:38
New changeset 3901c081143ef29624f9c1cb49cc70a70321d139 by Pablo Galindo Salgado in branch 'main':
bpo-45256: Fix cleanup of stolen locals for Python-to-Python calls (GH-28905)
https://github.com/python/cpython/commit/3901c081143ef29624f9c1cb49cc70a70321d139
msg404166 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-10-18 08:57
New changeset 70945d57e775b335eb58b734d82e68484063e835 by Mark Shannon in branch 'main':
bpo-45256: Avoid C calls for most Python to Python calls. (GH-28937)
https://github.com/python/cpython/commit/70945d57e775b335eb58b734d82e68484063e835
History
Date User Action Args
2021-10-18 08:57:28Mark.Shannonsetmessages: + msg404166
2021-10-13 16:48:56Mark.Shannonsetpull_requests: + pull_request27226
2021-10-13 13:38:49Mark.Shannonsetmessages: + msg403832
2021-10-12 16:43:59pablogsalsetpull_requests: + pull_request27196
2021-10-09 16:52:09pablogsalsetmessages: + msg403543
2021-10-09 16:07:50pablogsalsetpull_requests: + pull_request27152
2021-10-09 15:51:34pablogsalsetmessages: + msg403538
2021-09-28 16:09:12steve.dowersetmessages: + msg402785
2021-09-28 14:57:25Christian.Tismersetmessages: + msg402780
2021-09-28 11:41:33pablogsalsetmessages: + msg402767
2021-09-28 11:11:32Christian.Tismersetmessages: + msg402766
2021-09-28 10:03:21Mark.Shannonsetmessages: + msg402763
2021-09-24 09:16:19Christian.Tismersetmessages: + msg402546
2021-09-23 18:01:17pablogsalsetmessages: + msg402518
2021-09-23 15:59:03steve.dowersetnosy: + steve.dower
messages: + msg402504
2021-09-23 11:05:30Mark.Shannonsetmessages: + msg402491
2021-09-23 09:39:02Christian.Tismersetnosy: + Christian.Tismer
messages: + msg402485
2021-09-22 15:57:55vstinnersettitle: Remove the usage of the cstack in Python to Python calls -> Remove the usage of the C stack in Python to Python calls
2021-09-22 14:07:09vstinnersetnosy: + vstinner
2021-09-22 13:04:35pablogsalsetmessages: + msg402430
2021-09-22 12:52:08serhiy.storchakasetnosy: + serhiy.storchaka, gvanrossum
messages: + msg402429
2021-09-22 12:24:21corona10setnosy: + corona10
2021-09-21 14:47:35kjsetnosy: + kj
2021-09-21 10:42:59pablogsalsetkeywords: + patch
stage: patch review
pull_requests: + pull_request26884
2021-09-21 10:41:59pablogsalcreate