Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Include/internal/pycore_dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ extern Py_ssize_t _Py_dict_lookup(PyDictObject *mp, PyObject *key, Py_hash_t has
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 int _PyDict_GetMethodStackRef(PyDictObject *dict, PyObject *name, _PyStackRef *method);

extern Py_ssize_t _PyDict_LookupIndex(PyDictObject *, PyObject *);
extern Py_ssize_t _PyDictKeys_StringLookup(PyDictKeysObject* dictkeys, PyObject *key);

Expand Down
7 changes: 7 additions & 0 deletions Include/internal/pycore_stackref.h
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,13 @@ _Py_TryXGetStackRef(PyObject **src, _PyStackRef *out)

#endif

#define PyStackRef_XSETREF(dst, src) \
do { \
_PyStackRef _tmp_dst_ref = (dst); \
(dst) = (src); \
PyStackRef_XCLOSE(_tmp_dst_ref); \
} while(0)

// Like Py_VISIT but for _PyStackRef fields
#define _Py_VISIT_STACKREF(ref) \
do { \
Expand Down
101 changes: 82 additions & 19 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1584,34 +1584,49 @@ _Py_dict_lookup_threadsafe(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyOb
return ix;
}

static Py_ssize_t
lookup_threadsafe_unicode(PyDictKeysObject *dk, PyObject *key, Py_hash_t hash, _PyStackRef *value_addr)
{
assert(dk->dk_kind == DICT_KEYS_UNICODE);
assert(PyUnicode_CheckExact(key));

Py_ssize_t ix = unicodekeys_lookup_unicode_threadsafe(dk, key, hash);
if (ix == DKIX_EMPTY) {
*value_addr = PyStackRef_NULL;
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;
}
return DKIX_KEY_CHANGED;
}
assert(ix == DKIX_KEY_CHANGED);
return ix;
}

Py_ssize_t
_Py_dict_lookup_threadsafe_stackref(PyDictObject *mp, PyObject *key, Py_hash_t hash, _PyStackRef *value_addr)
{
ensure_shared_on_read(mp);

PyDictKeysObject *dk = _Py_atomic_load_ptr_acquire(&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) {
*value_addr = PyStackRef_NULL;
Py_ssize_t ix = lookup_threadsafe_unicode(dk, key, hash, value_addr);
if (ix != DKIX_KEY_CHANGED) {
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;
}
}
}

PyObject *obj;
Expand Down Expand Up @@ -1651,6 +1666,54 @@ _Py_dict_lookup_threadsafe_stackref(PyDictObject *mp, PyObject *key, Py_hash_t h

#endif

// Looks up the unicode key `key` in the dictionary. Note that `*method` may
// already contain a valid value! See _PyObject_GetMethodStackRef().
int
_PyDict_GetMethodStackRef(PyDictObject *mp, PyObject *key, _PyStackRef *method)
{
assert(PyUnicode_CheckExact(key));
Py_hash_t hash = hash_unicode_key(key);

#ifdef Py_GIL_DISABLED
// NOTE: We can only do the fast-path lookup if we are on the owning
// thread or if the dict is already marked as shared so that the load
// of ma_keys is safe without a lock. We cannot call ensure_shared_on_read()
// in this code path without incref'ing the dict because the dict is a
// borrowed reference protected by QSBR, and acquiring the lock could lead
// to a quiescent state (allowing the dict to be freed).
if (_Py_IsOwnedByCurrentThread((PyObject *)mp) || IS_DICT_SHARED(mp)) {
PyDictKeysObject *dk = _Py_atomic_load_ptr_acquire(&mp->ma_keys);
if (dk->dk_kind == DICT_KEYS_UNICODE) {
_PyStackRef ref;
Py_ssize_t ix = lookup_threadsafe_unicode(dk, key, hash, &ref);
if (ix >= 0) {
assert(!PyStackRef_IsNull(ref));
PyStackRef_XSETREF(*method, ref);
return 1;
}
else if (ix == DKIX_EMPTY) {
return 0;
}
assert(ix == DKIX_KEY_CHANGED);
}
}
#endif

PyObject *obj;
Py_INCREF(mp);
Py_ssize_t ix = _Py_dict_lookup_threadsafe(mp, key, hash, &obj);
Py_DECREF(mp);
if (ix == DKIX_ERROR) {
PyStackRef_CLEAR(*method);
return -1;
}
else if (ix >= 0 && obj != NULL) {
PyStackRef_XSETREF(*method, PyStackRef_FromPyObjectSteal(obj));
return 1;
}
return 0; // not found
}

int
_PyDict_HasOnlyStringKeys(PyObject *dict)
{
Expand Down
Loading