@@ -1597,19 +1597,11 @@ insertion_resize(PyInterpreterState *interp, PyDictObject *mp, int unicode)
1597
1597
}
1598
1598
1599
1599
static Py_ssize_t
1600
- insert_into_splitdictkeys (PyDictKeysObject * keys , PyObject * name )
1600
+ insert_into_splitdictkeys (PyDictKeysObject * keys , PyObject * name , Py_hash_t hash )
1601
1601
{
1602
1602
assert (PyUnicode_CheckExact (name ));
1603
1603
ASSERT_KEYS_LOCKED (keys );
1604
1604
1605
- Py_hash_t hash = unicode_get_hash (name );
1606
- if (hash == -1 ) {
1607
- hash = PyUnicode_Type .tp_hash (name );
1608
- if (hash == -1 ) {
1609
- PyErr_Clear ();
1610
- return DKIX_EMPTY ;
1611
- }
1612
- }
1613
1605
Py_ssize_t ix = unicodekeys_lookup_unicode (keys , name , hash );
1614
1606
if (ix == DKIX_EMPTY ) {
1615
1607
if (keys -> dk_usable <= 0 ) {
@@ -6692,8 +6684,25 @@ _PyObject_StoreInstanceAttribute(PyObject *obj, PyDictValues *values,
6692
6684
assert (Py_TYPE (obj )-> tp_flags & Py_TPFLAGS_MANAGED_DICT );
6693
6685
Py_ssize_t ix = DKIX_EMPTY ;
6694
6686
if (PyUnicode_CheckExact (name )) {
6695
- LOCK_KEYS (keys );
6696
- ix = insert_into_splitdictkeys (keys , name );
6687
+ Py_hash_t hash = unicode_get_hash (name );
6688
+ if (hash == -1 ) {
6689
+ hash = PyUnicode_Type .tp_hash (name );
6690
+ assert (hash != -1 );
6691
+ }
6692
+
6693
+ #ifdef Py_GIL_DISABLED
6694
+ // Try a thread-safe lookup to see if the index is already allocated
6695
+ ix = unicodekeys_lookup_unicode_threadsafe (keys , name , hash );
6696
+ if (ix == DKIX_EMPTY ) {
6697
+ // Lock keys and do insert
6698
+ LOCK_KEYS (keys );
6699
+ ix = insert_into_splitdictkeys (keys , name , hash );
6700
+ UNLOCK_KEYS (keys );
6701
+ }
6702
+ #else
6703
+ ix = insert_into_splitdictkeys (keys , name , hash );
6704
+ #endif
6705
+
6697
6706
#ifdef Py_STATS
6698
6707
if (ix == DKIX_EMPTY ) {
6699
6708
if (PyUnicode_CheckExact (name )) {
@@ -6709,7 +6718,6 @@ _PyObject_StoreInstanceAttribute(PyObject *obj, PyDictValues *values,
6709
6718
}
6710
6719
}
6711
6720
#endif
6712
- UNLOCK_KEYS (keys );
6713
6721
}
6714
6722
if (ix == DKIX_EMPTY ) {
6715
6723
PyObject * dict = make_dict_from_instance_attributes (
0 commit comments