Skip to content

bpo-47215: Add undocumented, unstable FrameStack API for use by greenlets and similar libraries. #32303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from

Conversation

markshannon
Copy link
Member

@markshannon markshannon commented Apr 4, 2022

Skipping news, as I want to keep this as low key as possible.
We want this for greenlets, we don't want it misused by other third party code.

@brandtbucher would this cover greenlet's needs?

https://bugs.python.org/issue47215

@@ -364,3 +368,16 @@ typedef int (*crossinterpdatafunc)(PyObject *, _PyCrossInterpreterData *);

PyAPI_FUNC(int) _PyCrossInterpreterData_RegisterClass(PyTypeObject *, crossinterpdatafunc);
PyAPI_FUNC(crossinterpdatafunc) _PyCrossInterpreterData_Lookup(PyObject *);

/* UNSTABLE API for stackful coroutines.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about hiding this behind an ifdef, it would technically be an api break, but would make the intention clearer:

#ifdef _PYTHON_C_UNSTABLE_FRAMESTACK_API
...
#endif

@brandtbucher
Copy link
Member

@brandtbucher would this cover greenlet's needs?

Sorry, I didn't get a chance to look at this too closely today. My plan is to attempt to modify my fork of Greenlet tomorrow to see how well it works with this branch (and then we'll have a patch we can upstream right away, too).

A few thoughts just from looking at this so far:

  • I don't see much value in exposing a tunable chunk_size parameter, even internally.
  • As I understand it, when it comes to the Python stack, Greenlet's needs are essentially limited to saving and restoring the state of an arbitrary thread.
  • These steps also happen at different points: saving happens here, restoring happens here.
  • To that end, I'm not sure if this init/swap/free model is a very good fit. A more useful API might be (roughly):
    • void *_PyThreadState_FrameStackSave(PyThreadState *tstate)
    • void _PyThreadState_FrameStackRestore(PyThreadState *tstate, void *saved)

We can also loop in the Greenlet maintainers and get their opinion on this. @jamadden?

@markshannon
Copy link
Member Author

I don't see much value in exposing a tunable chunk_size parameter

The default size is 64k, which is fine for a thread, but quite large for a coroutine. It costs nothing to expose it.

The problem with a save/restore model is that it has too many states and error conditions. What state is the VM in after mutliple saves, with no restore? Or vice versa?

For performance reasons the frame stack struct needs to be embedded in the thread state, so we can't return a pointer to it.

@brandtbucher brandtbucher removed their request for review May 10, 2023 22:02
@markshannon markshannon closed this Aug 6, 2024
@markshannon markshannon deleted the frame-stack-api branch August 6, 2024 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants