summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorDennis Sweeney <36520290+sweeneyde@users.noreply.github.com>2022-03-29 02:07:05 (GMT)
committerGitHub <noreply@github.com>2022-03-29 02:07:05 (GMT)
commit788154919c2d843a0a995994bf2aed2d074761ec (patch)
tree4c408648a6f79b03b92ce20cdc930b99cf4f81c5 /Python
parentbad86a621af61f383b9f06fe4a08f66245df99e2 (diff)
downloadcpython-788154919c2d843a0a995994bf2aed2d074761ec.zip
cpython-788154919c2d843a0a995994bf2aed2d074761ec.tar.gz
cpython-788154919c2d843a0a995994bf2aed2d074761ec.tar.bz2
bpo-47053: Refactor BINARY_OP_INPLACE_ADD_UNICODE (GH-32122)
Diffstat (limited to 'Python')
-rw-r--r--Python/ceval.c32
1 files changed, 18 insertions, 14 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index 8b59f42..4b9d334 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -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();
}