8000 Simplify incref's around new_dict_with_shared_keys, fix stats · python/cpython@f328d37 · GitHub
[go: up one dir, main page]

Skip to content

Commit f328d37

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 0404e6d commit f328d37

File tree

2 files changed

+6
-16
lines changed

2 files changed

+6
-16
lines changed

Objects/dictobject.c

Lines changed: 4 additions & 12 deletions
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
}
@@ -6693,8 +6693,6 @@ materialize_managed_dict_lock_held(PyObject *obj)
66936693
{
66946694
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(obj);
66956695

6696-
OBJECT_STAT_INC(dict_materialized_on_request);
6697-
66986696
PyDictValues *values = _PyObject_InlineValues(obj);
66996697
PyInterpreterState *interp = _PyInterpreterState_GET();
67006698
PyDictKeysObject *keys = CACHED_KEYS(Py_TYPE(obj));
@@ -7205,8 +7203,6 @@ ensure_managed_dict(PyObject *obj)
72057203
goto done;
72067204
}
72077205
#endif
7208-
OBJECT_STAT_INC(dict_materialized_on_request);
7209-
dictkeys_incref(CACHED_KEYS(tp));
72107206
dict = (PyDictObject *)new_dict_with_shared_keys(_PyInterpreterState_GET(),
72117207
CACHED_KEYS(tp));
72127208
FT_ATOMIC_STORE_PTR_RELEASE(_PyObject_ManagedDictPointer(obj)->dict,
@@ -7226,7 +7222,7 @@ ensure_nonmanaged_dict(PyObject *obj, PyObject **dictptr)
72267222
{
72277223
PyDictKeysObject *cached;
72287224

7229-
PyObject *dict = FT_ATOMIC_LOAD_PTR_RELAXED(*dictptr);
7225+
PyObject *dict = FT_ATOMIC_LOAD_PTR_ACQUIRE(*dictptr);
72307226
if (dict == NULL) {
72317227
#ifdef Py_GIL_DISABLED
72327228
Py_BEGIN_CRITICAL_SECTION(obj);
@@ -7236,19 +7232,15 @@ ensure_nonmanaged_dict(PyObject *obj, PyObject **dictptr)
72367232
}
72377233
#endif
72387234
PyTypeObject *tp = Py_TYPE(obj);
7239-
if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
7235+
if (_PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
72407236
PyInterpreterState *interp = _PyInterpreterState_GET();
72417237
assert(!_PyType_HasFeature(tp, Py_TPFLAGS_INLINE_VALUES));
7242-
dictkeys_incref(cached);
72437238
dict = new_dict_with_shared_keys(interp, cached);
7244-
if (dict == NULL) {
7245-
dictkeys_decref(interp, cached, false);
7246-
}
72477239
}
72487240
else {
72497241
dict = PyDict_New();
72507242
}
7251-
FT_ATOMIC_STORE_PTR_RELAXED(*dictptr, dict);
7243+
FT_ATOMIC_STORE_PTR_RELEASE(*dictptr, dict);
72527244
#ifdef Py_GIL_DISABLED
72537245
done:
72547246
Py_END_CRITICAL_SECTION();

Objects/object.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,11 +1789,9 @@ PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
17891789
"not a '%.200s'", Py_TYPE(value)->tp_name);
17901790
return -1;
17911791
}
1792-
#ifdef Py_GIL_DISABLED
1793-
Py_XDECREF(_Py_atomic_exchange_ptr(dictptr, Py_NewRef(value)));
1794-
#else
1792+
Py_BEGIN_CRITICAL_SECTION(obj);
17951793
Py_XSETREF(*dictptr, Py_NewRef(value));
1796-
#endif
1794+
Py_END_CRITICAL_SECTION();
17971795
return 0;
17981796
}
17991797

0 commit comments

Comments
 (0)
0