Skip to content

Commit 14eb91d

Browse files
committed
Avoid locking in _LOAD_ATTR_WITH_HINT
1 parent a26a301 commit 14eb91d

File tree

5 files changed

+61
-41
lines changed

5 files changed

+61
-41
lines changed

Include/internal/pycore_dict.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ extern int _PyDict_Pop_KnownHash(
150150
Py_hash_t hash,
151151
PyObject **result);
152152

153+
#ifdef Py_GIL_DISABLED
154+
PyAPI_FUNC(void) _PyDict_EnsureSharedOnRead(PyDictObject *mp);
155+
#endif
156+
153157
#define DKIX_EMPTY (-1)
154158
#define DKIX_DUMMY (-2) /* Used internally */
155159
#define DKIX_ERROR (-3)

Objects/dictobject.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,12 @@ ensure_shared_on_read(PyDictObject *mp)
13251325
Py_END_CRITICAL_SECTION();
13261326
}
13271327
}
1328+
1329+
void
1330+
_PyDict_EnsureSharedOnRead(PyDictObject *mp)
1331+
{
1332+
ensure_shared_on_read(mp);
1333+
}
13281334
#endif
13291335

13301336
static inline void

Python/bytecodes.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,34 +2291,36 @@ dummy_func(
22912291
assert(Py_TYPE(owner_o)->tp_flags & Py_TPFLAGS_MANAGED_DICT);
22922292
PyDictObject *dict = _PyObject_GetManagedDict(owner_o);
22932293
DEOPT_IF(dict == NULL);
2294+
PyDictKeysObject *dk = FT_ATOMIC_LOAD_PTR(dict->ma_keys);
22942295
assert(PyDict_CheckExact((PyObject *)dict));
2296+
#ifdef Py_GIL_DISABLED
2297+
_PyDict_EnsureSharedOnRead(dict);
2298+
#endif
22952299
PyObject *attr_o;
2296-
if (!LOCK_OBJECT(dict)) {
2300+
if (hint >= (size_t)FT_ATOMIC_LOAD_SSIZE_RELAXED(dk->dk_nentries)) {
22972301
DEOPT_IF(true);
22982302
}
22992303

2300-
if (hint >= (size_t)dict->ma_keys->dk_nentries) {
2301-
UNLOCK_OBJECT(dict);
2302-
DEOPT_IF(true);
2303-
}
23042304
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg>>1);
2305-
if (dict->ma_keys->dk_kind != DICT_KEYS_UNICODE) {
2306-
UNLOCK_OBJECT(dict);
2305+
if (dk->dk_kind != DICT_KEYS_UNICODE) {
23072306
DEOPT_IF(true);
23082307
}
2309-
PyDictUnicodeEntry *ep = DK_UNICODE_ENTRIES(dict->ma_keys) + hint;
2310-
if (ep->me_key != name) {
2311-
UNLOCK_OBJECT(dict);
2308+
PyDictUnicodeEntry *ep = DK_UNICODE_ENTRIES(dk) + hint;
2309+
if (FT_ATOMIC_LOAD_PTR_RELAXED(ep->me_key) != name) {
23122310
DEOPT_IF(true);
23132311
}
2314-
attr_o = ep->me_value;
2312+
attr_o = FT_ATOMIC_LOAD_PTR(ep->me_value);
23152313
if (attr_o == NULL) {
2316-
UNLOCK_OBJECT(dict);
23172314
DEOPT_IF(true);
23182315
}
23192316
STAT_INC(LOAD_ATTR, hit);
2317+
#ifdef Py_GIL_DISABLED
2318+
if (!_Py_TryIncrefCompareStackRef(&ep->me_value, attr_o, &attr)) {
2319+
DEOPT_IF(true);
2320+
}
2321+
#else
23202322
attr = PyStackRef_FromPyObjectNew(attr_o);
2321-
UNLOCK_OBJECT(dict);
2323+
#endif
23222324
PyStackRef_CLOSE(owner);
23232325
}
23242326

Python/executor_cases.c.h

Lines changed: 18 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 18 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)