Skip to content

Commit e6091f8

Browse files
committed
gh-81381: Reduce allcoated size of PyType_GenericAlloc if possible
1 parent 11f9932 commit e6091f8

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Reduce the extra allocation size of :c:func:`PyType_GenericAlloc` except the
2+
type is if a subtype of 'type'.

Objects/typeobject.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,9 +1289,12 @@ PyObject *
12891289
_PyType_AllocNoTrack(PyTypeObject *type, Py_ssize_t nitems)
12901290
{
12911291
PyObject *obj;
1292-
const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
1293-
/* note that we need to add one, for the sentinel */
1294-
1292+
size_t extra = 0;
1293+
if (type->tp_flags & Py_TPFLAGS_TYPE_SUBCLASS || type->tp_flags & Py_TPFLAGS_HAVE_VECTORCALL) {
1294+
/* note that we need to add one, for the sentinel */
1295+
extra = 1;
1296+
}
1297+
const size_t size = _PyObject_VAR_SIZE(type, nitems + extra);
12951298
const size_t presize = _PyType_PreHeaderSize(type);
12961299
char *alloc = PyObject_Malloc(size + presize);
12971300
if (alloc == NULL) {
@@ -1309,7 +1312,7 @@ _PyType_AllocNoTrack(PyTypeObject *type, Py_ssize_t nitems)
13091312
_PyObject_Init(obj, type);
13101313
}
13111314
else {
1312-
_PyObject_InitVar((PyVarObject *)obj, type, nitems);
1315+
_PyObject_InitVar((PyVarObject *)obj, type, nitems + extra);
13131316
}
13141317
return obj;
13151318
}

0 commit comments

Comments
 (0)