Skip to content

Commit 3946154

Browse files
committed
gh-81381: Reduce allcoated size of PyType_GenericAlloc if possible
1 parent 26ff436 commit 3946154

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
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: 6 additions & 3 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) {
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) {

0 commit comments

Comments
 (0)