Skip to content

Commit aa0b67d

Browse files
committed
Avoid locking in _LOAD_ATTR_WITH_HINT
1 parent c1a02f9 commit aa0b67d

File tree

5 files changed

+60
-40
lines changed

5 files changed

+60
-40
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 & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,34 +2284,36 @@ 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) {
2299-
UNLOCK_OBJECT(dict);
2298+
if (dk->dk_kind != DICT_KEYS_UNICODE) {
23002299
DEOPT_IF(true);
23012300
}
23022301
PyDictUnicodeEntry *ep = DK_UNICODE_ENTRIES(dict->ma_keys) + hint;
2303-
if (ep->me_key != name) {
2304-
UNLOCK_OBJECT(dict);
2302+
if (FT_ATOMIC_LOAD_PTR_RELAXED(ep->me_key) != name) {
23052303
DEOPT_IF(true);
23062304
}
2307-
attr_o = ep->me_value;
2305+
attr_o = FT_ATOMIC_LOAD_PTR(ep->me_value);
23082306
if (attr_o == NULL) {
2309-
UNLOCK_OBJECT(dict);
23102307
DEOPT_IF(true);
23112308
}
23122309
STAT_INC(LOAD_ATTR, hit);
2310+
#ifdef Py_GIL_DISABLED
2311+
if (!_Py_TryIncrefCompareStackRef(&ep->me_value, attr_o, &attr)) {
2312+
DEOPT_IF(true);
2313+
}
2314+
#else
23132315
attr = PyStackRef_FromPyObjectNew(attr_o);
2314-
UNLOCK_OBJECT(dict);
2316+
#endif
23152317
PyStackRef_CLOSE(owner);
23162318
}
23172319

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)