Hi, I am an HPy and GraalPy developer. Commenting as myself, no “official” HPy/GraalPy stance.
What is HPy, really?
It seems to me that HPy is often put into the same bucket as tools like pybind11 or Cython. The goal of HPy, in my view, is different. It is to design an API between the Python VM and 3rd party native extensions for Python.
The fact that the current reference implementation of HPy for CPython happens to be implemented on top of the CPython C API is an implementation detail that, I suspect, misleads people to see HPy like “other bindings”. To me such “bindings” are projects that provide some abstraction on top of the Python VM API (for the time being the CPython C API), but are not meant to be provided by the Python VM itself (now or in the future).
Why is this difference important? Because the goal of HPy is to enable the Python VM to hide and freely evolve its internals and still provide a performant API and stable ABI, which in the end should benefit also the packages and ecosystem as a whole.
We’ve shown that this works: in our experiments, HPy-based extensions performed significantly better on alternative Pythons (GraalPy and PyPy), because HPy allowed them to avoid the costly emulation of the CPython C API tailored for what happens to be CPython internals at this point. At the same time, HPy extensions can be binary compatible between CPython, GraalPy and PyPy. I think that this is a strong evidence that the HPy API (and ABI) would work reasonably well for any Python VM with whatever internal structure it may have.
HPy and CPython
CPython (and alternative Pythons) will have to support the CPython C API for the foreseeable future (and it is worthwhile to incrementally improve it in any case), but it doesn’t mean that CPython cannot provide an alternative API to the VM. HPy has shown that “legacy” C API and some new API can be mixed to provide a gradual migration path. If you port a single extension method to HPy, its invocations will already benefit from HPy; for example, the VM can pass tagged integers to it (like GraalPy does).
Over time, important packages would migrate (at least their most important parts) to the new API not just because it’s nice and shiny, but because it would provide real benefits on CPython (mainly performance and ABI stability once fully migrated). At that point, CPython can do what GraalPy/PyPy do: emulate the “old” API—it will be slower, but it won’t matter (so much) anymore, and CPython would be free to do almost any internal changes without breaking the ecosystem.
Current state of HPy
Right now HPy development is not very active. AFAIK it is because most of the maintainers have other commitments, but we are still keen advocates of the HPy ideas. We’ve given multiple presentations and shared our results and experiences. I believe that some of those inspired some CPython C API changes and more may inspire future changes. We are watching this space and try to contribute (these days mainly to design discussions about the CPython C API) where we see fit and think our expertise can be useful.
At this point, HPy is, in our view, a successful demonstration of the goals that drove its initial development. Now the question is: what next?
Future of HPy, Take 1
HPy can stay as a completely separate project from CPython. In such a case, the benefits of HPy for package authors would be: binary stability like stable ABI, but even for GraalPy and PyPy (and possibly any new Python VM), and potentially better performance on GraalPy and PyPy. Admittedly, for HPy to succeed on this front, we’d need more (active) contributors. The current contributors cannot allocate enough resources for HPy to fully push this forward, but I think we’ll be happy to mentor and help.
Future of HPy, Take 2
Imagine for a while that HPy or some API inspired by it would be declared as the Python VM API for 3rd party C extensions. It could be governed by the C API work group or even better by some work group consisting of representatives of CPython core devs, other Python VM devs, package authors and the community. In my opinion, in the long run, CPython would benefit and the ecosystem would benefit.
One ABI breakage is already on the horizon (Stable ABI/Limited API for free-threaded builds) and there are discussions about providing a new parallel API (Add a `PyThreadState *` parameter (almost) everywhere · Issue #132312 · python/cpython · GitHub). If it is going to happen sooner or later, why delay the effort? The migration of the ecosystem will take some time. I believe that, also thanks to HPy, we have good enough understanding of the problem to start solving it. Both linked issues (exposed ob_refcount
in old stable ABI) and explicit “context” parameter would have been solved with HPy.
If such a parallel API would not simply be included in Python.h, but instead in a new PythonAPI.h, the difference could be very clear and an incremental pathway to getting people off the old Python.h APIs and onto a better designed API specifically for 3rd party native extensions might be clearer.
One approach to such a new Python VM API could be to gradually implement parts of the HPy API in CPython (possibly adjust/polish/rethink some parts, but lots of work has been already done!) and at the same time already take advantage of it for better performance (I think there are already some situations where CPython must revert some optimizations when PyObject leaks to an extension). It can start as an API for extensions that just have simple module-level methods, then it may add support for basic heap types, then module state, then meta types, etc.
This HPy future take 2 would suffer from the same problem with lack of contributors, but I think it would be in a much better position for attracting new contributors (or re-attracting the old ones :-)) and getting adoption from extension authors, because it would provide benefits (performance) over the existing stable ABI even for extensions that do not care (so much) about alternative Pythons. Hopefully this would kick off the positive feedback loop between development and adoption.
What would be the next step? I think a prototype in CPython fork/branch that would show the potential benefits can help move this forward. This would be no small project. I don’t think there is anyone in HPy right now, who would be able to allocate enough time such such project, but again if there are people willing to push this effort, I believe the current HPy maintainers would be happy to mentor and help as much as their other commitments allow.