diff -r fd0c02c3df31 Objects/longobject.c --- a/Objects/longobject.c Fri Sep 26 09:04:19 2014 +0300 +++ b/Objects/longobject.c Fri Sep 26 09:36:32 2014 +0200 @@ -2372,6 +2372,7 @@ static PyLongObject *x_divrem (PyLongObject *, PyLongObject *, PyLongObject **); static PyObject *long_long(PyObject *v); +static PyObject *long_neg(PyLongObject *v); /* Int division with remainder, top-level routine */ @@ -2387,7 +2388,7 @@ "integer division or modulo by zero"); return -1; } - if (size_a < size_b || + if (size_a == 0 || size_a < size_b || (size_a == size_b && a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) { /* |a| < |b|. */ @@ -3502,6 +3503,19 @@ PyLongObject *div; CHECK_BINOP(a, b); + + /* Fast path for division by 1 or -1 */ + if (Py_SIZE(b) == 1) { + if (((PyLongObject*)b)->ob_digit[0] == 1) { + Py_INCREF(a); + return a; + } + } + else if (Py_SIZE(b) == -1) { + if (((PyLongObject*)b)->ob_digit[0] == 1) + return long_neg((PyLongObject*)a); + } + if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0) div = NULL; return (PyObject *)div; @@ -3624,6 +3638,15 @@ if (a_size == 0) goto underflow_or_zero; + /* Fast path for division by 1 or -1 */ + if (b_size == 1 && b->ob_digit[0] == 1) { + if (Py_SIZE(b) == -1) + v = long_neg(a); + else + Py_INCREF(v); + return v; + } + /* Fast path for a and b small (exactly representable in a double). Relies on floating-point division being correctly rounded; results may be subject to double rounding on x86 machines that operate with