GH-106485: Create object's dict-values instead of creating __dict__, when we can. (GH-107843)

This commit is contained in:
Mark Shannon 2023-08-11 20:05:56 +01:00 committed by GitHub
parent 66e4edd734
commit 666b68e8f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 3 deletions

View File

@ -5762,10 +5762,8 @@ _PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
assert(dictptr != NULL); assert(dictptr != NULL);
dict = *dictptr; dict = *dictptr;
if (dict == NULL) { if (dict == NULL) {
assert(!_PyType_HasFeature(tp, Py_TPFLAGS_MANAGED_DICT));
dictkeys_incref(cached); 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); dict = new_dict_with_shared_keys(interp, cached);
if (dict == NULL) if (dict == NULL)
return -1; return -1;

View File

@ -1577,6 +1577,14 @@ _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
goto error_check; goto error_check;
} }
dictptr = &dorv_ptr->dict; dictptr = &dorv_ptr->dict;
if (*dictptr == NULL) {
if (_PyObject_InitInlineValues(obj, tp) < 0) {
goto done;
}
res = _PyObject_StoreInstanceAttribute(
obj, _PyDictOrValues_GetValues(*dorv_ptr), name, value);
goto error_check;
}
} }
else { else {
dictptr = _PyObject_ComputedDictPointer(obj); dictptr = _PyObject_ComputedDictPointer(obj);