@@ -741,9 +741,11 @@ dict_add_o(PyObject *dict, PyObject *o)
741
741
return arg ;
742
742
}
743
743
744
- // Merge const *o* recursively and return constant key object.
744
+ /* Merge const *o* and return constant key object.
745
+ * If recursive, insert all elements if o is a tuple or frozen set.
746
+ */
745
747
static PyObject *
746
- merge_consts_recursive (PyObject * const_cache , PyObject * o )
748
+ const_cache_insert (PyObject * const_cache , PyObject * o , bool recursive )
747
749
{
748
750
assert (PyDict_CheckExact (const_cache ));
749
751
// None and Ellipsis are immortal objects, and key is the singleton.
@@ -767,14 +769,18 @@ merge_consts_recursive(PyObject *const_cache, PyObject *o)
767
769
}
768
770
Py_DECREF (t );
769
771
772
+ if (!recursive ) {
773
+ return key ;
774
+ }
775
+
770
776
// We registered o in const_cache.
771
777
// When o is a tuple or frozenset, we want to merge its
772
778
// items too.
773
779
if (PyTuple_CheckExact (o )) {
774
780
Py_ssize_t len = PyTuple_GET_SIZE (o );
775
781
for (Py_ssize_t i = 0 ; i < len ; i ++ ) {
776
782
PyObject * item = PyTuple_GET_ITEM (o , i );
777
- PyObject * u = merge_consts_recursive (const_cache , item );
783
+ PyObject * u = const_cache_insert (const_cache , item , recursive );
778
784
if (u == NULL ) {
779
785
Py_DECREF (key );
780
786
return NULL ;
@@ -816,7 +822,7 @@ merge_consts_recursive(PyObject *const_cache, PyObject *o)
816
822
PyObject * item ;
817
823
Py_hash_t hash ;
818
824
while (_PySet_NextEntry (o , & pos , & item , & hash )) {
819
- PyObject * k = merge_consts_recursive (const_cache , item );
825
+ PyObject * k = const_cache_insert (const_cache , item , recursive );
820
826
if (k == NULL ) {
821
827
Py_DECREF (tuple );
822
828
Py_DECREF (key );
@@ -854,7 +860,7 @@ static Py_ssize_t
854
860
compiler_add_const (PyObject * const_cache , struct compiler_unit * u , PyObject * o )
855
861
{
856
862
assert (PyDict_CheckExact (const_cache ));
857
- PyObject * key = merge_consts_recursive (const_cache , o );
863
+ PyObject * key = const_cache_insert (const_cache , o , true );
858
864
if (key == NULL ) {
859
865
return ERROR ;
860
866
}
@@ -7506,37 +7512,22 @@ compute_code_flags(struct compiler *c)
7506
7512
return flags ;
7507
7513
}
7508
7514
7509
- // Merge *obj* with constant cache.
7510
- // Unlike merge_consts_recursive(), this function doesn't work recursively.
7515
+ // Merge *obj* with constant cache, without recursion.
7511
7516
int
7512
7517
_PyCompile_ConstCacheMergeOne (PyObject * const_cache , PyObject * * obj )
7513
7518
{
7514
- assert (PyDict_CheckExact (const_cache ));
7515
- PyObject * key = _PyCode_ConstantKey (* obj );
7519
+ PyObject * key = const_cache_insert (const_cache , * obj , false);
7516
7520
if (key == NULL ) {
7517
7521
return ERROR ;
7518
7522
}
7519
-
7520
- PyObject * t ;
7521
- int res = PyDict_SetDefaultRef (const_cache , key , key , & t );
7522
- Py_DECREF (key );
7523
- if (res < 0 ) {
7524
- return ERROR ;
7525
- }
7526
- if (res == 0 ) { // inserted: obj is new constant.
7527
- Py_DECREF (t );
7528
- return SUCCESS ;
7529
- }
7530
-
7531
- if (PyTuple_CheckExact (t )) {
7532
- PyObject * item = PyTuple_GET_ITEM (t , 1 );
7523
+ if (PyTuple_CheckExact (key )) {
7524
+ PyObject * item = PyTuple_GET_ITEM (key , 1 );
7533
7525
Py_SETREF (* obj , Py_NewRef (item ));
7534
- Py_DECREF (t );
7526
+ Py_DECREF (key );
7535
7527
}
7536
7528
else {
7537
- Py_SETREF (* obj , t );
7529
+ Py_SETREF (* obj , key );
7538
7530
}
7539
-
7540
7531
return SUCCESS ;
7541
7532
}
7542
7533
0 commit comments