Skip to content

gh-134043: use stackrefs for dict lookup in _PyObject_GetMethodStackRef #136412

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 12 commits into from
Jul 28, 2025
Merged
Prev Previous commit
Next Next commit
add _Py_dict_lookup_unicode_threadsafe_stackref to lookup method
  • Loading branch information
kumaraditya303 committed Jul 18, 2025
commit aadb4826da39f56d248c9021705a28a6fe318ee0
1 change: 1 addition & 0 deletions Include/internal/pycore_dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ extern void _PyDictKeys_DecRef(PyDictKeysObject *keys);
extern Py_ssize_t _Py_dict_lookup(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject **value_addr);
extern Py_ssize_t _Py_dict_lookup_threadsafe(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject **value_addr);
extern Py_ssize_t _Py_dict_lookup_threadsafe_stackref(PyDictObject *mp, PyObject *key, Py_hash_t hash, _PyStackRef *value_addr);
extern Py_ssize_t _Py_dict_lookup_unicode_threadsafe_stackref(PyDictObject *mp, PyObject *key, Py_hash_t hash, _PyStackRef *value_addr);

extern Py_ssize_t _PyDict_LookupIndex(PyDictObject *, PyObject *);
extern Py_ssize_t _PyDictKeys_StringLookup(PyDictKeysObject* dictkeys, PyObject *key);
Expand Down
94 changes: 73 additions & 21 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1581,33 +1581,41 @@
return ix;
}

Py_ssize_t
_Py_dict_lookup_threadsafe_stackref(PyDictObject *mp, PyObject *key, Py_hash_t hash, _PyStackRef *value_addr)
static Py_ssize_t
unicodekeys_lookup_unicode_threadsafe_stackref(PyDictKeysObject* dk, PyObject *key, Py_hash_t hash, _PyStackRef *value_addr)
{
PyDictKeysObject *dk = _Py_atomic_load_ptr(&mp->ma_keys);
if (dk->dk_kind == DICT_KEYS_UNICODE && PyUnicode_CheckExact(key)) {
Py_ssize_t ix = unicodekeys_lookup_unicode_threadsafe(dk, key, hash);
if (ix == DKIX_EMPTY) {
assert(PyUnicode_CheckExact(key));
assert(dk->dk_kind == DICT_KEYS_UNICODE);
Py_ssize_t ix = unicodekeys_lookup_unicode_threadsafe(dk, key, hash);
if (ix == DKIX_EMPTY) {
*value_addr = PyStackRef_NULL;
return DKIX_EMPTY;
} else if (ix >= 0) {
PyObject **addr_of_value = &DK_UNICODE_ENTRIES(dk)[ix].me_value;
PyObject *value = _Py_atomic_load_ptr(addr_of_value);
if (value == NULL) {
*value_addr = PyStackRef_NULL;
return DKIX_EMPTY;
}
if (_PyObject_HasDeferredRefcount(value)) {
*value_addr = (_PyStackRef){ .bits = (uintptr_t)value | Py_TAG_DEFERRED };
return ix;
}
else if (ix >= 0) {
PyObject **addr_of_value = &DK_UNICODE_ENTRIES(dk)[ix].me_value;
PyObject *value = _Py_atomic_load_ptr(addr_of_value);
if (value == NULL) {
*value_addr = PyStackRef_NULL;
return DKIX_EMPTY;
}
if (_PyObject_HasDeferredRefcount(value)) {
*value_addr = (_PyStackRef){ .bits = (uintptr_t)value | Py_TAG_DEFERRED };
return ix;
}
if (_Py_TryIncrefCompare(addr_of_value, value)) {
*value_addr = PyStackRef_FromPyObjectSteal(value);
return ix;
}
if (_Py_TryIncrefCompare(addr_of_value, value)) {
*value_addr = PyStackRef_FromPyObjectSteal(value);
return ix;
}
}
}

Check warning on line 1609 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04)

control reaches end of non-void function [-Wreturn-type]

Check warning on line 1609 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64)

'unicodekeys_lookup_unicode_threadsafe_stackref': not all control paths return a value [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 1609 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64)

'unicodekeys_lookup_unicode_threadsafe_stackref': not all control paths return a value [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]


Py_ssize_t
_Py_dict_lookup_threadsafe_stackref(PyDictObject *mp, PyObject *key, Py_hash_t hash, _PyStackRef *value_addr)
{
PyDictKeysObject *dk = _Py_atomic_load_ptr(&mp->ma_keys);
if (dk->dk_kind == DICT_KEYS_UNICODE && PyUnicode_CheckExact(key)) {
return unicodekeys_lookup_unicode_threadsafe_stackref(dk, key, hash, value_addr);
}

PyObject *obj;
Py_ssize_t ix = _Py_dict_lookup_threadsafe(mp, key, hash, &obj);
Expand All @@ -1620,6 +1628,33 @@
return ix;
}

// This is similar to _Py_dict_lookup_threadsafe_stackref() but
// it is used when dict is borrowed reference and key is known to be unicode.
// It avoids increfing the dict if dict only has unicode keys in which case
// the lookup is safe, otherwise it increfs the dict and lookups the key.

Py_ssize_t
_Py_dict_lookup_unicode_threadsafe_stackref(PyDictObject *mp, PyObject *key, Py_hash_t hash, _PyStackRef *value_addr)
{
assert(PyUnicode_CheckExact(key));
PyDictKeysObject *dk = _Py_atomic_load_ptr(&mp->ma_keys);
if (dk->dk_kind == DICT_KEYS_UNICODE) {
return unicodekeys_lookup_unicode_threadsafe_stackref(dk, key, hash, value_addr);
}

PyObject *obj;
Py_INCREF(mp);
Py_ssize_t ix = _Py_dict_lookup_threadsafe(mp, key, hash, &obj);
Py_DECREF(mp);
if (ix >= 0 && obj != NULL) {
*value_addr = PyStackRef_FromPyObjectSteal(obj);
}
else {
*value_addr = PyStackRef_NULL;
}
return ix;
}

#else // Py_GIL_DISABLED

Py_ssize_t
Expand All @@ -1644,6 +1679,23 @@
return ix;
}

Py_ssize_t
_Py_dict_lookup_unicode_threadsafe_stackref(PyDictObject *mp, PyObject *key, Py_hash_t hash, _PyStackRef *value_addr)
{
assert(PyUnicode_CheckExact(key));
PyObject *val;
Py_INCREF(mp);
Py_ssize_t ix = _Py_dict_lookup(mp, key, hash, &val);
Py_DECREF(mp);
if (ix >= 0 && val != NULL) {
*value_addr = PyStackRef_FromPyObjectNew(val);
}
else {
*value_addr = PyStackRef_NULL;
}
return ix;
}

#endif

int
Expand Down
13 changes: 2 additions & 11 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1759,18 +1759,9 @@ _PyObject_GetMethodStackRef(PyThreadState *ts, PyObject *obj,
assert(hash != -1);
// ref is not visible to gc so there should be
// no escaping calls before assigning it to method
PyDictObject *mp = (PyDictObject *)dict;
PyDictKeysObject *keys = FT_ATOMIC_LOAD_PTR_ACQUIRE(mp->ma_keys);
bool unicode_keys = DK_IS_UNICODE(keys);
_PyStackRef ref;
if (!unicode_keys) {
Py_INCREF(mp);
}
Py_ssize_t ix = _Py_dict_lookup_threadsafe_stackref(mp, name,
hash, &ref);
if (!unicode_keys) {
Py_DECREF(mp);
}
Py_ssize_t ix = _Py_dict_lookup_unicode_threadsafe_stackref((PyDictObject *)dict,
name, hash, &ref);
if (ix == DKIX_ERROR) {
// error
PyStackRef_CLEAR(*method);
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy