Skip to content

Commit e7e5ed4

Browse files
committed
Simplify incref's around new_dict_with_shared_keys, fix stats
Lock obj instead of using atomic, and fix reads/writes
1 parent 56a79e6 commit e7e5ed4

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

Objects/dictobject.c

+5-13
Original file line numberDiff line numberDiff line change
@@ -931,9 +931,9 @@ new_dict_with_shared_keys(PyInterpreterState *interp, PyDictKeysObject *keys)
931931
size_t size = shared_keys_usable_size(keys);
932932
PyDictValues *values = new_values(size);
933933
if (values == NULL) {
934-
dictkeys_decref(interp, keys, false);
935934
return PyErr_NoMemory();
936935
}
936+
dictkeys_incref(keys);
937937
for (size_t i = 0; i < size; i++) {
938938
values->values[i] = NULL;
939939
}
@@ -6666,8 +6666,6 @@ materialize_managed_dict_lock_held(PyObject *obj)
66666666
{
66676667
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(obj);
66686668

6669-
OBJECT_STAT_INC(dict_materialized_on_request);
6670-
66716669
PyDictValues *values = _PyObject_InlineValues(obj);
66726670
PyInterpreterState *interp = _PyInterpreterState_GET();
66736671
PyDictKeysObject *keys = CACHED_KEYS(Py_TYPE(obj));
@@ -7178,8 +7176,6 @@ ensure_managed_dict(PyObject *obj)
71787176
goto done;
71797177
}
71807178
#endif
7181-
OBJECT_STAT_INC(dict_materialized_on_request);
7182-
dictkeys_incref(CACHED_KEYS(tp));
71837179
dict = (PyDictObject *)new_dict_with_shared_keys(_PyInterpreterState_GET(),
71847180
CACHED_KEYS(tp));
71857181
FT_ATOMIC_STORE_PTR_RELEASE(_PyObject_ManagedDictPointer(obj)->dict,
@@ -7199,7 +7195,7 @@ ensure_nonmanaged_dict(PyObject *obj, PyObject **dictptr)
71997195
{
72007196
PyDictKeysObject *cached;
72017197

7202-
PyObject *dict = FT_ATOMIC_LOAD_PTR_RELAXED(*dictptr);
7198+
PyObject *dict = FT_ATOMIC_LOAD_PTR_ACQUIRE(*dictptr);
72037199
if (dict == NULL) {
72047200
#ifdef Py_GIL_DISABLED
72057201
Py_BEGIN_CRITICAL_SECTION(obj);
@@ -7209,19 +7205,15 @@ ensure_nonmanaged_dict(PyObject *obj, PyObject **dictptr)
72097205
}
72107206
#endif
72117207
PyTypeObject *tp = Py_TYPE(obj);
7212-
if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
7208+
if (_PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
72137209
PyInterpreterState *interp = _PyInterpreterState_GET();
72147210
assert(!_PyType_HasFeature(tp, Py_TPFLAGS_INLINE_VALUES));
7215-
dictkeys_incref(cached);
72167211
dict = new_dict_with_shared_keys(interp, cached);
7217-
if (dict == NULL) {
7218-
dictkeys_decref(interp, cached, false);
7219-
}
72207212
}
72217213
else {
72227214
dict = PyDict_New();
72237215
}
7224-
FT_ATOMIC_STORE_PTR_RELAXED(*dictptr, dict);
7216+
FT_ATOMIC_STORE_PTR_RELEASE(*dictptr, dict);
72257217
#ifdef Py_GIL_DISABLED
72267218
done:
72277219
Py_END_CRITICAL_SECTION();
@@ -7264,8 +7256,8 @@ _PyObjectDict_SetItem(PyTypeObject *tp, PyObject *obj, PyObject **dictptr,
72647256

72657257
Py_BEGIN_CRITICAL_SECTION(dict);
72667258
res = set_or_del_lock_held((PyDictObject *)dict, key, value);
7267-
Py_END_CRITICAL_SECTION();
72687259
ASSERT_CONSISTENT(dict);
7260+
Py_END_CRITICAL_SECTION();
72697261
return res;
72707262
}
72717263

Objects/object.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -1797,11 +1797,9 @@ PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
17971797
"not a '%.200s'", Py_TYPE(value)->tp_name);
17981798
return -1;
17991799
}
1800-
#ifdef Py_GIL_DISABLED
1801-
Py_XDECREF(_Py_atomic_exchange_ptr(dictptr, Py_NewRef(value)));
1802-
#else
1800+
Py_BEGIN_CRITICAL_SECTION(obj);
18031801
Py_XSETREF(*dictptr, Py_NewRef(value));
1804-
#endif
1802+
Py_END_CRITICAL_SECTION();
18051803
return 0;
18061804
}
18071805

0 commit comments

Comments
 (0)