Skip to content

Commit e4c36a8

Browse files
avoid incref/decref for unicode keys dict
1 parent a5498aa commit e4c36a8

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

Objects/object.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,27 +1757,30 @@ _PyObject_GetMethodStackRef(PyThreadState *ts, PyObject *obj,
17571757
Py_hash_t hash = _PyObject_HashFast(name);
17581758
// cannot fail for exact unicode
17591759
assert(hash != -1);
1760-
Py_INCREF(dict);
17611760
// ref is not visible to gc so there should be
17621761
// no escaping calls before assigning it to method
1762+
PyDictObject *mp = (PyDictObject *)dict;
1763+
bool unicode_keys = DK_IS_UNICODE(FT_ATOMIC_LOAD_PTR_ACQUIRE(mp->ma_keys));
17631764
_PyStackRef ref;
1764-
Py_ssize_t ix = _Py_dict_lookup_threadsafe_stackref((PyDictObject *)dict,
1765-
name, hash, &ref);
1765+
if (!unicode_keys) {
1766+
Py_INCREF(mp);
1767+
}
1768+
Py_ssize_t ix = _Py_dict_lookup_threadsafe_stackref(mp, name,
1769+
hash, &ref);
1770+
if (!unicode_keys) {
1771+
Py_DECREF(mp);
1772+
}
17661773
if (ix == DKIX_ERROR) {
17671774
// error
17681775
PyStackRef_CLEAR(*method);
1769-
Py_DECREF(dict);
17701776
return -1;
17711777
} else if (!PyStackRef_IsNull(ref)) {
17721778
// found
17731779
_PyStackRef tmp = *method;
17741780
*method = ref;
17751781
PyStackRef_XCLOSE(tmp);
1776-
Py_DECREF(dict);
17771782
return 0;
17781783
}
1779-
// not found
1780-
Py_DECREF(dict);
17811784
}
17821785

17831786
if (meth_found) {

0 commit comments

Comments
 (0)