From 7a7463253d8197e4c436679ef5d3c5851683c546 Mon Sep 17 00:00:00 2001 From: animalize Date: Wed, 9 Jan 2019 17:24:31 +0800 Subject: [PATCH 1/2] remove unnecessary operation in long_compare() We only need the sign, converting to -1 or +1 is not necessary. --- Objects/longobject.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Objects/longobject.c b/Objects/longobject.c index f42683e9d024ce7..660d34e2959f73d 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -2992,7 +2992,11 @@ long_dealloc(PyObject *v) Py_TYPE(v)->tp_free(v); } -static int +/* Returns a negative number when a < b + Returns 0 when a = b + Returns a positive number when a > b */ + +static Py_ssize_t long_compare(PyLongObject *a, PyLongObject *b) { Py_ssize_t sign; @@ -3012,13 +3016,13 @@ long_compare(PyLongObject *a, PyLongObject *b) sign = -sign; } } - return sign < 0 ? -1 : sign > 0 ? 1 : 0; + return sign; } static PyObject * long_richcompare(PyObject *self, PyObject *other, int op) { - int result; + Py_ssize_t result; CHECK_BINOP(self, other); if (self == other) result = 0; @@ -5023,7 +5027,8 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b) { PyLongObject *quo = NULL, *rem = NULL; PyObject *twice_rem, *result, *temp; - int cmp, quo_is_odd, quo_is_neg; + Py_ssize_t cmp; + int quo_is_odd, quo_is_neg; /* Equivalent Python code: From 645c3debefbc58357bda1854208fb501a4e10a55 Mon Sep 17 00:00:00 2001 From: animalize Date: Thu, 10 Jan 2019 11:33:41 +0800 Subject: [PATCH 2/2] check the second argument first when using CHECK_BINOP This may save a compare. --- Objects/longobject.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Objects/longobject.c b/Objects/longobject.c index 660d34e2959f73d..25bbdcf967294a0 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -3023,7 +3023,7 @@ static PyObject * long_richcompare(PyObject *self, PyObject *other, int op) { Py_ssize_t result; - CHECK_BINOP(self, other); + CHECK_BINOP(other, self); if (self == other) result = 0; else @@ -3184,7 +3184,7 @@ long_add(PyLongObject *a, PyLongObject *b) { PyLongObject *z; - CHECK_BINOP(a, b); + CHECK_BINOP(b, a); if (Py_ABS(Py_SIZE(a)) <= 1 && Py_ABS(Py_SIZE(b)) <= 1) { return PyLong_FromLong(MEDIUM_VALUE(a) + MEDIUM_VALUE(b)); @@ -3218,7 +3218,7 @@ long_sub(PyLongObject *a, PyLongObject *b) { PyLongObject *z; - CHECK_BINOP(a, b); + CHECK_BINOP(b, a); if (Py_ABS(Py_SIZE(a)) <= 1 && Py_ABS(Py_SIZE(b)) <= 1) { return PyLong_FromLong(MEDIUM_VALUE(a) - MEDIUM_VALUE(b)); @@ -3650,7 +3650,7 @@ long_mul(PyLongObject *a, PyLongObject *b) { PyLongObject *z; - CHECK_BINOP(a, b); + CHECK_BINOP(b, a); /* fast path for single-digit multiplication */ if (Py_ABS(Py_SIZE(a)) <= 1 && Py_ABS(Py_SIZE(b)) <= 1) { @@ -3804,7 +3804,7 @@ long_div(PyObject *a, PyObject *b) { PyLongObject *div; - CHECK_BINOP(a, b); + CHECK_BINOP(b, a); if (Py_ABS(Py_SIZE(a)) == 1 && Py_ABS(Py_SIZE(b)) == 1) { return fast_floor_div((PyLongObject*)a, (PyLongObject*)b); @@ -3829,7 +3829,7 @@ long_true_divide(PyObject *v, PyObject *w) int inexact, negate, a_is_small, b_is_small; double dx, result; - CHECK_BINOP(v, w); + CHECK_BINOP(w, v); a = (PyLongObject *)v; b = (PyLongObject *)w; @@ -4083,7 +4083,7 @@ long_mod(PyObject *a, PyObject *b) { PyLongObject *mod; - CHECK_BINOP(a, b); + CHECK_BINOP(b, a); if (Py_ABS(Py_SIZE(a)) == 1 && Py_ABS(Py_SIZE(b)) == 1) { return fast_mod((PyLongObject*)a, (PyLongObject*)b); @@ -4100,7 +4100,7 @@ long_divmod(PyObject *a, PyObject *b) PyLongObject *div, *mod; PyObject *z; - CHECK_BINOP(a, b); + CHECK_BINOP(b, a); if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) { return NULL; @@ -4135,7 +4135,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x) 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; /* a, b, c = v, w, x */ - CHECK_BINOP(v, w); + CHECK_BINOP(w, v); a = (PyLongObject*)v; Py_INCREF(a); b = (PyLongObject*)w; Py_INCREF(b); if (PyLong_Check(x)) { @@ -4391,7 +4391,7 @@ long_rshift(PyLongObject *a, PyLongObject *b) Py_ssize_t newsize, wordshift, hishift, i, j; digit loshift, lomask, himask; - CHECK_BINOP(a, b); + CHECK_BINOP(b, a); if (Py_SIZE(b) < 0) { PyErr_SetString(PyExc_ValueError, @@ -4445,7 +4445,7 @@ long_lshift(PyObject *v, PyObject *w) digit remshift; twodigits accum; - CHECK_BINOP(a, b); + CHECK_BINOP(b, a); if (Py_SIZE(b) < 0) { PyErr_SetString(PyExc_ValueError, "negative shift count"); @@ -4630,7 +4630,7 @@ static PyObject * long_and(PyObject *a, PyObject *b) { PyObject *c; - CHECK_BINOP(a, b); + CHECK_BINOP(b, a); c = long_bitwise((PyLongObject*)a, '&', (PyLongObject*)b); return c; } @@ -4639,7 +4639,7 @@ static PyObject * long_xor(PyObject *a, PyObject *b) { PyObject *c; - CHECK_BINOP(a, b); + CHECK_BINOP(b, a); c = long_bitwise((PyLongObject*)a, '^', (PyLongObject*)b); return c; } @@ -4648,7 +4648,7 @@ static PyObject * long_or(PyObject *a, PyObject *b) { PyObject *c; - CHECK_BINOP(a, b); + CHECK_BINOP(b, a); c = long_bitwise((PyLongObject*)a, '|', (PyLongObject*)b); return c; }