Concerns about PEP 620 ("Hide implementation details from the C API")

Just a quick update: I am currently going down the rabbit hole to see if nanobind could be realized using only Py_LIMITED_API, and what the performance implications would be. It’s generally found it possible to emulate missing functionality (like PyObject_Vectorcall) with some potential performance cost that will be interesting to analyze once this all works. I also realized that implementing the main dispatch function using vector calls (the calleee) is actually possible with the limited API, which is the important part.

After going through everything, there is really just one blocker that I haven’t been able to work around – I need PyType_FromModuleAndSpec to be able to allocate extra space for the binding-related data structures. The previously linked PR (PyType_FromSpec should take metaclass as an argument · Issue #60074 · python/cpython · GitHub) contained a reasonable-looking patch but the discussion there seems to have stalled. Does anybody known more?

2 Likes

Why don’t you open a ticket to have the vector calls added to the limited API, so that you don’t have to go through such emulations ?

BTW: I hope that you are making the use of the limited API an optional feature in nanobind and not the default.

Python’s success in data science and other data heavy industries in general is based on being able to tap into fast C code without much overhead.

The Limited C API is really only suited for applications which are not data heavy and where a few extra round-trips, slow list/tuple item setting and copying of data don’t hurt.

Given the many good CI/CD tools we have nowadays, I also don’t think that compiling packages for the standard set of architectures is an important factor to consider anymore, given the greater flexibility and performance you get from using the standard public Python C API. This was an issue 13+ years ago (and the motivation for creating the Stable ABI), when such tooling was not readily available and compiling on Windows was not possible without buying MSVC.

Disclosure: I’m not a big fan of the limited API and stable ABI and prefer to use the Python C API at full speed :slight_smile:

1 Like

I hope that you are making the use of the limited API an optional feature in nanobind and not the default.

Victor mentioned above that the performance implications of the stable API should be benchmarked, hence that’s what I am trying to do. If I can get this to work, it would naturally be an optional feature.

3 Likes

I’ve been some progress on this and posted a PR adding needed functionality (gh-60074: add new stable API function PyMetaType_FromModuleAndSpec by wjakob · Pull Request #93012 · python/cpython · GitHub).

The discussion is steadily moving away from the original topic of this thread, which were concerns about the limited API :smile:. I’ll open a new thread if I encounter further issues.

2 Likes