Skip to content

Commit 2763e99

Browse files
committed
Avoid locking in _LOAD_ATTR_WITH_HINT
1 parent ccf1732 commit 2763e99

File tree

7 files changed

+72
-45
lines changed

7 files changed

+72
-45
lines changed

Include/internal/pycore_dict.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ extern int _PyDict_Pop_KnownHash(
149149
Py_hash_t hash,
150150
PyObject **result);
151151

152+
PyAPI_FUNC(void) _PyDict_EnsureSharedOnRead(PyDictObject *mp);
153+
152154
#define DKIX_EMPTY (-1)
153155
#define DKIX_DUMMY (-2) /* Used internally */
154156
#define DKIX_ERROR (-3)

Include/internal/pycore_opcode_metadata.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_metadata.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,38 +2286,38 @@ dummy_func(
22862286
}
22872287

22882288
op(_LOAD_ATTR_WITH_HINT, (hint/1, owner, dict: PyDictObject * -- attr)) {
2289+
_PyDict_EnsureSharedOnRead(dict);
2290+
2291+
PyDictKeysObject *dk = FT_ATOMIC_LOAD_PTR(dict->ma_keys);
22892292
PyObject *attr_o;
2290-
if (!LOCK_OBJECT(dict)) {
2293+
if (hint >= (size_t)dk->dk_nentries) {
22912294
POP_INPUT(dict);
22922295
DEOPT_IF(true);
22932296
}
2294-
2295-
if (hint >= (size_t)dict->ma_keys->dk_nentries) {
2296-
UNLOCK_OBJECT(dict);
2297+
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg>>1);
2298+
if (dk->dk_kind != DICT_KEYS_UNICODE) {
22972299
POP_INPUT(dict);
22982300
DEOPT_IF(true);
22992301
}
2300-
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg>>1);
2301-
if (dict->ma_keys->dk_kind != DICT_KEYS_UNICODE) {
2302-
UNLOCK_OBJECT(dict);
2302+
PyDictUnicodeEntry *ep = DK_UNICODE_ENTRIES(dk) + hint;
2303+
if (FT_ATOMIC_LOAD_PTR_RELAXED(ep->me_key) != name) {
23032304
POP_INPUT(dict);
23042305
DEOPT_IF(true);
23052306
}
2306-
PyDictUnicodeEntry *ep = DK_UNICODE_ENTRIES(dict->ma_keys) + hint;
2307-
if (ep->me_key != name) {
2308-
UNLOCK_OBJECT(dict);
2307+
attr_o = FT_ATOMIC_LOAD_PTR(ep->me_value);
2308+
if (attr_o == NULL) {
23092309
POP_INPUT(dict);
23102310
DEOPT_IF(true);
23112311
}
2312-
attr_o = ep->me_value;
2313-
if (attr_o == NULL) {
2314-
UNLOCK_OBJECT(dict);
2312+
#ifdef Py_GIL_DISABLED
2313+
if (!_Py_TryIncrefCompareStackRef(&ep->me_value, attr_o, &attr)) {
23152314
POP_INPUT(dict);
23162315
DEOPT_IF(true);
23172316
}
2318-
STAT_INC(LOAD_ATTR, hit);
2317+
#else
23192318
attr = PyStackRef_FromPyObjectNew(attr_o);
2320-
UNLOCK_OBJECT(dict);
2319+
#endif
2320+
STAT_INC(LOAD_ATTR, hit);
23212321
DEAD(dict);
23222322
DECREF_INPUTS();
23232323
}

Python/executor_cases.c.h

Lines changed: 16 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: 31 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)