PEP 697 – Limited C API for Extending Opaque Types

Here’s my belated critique of PEP 697.

In summary

Overall, I agree that this is a problem that needs solving, and the idea
of decoupling the structs needed for extension classes from the object layout
is important.

However, the details are too tied to the current VM implementation and will hurt
our ability to improve the VM in the future.

The overhead of using this API is likely to be too large for tools like Cython and
MyPyC, as well as extensions like NumPy. If the API isn’t good enough to implement tuple or list using it, then it is unreasonable to expect third-parties to use it as well.

Detailed criticisms

The section of variable-size layouts is too tied to the existing implementation, which we are trying to move away from.
We already moved the __dict__ to a fixed offset in Python objects, which allows much faster attribute access.
The only reason we can’t do that for variable sized objects like subclasses of tuple, is the constraints imposed by the C API. Let’s not make the situation worse.

The decision tree for layout seems too complicated. This is likely to lead to errors in understanding, implementation, or both.

PyObject_GetTypeData and PyType_GetTypeDataSize should be defined by what they do, not how they are implemented.
In other words, instead of defining PyObject_GetTypeData(obj, cls) as return (char *)obj + _align(cls->tp_base->tp_basicsize);
it should be defined as “returns a pointer to the struct for class cls within the object obj”.

The parts I like

The Py_RELATIVE_OFFSET is good, as it decouples field access from the absolute layout, which is what we want.
Putting the offset calculations behind an API is also a step forward.

What do I suggest instead?

Something like this:

Grand Unified Python Object Layout · Issue #553 · faster-cpython/ideas · GitHub

1 Like