Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve performance of arithmetic operations like multiplication and division for :class:`int`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This affects also string conversion.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but the gain is not really worth mentioning. For small number of digits the overhead dominates, for large number of digits the bigint algorithm in _pylong takes over.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On your benchmarks: 1.10x speedup, no? Looks too big for noise.

15 changes: 8 additions & 7 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ maybe_small_long(PyLongObject *v)
*/
#define HUGE_EXP_CUTOFF 60

#define SIGCHECK(PyTryBlock) \
do { \
if (PyErr_CheckSignals()) PyTryBlock \
/* Check for signals on every 64th iteration `i` of a loop. */
#define SIGCHECK(i, PyTryBlock) \
do { \
if ((((i) & 63) == 63) && PyErr_CheckSignals()) PyTryBlock \
} while(0)

/* Normalize (remove leading zeros from) an int object.
Expand Down Expand Up @@ -2186,7 +2187,7 @@ long_to_decimal_string_internal(PyObject *aa,
hi /= _PyLong_DECIMAL_BASE;
}
/* check for keyboard interrupt */
SIGCHECK({
SIGCHECK(i, {
Py_DECREF(scratch);
return -1;
});
Expand Down Expand Up @@ -3381,7 +3382,7 @@ x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
/* inner loop: divide vk[0:size_w+1] by w0[0:size_w], giving
single-digit quotient q, remainder in vk[0:size_w]. */

SIGCHECK({
SIGCHECK(vk - v0, {
Py_DECREF(a);
Py_DECREF(w);
Py_DECREF(v);
Expand Down Expand Up @@ -3954,7 +3955,7 @@ x_mul(PyLongObject *a, PyLongObject *b)
digit *pz = z->long_value.ob_digit + (i << 1);
digit *pa = a->long_value.ob_digit + i + 1;

SIGCHECK({
SIGCHECK(i, {
Py_DECREF(z);
return NULL;
});
Expand Down Expand Up @@ -4006,7 +4007,7 @@ x_mul(PyLongObject *a, PyLongObject *b)
digit *pb = b->long_value.ob_digit;
digit *pbend = b->long_value.ob_digit + size_b;

SIGCHECK({
SIGCHECK(i, {
Py_DECREF(z);
return NULL;
});
Expand Down
Loading