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 8, 2023
1 parent 11f9932 commit a9952ac
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 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'.
9 changes: 6 additions & 3 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) {
/* 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 Down

0 comments on commit a9952ac

Please sign in to comment.