@@ -741,9 +741,11 @@ dict_add_o(PyObject *dict, PyObject *o)
741741 return arg ;
742742}
743743
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+ */
745747static PyObject *
746- merge_consts_recursive (PyObject * const_cache , PyObject * o )
748+ const_cache_insert (PyObject * const_cache , PyObject * o , bool recursive )
747749{
748750 assert (PyDict_CheckExact (const_cache ));
749751 // None and Ellipsis are immortal objects, and key is the singleton.
@@ -767,14 +769,18 @@ merge_consts_recursive(PyObject *const_cache, PyObject *o)
767769 }
768770 Py_DECREF (t );
769771
772+ if (!recursive ) {
773+ return key ;
774+ }
775+
770776 // We registered o in const_cache.
771777 // When o is a tuple or frozenset, we want to merge its
772778 // items too.
773779 if (PyTuple_CheckExact (o )) {
774780 Py_ssize_t len = PyTuple_GET_SIZE (o );
775781 for (Py_ssize_t i = 0 ; i < len ; i ++ ) {
776782 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 );
778784 if (u == NULL ) {
779785 Py_DECREF (key );
780786 return NULL ;
@@ -816,7 +822,7 @@ merge_consts_recursive(PyObject *const_cache, PyObject *o)
816822 PyObject * item ;
817823 Py_hash_t hash ;
818824 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 );
820826 if (k == NULL ) {
821827 Py_DECREF (tuple );
822828 Py_DECREF (key );
@@ -850,6 +856,12 @@ merge_consts_recursive(PyObject *const_cache, PyObject *o)
850856 return key ;
851857}
852858
859+ static PyObject *
860+ merge_consts_recursive (PyObject * const_cache , PyObject * o )
861+ {
862+ return const_cache_insert (const_cache , o , true);
863+ }
864+
853865static Py_ssize_t
854866compiler_add_const (PyObject * const_cache , struct compiler_unit * u , PyObject * o )
855867{
@@ -7506,37 +7518,22 @@ compute_code_flags(struct compiler *c)
75067518 return flags ;
75077519}
75087520
7509- // Merge *obj* with constant cache.
7510- // Unlike merge_consts_recursive(), this function doesn't work recursively.
7521+ // Merge *obj* with constant cache, without recursion.
75117522int
75127523_PyCompile_ConstCacheMergeOne (PyObject * const_cache , PyObject * * obj )
75137524{
7514- assert (PyDict_CheckExact (const_cache ));
7515- PyObject * key = _PyCode_ConstantKey (* obj );
7525+ PyObject * key = const_cache_insert (const_cache , * obj , false);
75167526 if (key == NULL ) {
75177527 return ERROR ;
75187528 }
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 );
7529+ if (PyTuple_CheckExact (key )) {
7530+ PyObject * item = PyTuple_GET_ITEM (key , 1 );
75337531 Py_SETREF (* obj , Py_NewRef (item ));
7534- Py_DECREF (t );
7532+ Py_DECREF (key );
75357533 }
75367534 else {
7537- Py_SETREF (* obj , t );
7535+ Py_SETREF (* obj , key );
75387536 }
7539-
75407537 return SUCCESS ;
75417538}
75427539
0 commit comments