Skip to content

Commit 9d13a23

Browse files
committed
review comments
1 parent af5b4c9 commit 9d13a23

File tree

3 files changed

+213
-226
lines changed

3 files changed

+213
-226
lines changed

Python/bytecodes.c

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -709,98 +709,96 @@ dummy_func(
709709
_GUARD_TOS_INT + _GUARD_NOS_INT + unused/5 + _BINARY_OP_SUBTRACT_INT + _POP_TOP_INT + _POP_TOP_INT;
710710

711711
// Inplace compact int ops: mutate the uniquely-referenced operand
712-
// when possible. If TARGET is immortal (small int) or the result
713-
// is a small int or overflows, fall back to _PyCompactLong_*.
714-
// On mutation success, DUP the target so POP_TOP_INT can safely
715-
// decref the original. Tier 2 only.
712+
// when possible. The op handles decref of TARGET internally so
713+
// the following _POP_TOP_INT becomes _POP_TOP_NOP. Tier 2 only.
716714
tier2 op(_BINARY_OP_ADD_INT_INPLACE, (left, right -- res, l, r)) {
717-
INT_INPLACE_OP(left, right, left, +);
715+
INT_INPLACE_OP(left, right, left, +, _PyCompactLong_Add);
716+
r = right;
717+
DEAD(right);
718+
EXIT_IF(!_int_inplace_ok && PyStackRef_IsNull(_int_inplace_res));
718719
if (_int_inplace_ok) {
719720
res = PyStackRef_DUP(left);
720721
}
721722
else {
722-
res = _PyCompactLong_Add((PyLongObject *)PyStackRef_AsPyObjectBorrow(left),
723-
(PyLongObject *)PyStackRef_AsPyObjectBorrow(right));
724-
EXIT_IF(PyStackRef_IsNull(res));
723+
res = _int_inplace_res;
725724
}
726725
l = left;
727-
r = right;
728-
INPUTS_DEAD();
726+
DEAD(left);
729727
}
730728

731729
tier2 op(_BINARY_OP_SUBTRACT_INT_INPLACE, (left, right -- res, l, r)) {
732-
INT_INPLACE_OP(left, right, left, -);
730+
INT_INPLACE_OP(left, right, left, -, _PyCompactLong_Subtract);
731+
r = right;
732+
DEAD(right);
733+
EXIT_IF(!_int_inplace_ok && PyStackRef_IsNull(_int_inplace_res));
733734
if (_int_inplace_ok) {
734735
res = PyStackRef_DUP(left);
735736
}
736737
else {
737-
res = _PyCompactLong_Subtract((PyLongObject *)PyStackRef_AsPyObjectBorrow(left),
738-
(PyLongObject *)PyStackRef_AsPyObjectBorrow(right));
739-
EXIT_IF(PyStackRef_IsNull(res));
738+
res = _int_inplace_res;
740739
}
741740
l = left;
742-
r = right;
743-
INPUTS_DEAD();
741+
DEAD(left);
744742
}
745743

746744
tier2 op(_BINARY_OP_MULTIPLY_INT_INPLACE, (left, right -- res, l, r)) {
747-
INT_INPLACE_OP(left, right, left, *);
745+
INT_INPLACE_OP(left, right, left, *, _PyCompactLong_Multiply);
746+
r = right;
747+
DEAD(right);
748+
EXIT_IF(!_int_inplace_ok && PyStackRef_IsNull(_int_inplace_res));
748749
if (_int_inplace_ok) {
749750
res = PyStackRef_DUP(left);
750751
}
751752
else {
752-
res = _PyCompactLong_Multiply((PyLongObject *)PyStackRef_AsPyObjectBorrow(left),
753-
(PyLongObject *)PyStackRef_AsPyObjectBorrow(right));
754-
EXIT_IF(PyStackRef_IsNull(res));
753+
res = _int_inplace_res;
755754
}
756755
l = left;
757-
r = right;
758-
INPUTS_DEAD();
756+
DEAD(left);
759757
}
760758

761759
tier2 op(_BINARY_OP_ADD_INT_INPLACE_RIGHT, (left, right -- res, l, r)) {
762-
INT_INPLACE_OP(left, right, right, +);
760+
INT_INPLACE_OP(left, right, right, +, _PyCompactLong_Add);
761+
l = left;
762+
DEAD(left);
763+
EXIT_IF(!_int_inplace_ok && PyStackRef_IsNull(_int_inplace_res));
763764
if (_int_inplace_ok) {
764765
res = PyStackRef_DUP(right);
765766
}
766767
else {
767-
res = _PyCompactLong_Add((PyLongObject *)PyStackRef_AsPyObjectBorrow(left),
768-
(PyLongObject *)PyStackRef_AsPyObjectBorrow(right));
769-
EXIT_IF(PyStackRef_IsNull(res));
768+
res = _int_inplace_res;
770769
}
771-
l = left;
772770
r = right;
773-
INPUTS_DEAD();
771+
DEAD(right);
774772
}
775773

776774
tier2 op(_BINARY_OP_SUBTRACT_INT_INPLACE_RIGHT, (left, right -- res, l, r)) {
777-
INT_INPLACE_OP(left, right, right, -);
775+
INT_INPLACE_OP(left, right, right, -, _PyCompactLong_Subtract);
776+
l = left;
777+
DEAD(left);
778+
EXIT_IF(!_int_inplace_ok && PyStackRef_IsNull(_int_inplace_res));
778779
if (_int_inplace_ok) {
779780
res = PyStackRef_DUP(right);
780781
}
781782
else {
782-
res = _PyCompactLong_Subtract((PyLongObject *)PyStackRef_AsPyObjectBorrow(left),
783-
(PyLongObject *)PyStackRef_AsPyObjectBorrow(right));
784-
EXIT_IF(PyStackRef_IsNull(res));
783+
res = _int_inplace_res;
785784
}
786-
l = left;
787785
r = right;
788-
INPUTS_DEAD();
786+
DEAD(right);
789787
}
790788

791789
tier2 op(_BINARY_OP_MULTIPLY_INT_INPLACE_RIGHT, (left, right -- res, l, r)) {
792-
INT_INPLACE_OP(left, right, right, *);
790+
INT_INPLACE_OP(left, right, right, *, _PyCompactLong_Multiply);
791+
l = left;
792+
DEAD(left);
793+
EXIT_IF(!_int_inplace_ok && PyStackRef_IsNull(_int_inplace_res));
793794
if (_int_inplace_ok) {
794795
res = PyStackRef_DUP(right);
795796
}
796797
else {
797-
res = _PyCompactLong_Multiply((PyLongObject *)PyStackRef_AsPyObjectBorrow(left),
798-
(PyLongObject *)PyStackRef_AsPyObjectBorrow(right));
799-
EXIT_IF(PyStackRef_IsNull(res));
798+
res = _int_inplace_res;
800799
}
801-
l = left;
802800
r = right;
803-
INPUTS_DEAD();
801+
DEAD(right);
804802
}
805803

806804
op(_GUARD_NOS_FLOAT, (left, unused -- left, unused)) {

Python/ceval_macros.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -568,12 +568,14 @@ gen_try_set_executing(PyGenObject *gen)
568568
// cached small int singleton. We check _Py_IsImmortal on TARGET
569569
// to decide whether inplace mutation is safe.
570570
//
571-
// After the macro, exactly one of:
572-
// _int_inplace_ok = 1: mutated TARGET in place (res = TARGET)
573-
// _int_inplace_ok = 0: did not mutate; caller should call
574-
// _PyCompactLong_* as fallback
575-
#define INT_INPLACE_OP(left, right, TARGET, OP) \
571+
// After the macro:
572+
// _int_inplace_ok = 1: mutated TARGET in place (caller uses TARGET as res)
573+
// _int_inplace_ok = 0: _int_inplace_res holds the fallback result
574+
// (may be NULL on allocation failure)
575+
// FUNC is the fallback function (_PyCompactLong_Add etc.)
576+
#define INT_INPLACE_OP(left, right, TARGET, OP, FUNC) \
576577
int _int_inplace_ok = 0; \
578+
_PyStackRef _int_inplace_res = PyStackRef_NULL; \
577579
do { \
578580
PyObject *_target_o = PyStackRef_AsPyObjectBorrow(TARGET); \
579581
if (_Py_IsImmortal(_target_o)) { \
@@ -595,5 +597,10 @@ gen_try_set_executing(PyGenObject *gen)
595597
(digit)(_result < 0 ? -_result : _result); \
596598
_int_inplace_ok = 1; \
597599
} \
598-
} while (0)
600+
} while (0); \
601+
if (!_int_inplace_ok) { \
602+
_int_inplace_res = FUNC( \
603+
(PyLongObject *)PyStackRef_AsPyObjectBorrow(left), \
604+
(PyLongObject *)PyStackRef_AsPyObjectBorrow(right)); \
605+
}
599606

0 commit comments

Comments
 (0)