8000 gh-112075: _Py_dict_lookup needs to lock shared keys by DinoV · Pull Request #117528 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-112075: _Py_dict_lookup needs to lock shared keys #117528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 25, 2024
Prev Previous commit
Next Next commit
Remove splitdict_lookup_keys_lock_held
  • Loading branch information
DinoV committed Apr 25, 2024
commit a74c0ee4049343ad81007350f28d8527f6d99ddd
42 changes: 6 additions & 36 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1188,41 +1188,10 @@ _PyDictKeys_StringLookup(PyDictKeysObject* dk, PyObject *key)
return unicodekeys_lookup_unicode(dk, key, hash);
}


#ifdef Py_GIL_DISABLED

// Version of _Py_dict_lookup specialized for when we have split keys and the
// shared keys are locked.
static Py_ssize_t
splitdict_lookup_keys_lock_held(PyDictObject *mp, PyObject *key, Py_hash_t hash,
PyObject **value_addr)
{
PyDictKeysObject *dk = mp->ma_keys;
Py_ssize_t ix;

_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(mp);
ASSERT_KEYS_LOCKED(dk);
assert(PyUnicode_CheckExact(key));
assert(dk->dk_kind == DICT_KEYS_SPLIT);

ix = unicodekeys_lookup_unicode(dk, key, hash);

if (ix >= 0) {
*value_addr = mp->ma_values->values[ix];
}
else {
*value_addr = NULL;
}

return ix;
}

static Py_ssize_t
unicodekeys_lookup_unicode_threadsafe(PyDictKeysObject* dk, PyObject *key,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused code warning

Py_hash_t hash);

#endif // Py_GIL_DISABLED

/*
The basic lookup function used by all operations.
This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Expand Down Expand Up @@ -1260,15 +1229,16 @@ _Py_dict_lookup(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject **valu
ix = unicodekeys_lookup_unicode_threadsafe(dk, key, hash);
if (ix == DKIX_KEY_CHANGED) {
LOCK_KEYS(dk);
ix = splitdict_lookup_keys_lock_held(mp, key, hash,
value_addr);
ix = unicodekeys_lookup_unicode(dk, key, hash);
UNLOCK_KEYS(dk);
return ix;
}
}
#endif

else {
ix = unicodekeys_lookup_unicode(dk, key, hash);
}
#else
ix = unicodekeys_lookup_unicode(dk, key, hash);
#endif
}
else {
INCREF_KEYS_FT(dk);
Expand Down
0