Skip to content

Commit d66be85

Browse files
committed
Avoid locking in _LOAD_ATTR_WITH_HINT
1 parent 2270684 commit d66be85

File tree

5 files changed

+60
-37
lines changed

5 files changed

+60
-37
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
@@ -1323,6 +1323,12 @@ ensure_shared_on_read(PyDictObject *mp)
13231323
Py_END_CRITICAL_SECTION();
13241324
}
13251325
}
1326+
1327+
void
1328+
_PyDict_EnsureSharedOnRead(PyDictObject *mp)
1329+
{
1330+
ensure_shared_on_read(mp);
1331+
}
13261332
#endif
13271333

13281334
static inline void

Python/bytecodes.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,34 +2284,37 @@ dummy_func(
22842284
assert(Py_TYPE(owner_o)->tp_flags & Py_TPFLAGS_MANAGED_DICT);
22852285
PyDictObject *dict = _PyObject_GetManagedDict(owner_o);
22862286
DEOPT_IF(dict == NULL);
2287+
PyDictKeysObject *dk = FT_ATOMIC_LOAD_PTR(dict->ma_keys);
22872288
assert(PyDict_CheckExact((PyObject *)dict));
2289+
#ifdef Py_GIL_DISABLED
2290+
_PyDict_EnsureSharedOnRead(dict);
2291+
#endif
22882292
PyObject *attr_o;
2289-
if (!LOCK_OBJECT(dict)) {
2293+
if (hint >= (size_t)FT_ATOMIC_LOAD_SSIZE_RELAXED(dk->dk_nentries)) {
22902294
DEOPT_IF(true);
22912295
}
22922296

2293-
if (hint >= (size_t)dict->ma_keys->dk_nentries) {
2294-
UNLOCK_OBJECT(dict);
2295-
DEOPT_IF(true);
2296-
}
22972297
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg>>1);
2298-
if (dict->ma_keys->dk_kind != DICT_KEYS_UNICODE) {
2298+
if (dk->dk_kind != DICT_KEYS_UNICODE) {
22992299
UNLOCK_OBJECT(dict);
23002300
DEOPT_IF(true);
23012301
}
23022302
PyDictUnicodeEntry *ep = DK_UNICODE_ENTRIES(dict->ma_keys) + hint;
2303-
if (ep->me_key != name) {
2304-
UNLOCK_OBJECT(dict);
2303+
if (FT_ATOMIC_LOAD_PTR_RELAXED(ep->me_key) != name) {
23052304
DEOPT_IF(true);
23062305
}
2307-
attr_o = ep->me_value;
2306+
attr_o = FT_ATOMIC_LOAD_PTR(ep->me_value);
23082307
if (attr_o == NULL) {
2309-
UNLOCK_OBJECT(dict);
23102308
DEOPT_IF(true);
23112309
}
23122310
STAT_INC(LOAD_ATTR, hit);
2311+
#ifdef Py_GIL_DISABLED
2312+
if (!_Py_TryIncrefCompareStackRef(&ep->me_value, attr_o, &attr)) {
2313+
DEOPT_IF(true);
2314+
}
2315+
#else
23132316
attr = PyStackRef_FromPyObjectNew(attr_o);
2314-
UNLOCK_OBJECT(dict);
2317+
#endif
23152318
PyStackRef_CLOSE(owner);
23162319
}
23172320

Python/executor_cases.c.h

Lines changed: 18 additions & 13 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 & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)