Skip to content

Commit 91fc62a

Browse files
committed
Only consider Py_ABS(Py_SIZE(int)) <= 1
1 parent 124693c commit 91fc62a

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

Include/internal/pycore_long.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ static inline PyObject* _PyLong_GetOne(void)
2424
PyObject *_PyLong_Add(PyLongObject *left, PyLongObject *right);
2525
PyObject *_PyLong_Multiply(PyLongObject *left, PyLongObject *right);
2626
PyObject *_PyLong_Subtract(PyLongObject *left, PyLongObject *right);
27-
Py_ssize_t _PyLong_RichCompare(PyLongObject *left, PyLongObject *right);
2827

2928
/* Used by Python/mystrtoul.c, _PyBytes_FromHex(),
3029
_PyBytes_DecodeEscape(), etc. */

Objects/longobject.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2956,23 +2956,15 @@ long_compare(PyLongObject *a, PyLongObject *b)
29562956
return sign;
29572957
}
29582958

2959-
Py_ssize_t
2960-
_PyLong_RichCompare(PyLongObject *left, PyLongObject *right)
2961-
{
2962-
if (left == right) {
2963-
return 0;
2964-
}
2965-
else {
2966-
return long_compare(left, right);
2967-
}
2968-
}
2969-
29702959
static PyObject *
29712960
long_richcompare(PyObject *self, PyObject *other, int op)
29722961
{
2962+
Py_ssize_t result;
29732963
CHECK_BINOP(self, other);
2974-
Py_ssize_t result = _PyLong_RichCompare((PyLongObject *)self,
2975-
(PyLongObject *)other);
2964+
if (self == other)
2965+
result = 0;
2966+
else
2967+
result = long_compare((PyLongObject*)self, (PyLongObject*)other);
29762968
Py_RETURN_RICHCOMPARE(result, 0, op);
29772969
}
29782970

Python/ceval.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3716,6 +3716,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
37163716
DEOPT_IF(!PyFloat_CheckExact(right), COMPARE_OP);
37173717
double dleft = PyFloat_AS_DOUBLE(left);
37183718
double dright = PyFloat_AS_DOUBLE(right);
3719+
int sign = (dleft > dright) - (dleft < dright);
37193720
DEOPT_IF(isnan(dleft), COMPARE_OP);
37203721
DEOPT_IF(isnan(dright), COMPARE_OP);
37213722
STAT_INC(COMPARE_OP, hit);
@@ -3724,7 +3725,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
37243725
Py_DECREF(left);
37253726
Py_DECREF(right);
37263727
assert(opcode == POP_JUMP_IF_TRUE || opcode == POP_JUMP_IF_FALSE);
3727-
int sign = (dleft > dright) - (dleft < dright);
37283728
int jump = (1 << (sign + 1)) & mask;
37293729
if (!jump) {
37303730
next_instr++;
@@ -3746,16 +3746,18 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
37463746
PyObject *left = SECOND();
37473747
DEOPT_IF(!PyLong_CheckExact(left), COMPARE_OP);
37483748
DEOPT_IF(!PyLong_CheckExact(right), COMPARE_OP);
3749+
DEOPT_IF((size_t)(Py_SIZE(left) + 1) > 2, COMPARE_OP);
3750+
DEOPT_IF((size_t)(Py_SIZE(right) + 1) > 2, COMPARE_OP);
37493751
STAT_INC(COMPARE_OP, hit);
3750-
// This cannot fail.
3751-
Py_ssize_t cmp = _PyLong_RichCompare((PyLongObject *)left,
3752-
(PyLongObject *)right);
3752+
assert(Py_ABS(Py_SIZE(left)) <= 1 && Py_ABS(Py_SIZE(right)) <= 1);
3753+
Py_ssize_t ileft = Py_SIZE(left) * ((PyLongObject *)left)->ob_digit[0];
3754+
Py_ssize_t iright = Py_SIZE(right) * ((PyLongObject *)right)->ob_digit[0];
3755+
int sign = (ileft > iright) - (ileft < iright);
37533756
NEXTOPARG();
37543757
STACK_SHRINK(2);
37553758
Py_DECREF(left);
37563759
Py_DECREF(right);
37573760
assert(opcode == POP_JUMP_IF_TRUE || opcode == POP_JUMP_IF_FALSE);
3758-
int sign = (cmp > 0) - (cmp < 0);
37593761
int jump = (1 << (sign + 1)) & mask;
37603762
if (!jump) {
37613763
next_instr++;

Python/specialize.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ initial_counter_value(void) {
495495
#define SPEC_FAIL_STRING_COMPARE 13
496496
#define SPEC_FAIL_STRING_UNREADY 14
497497
#define SPEC_FAIL_NOT_FOLLOWED_BY_COND_JUMP 15
498+
#define SPEC_FAIL_BIG_INT 16
498499

499500
static int
500501
specialize_module_load_attr(
@@ -1589,9 +1590,15 @@ _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs,
15891590
goto success;
15901591
}
15911592
if (PyLong_CheckExact(lhs)) {
1592-
*instr = _Py_MAKECODEUNIT(COMPARE_OP_INT_JUMP, _Py_OPARG(*instr));
1593-
cache1->mask = mask;
1594-
goto success;
1593+
if (Py_ABS(Py_SIZE(lhs)) <= 1 && Py_ABS(Py_SIZE(rhs)) <= 1) {
1594+
*instr = _Py_MAKECODEUNIT(COMPARE_OP_INT_JUMP, _Py_OPARG(*instr));
1595+
cache1->mask = mask;
1596+
goto success;
1597+
}
1598+
else {
1599+
SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_BIG_INT);
1600+
goto failure;
1601+
}
15951602
}
15961603
if (PyUnicode_CheckExact(lhs)) {
15971604
if (op != Py_EQ && op != Py_NE) {

0 commit comments

Comments
 (0)