The Wayback Machine - https://web.archive.org/web/20260219014527/https://github.com/python/cpython/pull/118590
Skip to content

[3.12] gh-118164: Break a loop between _pydecimal and _pylong and optimize int to str conversion (GH-118483)#118590

Merged
serhiy-storchaka merged 1 commit intopython:3.12from
miss-islington:backport-711c80b-3.12
May 6, 2024
Merged

[3.12] gh-118164: Break a loop between _pydecimal and _pylong and optimize int to str conversion (GH-118483)#118590
serhiy-storchaka merged 1 commit intopython:3.12from
miss-islington:backport-711c80b-3.12

Conversation

@miss-islington
Copy link
Contributor

@miss-islington miss-islington commented May 5, 2024 •

For converting large ints to strings, CPython invokes a function in _pylong.py,
which uses the decimal module to implement an asymptotically waaaaay
sub-quadratic algorithm. But if the C decimal module isn't available, CPython
uses _pydecimal.py instead. Which in turn frequently does str(int). If the int
is very large, _pylong ends up doing the work, which in turn asks decimal to do
"big" arithmetic, which in turn calls str(big_int), which in turn ... it can
become infinite mutual recursion.

This change introduces a different int->str function that doesn't use decimal.
It's asymptotically worse, "Karatsuba time" instead of quadratic time, so
still a huge improvement. _pylong switches to that when the C decimal isn't
available. It is also used for not too large integers (less than 450_000 bits),
where it is faster (up to 2 times for 30_000 bits) than the asymptotically
better implementation that uses the C decimal.

(cherry picked from commit 711c80b)

Co-authored-by: Serhiy Storchaka storchaka@gmail.com
Co-authored-by: Tim Peters tim.peters@gmail.com

…mize int to str conversion (pythonGH-118483)

For converting large ints to strings, CPython invokes a function in _pylong.py,
which uses the decimal module to implement an asymptotically waaaaay
sub-quadratic algorithm. But if the C decimal module isn't available, CPython
uses _pydecimal.py instead. Which in turn frequently does str(int). If the int
is very large, _pylong ends up doing the work, which in turn asks decimal to do
"big" arithmetic, which in turn calls str(big_int), which in turn ... it can
become infinite mutual recursion.

This change introduces a different int->str function that doesn't use decimal.
It's asymptotically worse, "Karatsuba time" instead of quadratic time, so
still a huge improvement. _pylong switches to that when the C decimal isn't
available. It is also used for not too large integers (less than 450_000 bits),
where it is faster (up to 2 times for 30_000 bits) than the asymptotically
better implementation that uses the C decimal.

(cherry picked from commit 711c80b)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Tim Peters <tim.peters@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants

Comments