Skip to content
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

gh-81381: Reduce allocated size of PyType_GenericAlloc if possible #100855

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Reduce the extra allocation size of :c:func:`PyType_GenericAlloc` except the
type is if a subtype of 'type'.
11 changes: 7 additions & 4 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1289,9 +1289,12 @@ PyObject *
_PyType_AllocNoTrack(PyTypeObject *type, Py_ssize_t nitems)
{
PyObject *obj;
const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
/* note that we need to add one, for the sentinel */

size_t extra = 0;
if (type->tp_flags & Py_TPFLAGS_TYPE_SUBCLASS || type->tp_flags & Py_TPFLAGS_HAVE_VECTORCALL) {
/* note that we need to add one, for the sentinel */
extra = 1;
}
const size_t size = _PyObject_VAR_SIZE(type, nitems + extra);
const size_t presize = _PyType_PreHeaderSize(type);
char *alloc = PyObject_Malloc(size + presize);
if (alloc == NULL) {
Expand All @@ -1309,7 +1312,7 @@ _PyType_AllocNoTrack(PyTypeObject *type, Py_ssize_t nitems)
_PyObject_Init(obj, type);
}
else {
_PyObject_InitVar((PyVarObject *)obj, type, nitems);
_PyObject_InitVar((PyVarObject *)obj, type, nitems + extra);
}
return obj;
}
Expand Down