Repository navigation
gh-161184: Use a managed dict and weakref list for the _io types #161236
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
Open
eendebakpt
wants to merge
4
commits into
python:main
Choose a base branch
from
eendebakpt:io-managed-weakref
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+113
−110
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
Misc/NEWS.d/next/Library/2026-11-03-08-21-06.gh-issue-161184.PIhcD9.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| Speed up attribute access on instances of :mod:`io` classes and their | ||
| subclasses by storing instance attributes and weak references in the object | ||
| pre-header. The ``__dictoffset__`` and ``__weakrefoffset__`` attributes | ||
| of these classes now report the managed offsets, and C extension types that | ||
| subclass an :mod:`io` class can no longer declare their own ``__dictoffset__`` | ||
| or ``__weaklistoffset__`` member. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,7 +5,6 @@ | |||||||||||||||||||
| #include "pycore_object.h" | ||||||||||||||||||||
| #include "pycore_pyatomic_ft_wrappers.h" | ||||||||||||||||||||
| #include "pycore_sysmodule.h" // _PySys_GetSizeOf() | ||||||||||||||||||||
| #include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| #include <stddef.h> // offsetof() | ||||||||||||||||||||
| #include "_iomodule.h" | ||||||||||||||||||||
|
|
@@ -21,8 +20,6 @@ typedef struct { | |||||||||||||||||||
| PyObject *buf; | ||||||||||||||||||||
| Py_ssize_t pos; | ||||||||||||||||||||
| Py_ssize_t string_size; | ||||||||||||||||||||
| PyObject *dict; | ||||||||||||||||||||
| PyObject *weakreflist; | ||||||||||||||||||||
| Py_ssize_t exports; | ||||||||||||||||||||
| #ifdef Py_GIL_DISABLED | ||||||||||||||||||||
| int buf_shared; | ||||||||||||||||||||
|
|
@@ -974,11 +971,16 @@ _io_BytesIO___getstate___impl(bytesio *self) | |||||||||||||||||||
|
|
||||||||||||||||||||
| if (initvalue == NULL) | ||||||||||||||||||||
| return NULL; | ||||||||||||||||||||
| if (self->dict == NULL) { | ||||||||||||||||||||
| dict = Py_NewRef(Py_None); | ||||||||||||||||||||
| dict = PyObject_GenericGetDict((PyObject *)self, NULL); | ||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: This is always constructing a dict just to see "empty dict", can we incorporate (Applies to multiple of these) Lines 7917 to 7925 in 9c37326
|
||||||||||||||||||||
| if (dict == NULL) { | ||||||||||||||||||||
| Py_DECREF(initvalue); | ||||||||||||||||||||
| return NULL; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (PyDict_GET_SIZE(dict) == 0) { | ||||||||||||||||||||
| Py_SETREF(dict, Py_NewRef(Py_None)); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| else { | ||||||||||||||||||||
| dict = PyDict_Copy(self->dict); | ||||||||||||||||||||
| Py_SETREF(dict, PyDict_Copy(dict)); | ||||||||||||||||||||
| if (dict == NULL) { | ||||||||||||||||||||
| Py_DECREF(initvalue); | ||||||||||||||||||||
| return NULL; | ||||||||||||||||||||
|
|
@@ -1061,16 +1063,16 @@ _io_BytesIO___setstate___impl(bytesio *self, PyObject *state) | |||||||||||||||||||
| Py_TYPE(dict)->tp_name); | ||||||||||||||||||||
| return NULL; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (self->dict) { | ||||||||||||||||||||
| /* Alternatively, we could replace the internal dictionary | ||||||||||||||||||||
| completely. However, it seems more practical to just update it. */ | ||||||||||||||||||||
| if (PyDict_Update(self->dict, dict) < 0) | ||||||||||||||||||||
| return NULL; | ||||||||||||||||||||
| /* Alternatively, we could replace the internal dictionary | ||||||||||||||||||||
| completely. However, it seems more practical to just update it. */ | ||||||||||||||||||||
| PyObject *self_dict = PyObject_GenericGetDict((PyObject *)self, NULL); | ||||||||||||||||||||
| if (self_dict == NULL) { | ||||||||||||||||||||
| return NULL; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| else { | ||||||||||||||||||||
| /* The LOAD_ATTR specializations read the dict slot lock-free | ||||||||||||||||||||
| with an acquire load, so pair it with a release store. */ | ||||||||||||||||||||
| FT_ATOMIC_STORE_PTR_RELEASE(self->dict, Py_NewRef(dict)); | ||||||||||||||||||||
| int rc = PyDict_Update(self_dict, dict); | ||||||||||||||||||||
| Py_DECREF(self_dict); | ||||||||||||||||||||
| if (rc < 0) { | ||||||||||||||||||||
| return NULL; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -1089,8 +1091,8 @@ bytesio_dealloc(PyObject *op) | |||||||||||||||||||
| PyErr_Print(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| Py_CLEAR(self->buf); | ||||||||||||||||||||
| Py_CLEAR(self->dict); | ||||||||||||||||||||
| FT_CLEAR_WEAKREFS(op, self->weakreflist); | ||||||||||||||||||||
| PyObject_ClearManagedDict(op); | ||||||||||||||||||||
| PyObject_ClearWeakRefs(op); | ||||||||||||||||||||
| tp->tp_free(self); | ||||||||||||||||||||
| Py_DECREF(tp); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
@@ -1186,16 +1188,15 @@ bytesio_traverse(PyObject *op, visitproc visit, void *arg) | |||||||||||||||||||
| { | ||||||||||||||||||||
| bytesio *self = bytesio_CAST(op); | ||||||||||||||||||||
| Py_VISIT(Py_TYPE(self)); | ||||||||||||||||||||
| Py_VISIT(self->dict); | ||||||||||||||||||||
| Py_VISIT(self->buf); | ||||||||||||||||||||
| return 0; | ||||||||||||||||||||
| return PyObject_VisitManagedDict(op, visit, arg); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| static int | ||||||||||||||||||||
| bytesio_clear(PyObject *op) | ||||||||||||||||||||
| { | ||||||||||||||||||||
| bytesio *self = bytesio_CAST(op); | ||||||||||||||||||||
| Py_CLEAR(self->dict); | ||||||||||||||||||||
| PyObject_ClearManagedDict(op); | ||||||||||||||||||||
| if (FT_ATOMIC_LOAD_SSIZE_RELAXED(self->exports) == 0) { | ||||||||||||||||||||
| Py_CLEAR(self->buf); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
@@ -1239,8 +1240,6 @@ static struct PyMethodDef bytesio_methods[] = { | |||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| static PyMemberDef bytesio_members[] = { | ||||||||||||||||||||
| {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(bytesio, weakreflist), Py_READONLY}, | ||||||||||||||||||||
| {"__dictoffset__", Py_T_PYSSIZET, offsetof(bytesio, dict), Py_READONLY}, | ||||||||||||||||||||
| {NULL} | ||||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove these for the types where it is now empty now?