Skip to content

Commit

Permalink
pythongh-81381: Reduce allcoated size of PyType_GenericAlloc if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed Jan 9, 2023
1 parent 11f9932 commit e6091f8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
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

0 comments on commit e6091f8

Please sign in to comment.