From 17ae1789abac939d390ff699e753a73beeb59ed4 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 10 Aug 2023 00:43:52 +0100 Subject: [PATCH 1/2] Create object's dict-values on-demand, instead of creating __dict__ --- Objects/dictobject.c | 4 +--- Objects/object.c | 8 ++++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 36375f50646fd41..f9701f6b4b09ad2 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -5762,10 +5762,8 @@ _PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr, assert(dictptr != NULL); dict = *dictptr; if (dict == NULL) { + assert(!_PyType_HasFeature(tp, Py_TPFLAGS_MANAGED_DICT)); dictkeys_incref(cached); - if (_PyType_HasFeature(tp, Py_TPFLAGS_MANAGED_DICT)) { - OBJECT_STAT_INC(dict_materialized_on_request); - } dict = new_dict_with_shared_keys(interp, cached); if (dict == NULL) return -1; diff --git a/Objects/object.c b/Objects/object.c index d1154eb344fab1a..f21710b4bebacc3 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -1577,6 +1577,14 @@ _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name, goto error_check; } dictptr = &dorv_ptr->dict; + if (*dictptr == NULL) { + if (_PyObject_InitInlineValues(obj, tp)) { + goto done; + } + res = _PyObject_StoreInstanceAttribute( + obj, _PyDictOrValues_GetValues(*dorv_ptr), name, value); + goto error_check; + } } else { dictptr = _PyObject_ComputedDictPointer(obj); From 29d3a2f37a14b7b6539d0cf30345fe8abdfb74d5 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Fri, 11 Aug 2023 15:30:40 +0100 Subject: [PATCH 2/2] Update Objects/object.c Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Objects/object.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/object.c b/Objects/object.c index f21710b4bebacc3..868623a9f7bffc2 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -1578,7 +1578,7 @@ _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name, } dictptr = &dorv_ptr->dict; if (*dictptr == NULL) { - if (_PyObject_InitInlineValues(obj, tp)) { + if (_PyObject_InitInlineValues(obj, tp) < 0) { goto done; } res = _PyObject_StoreInstanceAttribute(