mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
bpo-47053: Refactor BINARY_OP_INPLACE_ADD_UNICODE (GH-32122)
This commit is contained in:
parent
bad86a621a
commit
788154919c
1 changed files with 18 additions and 14 deletions
|
|
@ -2003,28 +2003,32 @@ handle_eval_breaker:
|
||||||
DEOPT_IF(!PyUnicode_CheckExact(left), BINARY_OP);
|
DEOPT_IF(!PyUnicode_CheckExact(left), BINARY_OP);
|
||||||
DEOPT_IF(Py_TYPE(right) != Py_TYPE(left), BINARY_OP);
|
DEOPT_IF(Py_TYPE(right) != Py_TYPE(left), BINARY_OP);
|
||||||
_Py_CODEUNIT true_next = next_instr[INLINE_CACHE_ENTRIES_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 ||
|
assert(_Py_OPCODE(true_next) == STORE_FAST ||
|
||||||
_Py_OPCODE(true_next) == STORE_FAST__LOAD_FAST);
|
_Py_OPCODE(true_next) == STORE_FAST__LOAD_FAST);
|
||||||
/* In the common case, there are 2 references to the value
|
PyObject **target_local = &GETLOCAL(_Py_OPARG(true_next));
|
||||||
* stored in 'variable' when the v = v + ... is performed: one
|
DEOPT_IF(*target_local != left, BINARY_OP);
|
||||||
* 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);
|
|
||||||
STAT_INC(BINARY_OP, hit);
|
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);
|
assert(Py_REFCNT(left) >= 2);
|
||||||
Py_DECREF(left); // XXX never need to dealloc
|
Py_DECREF(left); // XXX never need to dealloc
|
||||||
STACK_SHRINK(1);
|
STACK_SHRINK(2);
|
||||||
PyUnicode_Append(&TOP(), right);
|
PyUnicode_Append(target_local, right);
|
||||||
Py_DECREF(right);
|
Py_DECREF(right);
|
||||||
if (TOP() == NULL) {
|
if (*target_local == NULL) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP);
|
// The STORE_FAST is already done.
|
||||||
|
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP + 1);
|
||||||
NOTRACE_DISPATCH();
|
NOTRACE_DISPATCH();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue