bpo-47053: Refactor BINARY_OP_INPLACE_ADD_UNICODE (GH-32122)

https://github.com/python/cpython/commit/788154919c2d843a0a995994bf2aed2d074... commit: 788154919c2d843a0a995994bf2aed2d074761ec branch: main author: Dennis Sweeney <36520290+sweeneyde@users.noreply.github.com> committer: sweeneyde <36520290+sweeneyde@users.noreply.github.com> date: 2022-03-28T22:07:05-04:00 summary: bpo-47053: Refactor BINARY_OP_INPLACE_ADD_UNICODE (GH-32122) files: M Python/ceval.c diff --git a/Python/ceval.c b/Python/ceval.c index 8b59f4233a7b2..4b9d33404bb78 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -2003,28 +2003,32 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int 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(); }
participants (1)
-
sweeneyde