]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-47053: Refactor BINARY_OP_INPLACE_ADD_UNICODE (GH-32122)
authorDennis Sweeney <36520290+sweeneyde@users.noreply.github.com>
Tue, 29 Mar 2022 02:07:05 +0000 (22:07 -0400)
committerGitHub <noreply@github.com>
Tue, 29 Mar 2022 02:07:05 +0000 (22:07 -0400)
Python/ceval.c

index 8b59f4233a7b23f7fd487357fc4a568f5f3459e0..4b9d33404bb78bd6e50227bfc67b4d3596de50e7 100644 (file)
@@ -2003,28 +2003,32 @@ handle_eval_breaker:
             DEOPT_IF(!PyUnicode_CheckExact(left), BINARY_OP);
             DEOPT_IF(Py_TYPE(right) != Py_TYPE(left), BINARY_OP);
             _Py_CODEUNIT true_next = next_instr[INLINE_CACHE_ENTRIES_BINARY_OP];
-            int next_oparg = _Py_OPARG(true_next);
             assert(_Py_OPCODE(true_next) == STORE_FAST ||
                    _Py_OPCODE(true_next) == STORE_FAST__LOAD_FAST);
-            /* In the common case, there are 2 references to the value
-            * stored in 'variable' when the v = v + ... is performed: one
-            * on the value stack (in 'v') and one still stored in the
-            * 'variable'.  We try to delete the variable now to reduce
-            * the refcnt to 1.
-            */
-            PyObject *var = GETLOCAL(next_oparg);
-            DEOPT_IF(var != left, BINARY_OP);
+            PyObject **target_local = &GETLOCAL(_Py_OPARG(true_next));
+            DEOPT_IF(*target_local != left, BINARY_OP);
             STAT_INC(BINARY_OP, hit);
-            GETLOCAL(next_oparg) = NULL;
+            /* Handle `left = left + right` or `left += right` for str.
+             *
+             * When possible, extend `left` in place rather than
+             * allocating a new PyUnicodeObject. This attempts to avoid
+             * quadratic behavior when one neglects to use str.join().
+             *
+             * If `left` has only two references remaining (one from
+             * the stack, one in the locals), DECREFing `left` leaves
+             * only the locals reference, so PyUnicode_Append knows
+             * that the string is safe to mutate.
+             */
             assert(Py_REFCNT(left) >= 2);
             Py_DECREF(left); // XXX never need to dealloc
-            STACK_SHRINK(1);
-            PyUnicode_Append(&TOP(), right);
+            STACK_SHRINK(2);
+            PyUnicode_Append(target_local, right);
             Py_DECREF(right);
-            if (TOP() == NULL) {
+            if (*target_local == NULL) {
                 goto error;
             }
-            JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP);
+            // The STORE_FAST is already done.
+            JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP + 1);
             NOTRACE_DISPATCH();
         }