Can I Change The Signature Of `ob_item` Of PyTupleObject And PyListObject?

by ADMIN 75 views

Introduction

In the Python C API, the ob_item field of PyTupleObject and PyListObject is currently defined as a pointer to a PyObject **. However, there is a proposal to change this to a pointer to a _PyHeapRef *, which is an alias for a struct containing a uintptr_t. This change does not affect the size of the struct or the offsets of the fields. In this article, we will explore whether this change requires a PEP (Python Enhancement Proposal) and discuss the implications of such a change.

Background

The ob_item field is used to store the items of a tuple or list. In the case of a tuple, it is a pointer to an array of PyObject * pointers, while in the case of a list, it is a pointer to an array of PyObject * pointers that can be dynamically resized. The current implementation uses a PyObject ** to store the items, which allows for dynamic resizing of the list.

Proposed Change

The proposed change would replace the PyObject ** with a _PyHeapRef *, which is an alias for a struct containing a uintptr_t. This change does not affect the size of the struct or the offsets of the fields, as the _PyHeapRef struct is the same size as a uintptr_t. The _PyHeapRef struct is used to store a reference to an object in the heap, and the uintptr_t is used to store the address of the object.

Implications

The proposed change has several implications:

  • Memory Layout: The memory layout of the PyTupleObject and PyListObject structs would remain the same, as the size of the _PyHeapRef struct is the same as a uintptr_t.
  • Alignment: The alignment of the ob_item field would remain the same, as the _PyHeapRef struct is the same size as a uintptr_t.
  • Access Patterns: The access patterns for the ob_item field would remain the same, as the _PyHeapRef struct is used to store a reference to an object in the heap.
  • Performance: The performance of the ob_item field would remain the same, as the _PyHeapRef struct is used to store a reference to an object in the heap.

PEP Requirements

A PEP is required for any change to the Python C API that affects the ABI (Application Binary Interface) or the API. The proposed change does not affect the ABI or the API, as the size of the struct and the offsets of the fields remain the same. However, a PEP is still required to document the change and provide a clear explanation of the reasoning behind it.

Conclusion

In conclusion, the proposed change to the ob_item field of PyTupleObject and PyListObject does not require a PEP, as it does not affect the ABI or the API. However, a PEP is still required to document the change and provide a clear explanation of the reasoning behind it. The change has several implications, including memory layout, alignment, access patterns, and performance, but these implications all neutral.

References

Discussion

The proposed change has been discussed on the Python Discourse forum, where several developers have expressed their opinions on the matter. Some developers have expressed concerns about the potential impact on performance, while others have argued that the change is unnecessary and would not provide any significant benefits.

Code

The proposed change would involve modifying the PyTupleObject and PyListObject structs to use a _PyHeapRef * instead of a PyObject ** for the ob_item field. The modified structs would look like this:

typedef struct {
    PyObject_VAR_HEAD
    _PyHeapRef *ob_item;
    Py_ssize_t allocated;
} PyListObject;

typedef struct {
    PyObject_VAR_HEAD
    _PyHeapRef *ob_item;
    Py_ssize_t allocated;
} PyTupleObject;

Q: What is the current implementation of ob_item in PyTupleObject and PyListObject?

A: The current implementation of ob_item in PyTupleObject and PyListObject is a pointer to a PyObject **. This allows for dynamic resizing of the list.

Q: What is the proposed change to ob_item?

A: The proposed change is to replace the PyObject ** with a _PyHeapRef *, which is an alias for a struct containing a uintptr_t. This change does not affect the size of the struct or the offsets of the fields.

Q: Why is the change proposed?

A: The change is proposed to improve the performance and memory usage of the ob_item field. The _PyHeapRef struct is used to store a reference to an object in the heap, which can be more efficient than using a PyObject **.

Q: What are the implications of the change?

A: The implications of the change are:

  • Memory layout: The memory layout of the PyTupleObject and PyListObject structs would remain the same.
  • Alignment: The alignment of the ob_item field would remain the same.
  • Access patterns: The access patterns for the ob_item field would remain the same.
  • Performance: The performance of the ob_item field would remain the same.

Q: Does the change require a PEP?

A: A PEP is required for any change to the Python C API that affects the ABI (Application Binary Interface) or the API. However, the proposed change does not affect the ABI or the API, as the size of the struct and the offsets of the fields remain the same.

Q: What are the benefits of the change?

A: The benefits of the change are:

  • Improved performance: The _PyHeapRef struct is used to store a reference to an object in the heap, which can be more efficient than using a PyObject **.
  • Improved memory usage: The _PyHeapRef struct is used to store a reference to an object in the heap, which can be more efficient than using a PyObject **.

Q: What are the potential drawbacks of the change?

A: The potential drawbacks of the change are:

  • Complexity: The change may add complexity to the code, as it requires modifying the PyTupleObject and PyListObject structs.
  • Backward compatibility: The change may break backward compatibility with existing code that relies on the current implementation of ob_item.

Q: How can I provide feedback on the proposed change?

A: You can provide feedback on the proposed change by commenting on the Python Discourse forum thread where the change was proposed.

Q: What is the next step in the process?

A: The next step in the process is to discuss the proposed change further and gather feedback from the community. If the change is deemed acceptable, it will be implemented and a PEP will be written to document the change.

Q: Can I try out the proposed change

A: Yes, you can try out the proposed change by modifying the PyTupleObject and PyListObject structs to use a _PyHeapRef * instead of a PyObject ** for the ob_item field. However, please note that this is an experimental change and may not be backward compatible with existing code.