This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: optimizing `1 << n` or `2 ** n` and modulo-only operations
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: tim.peters Nosy List: Crowthebird, Dennis Sweeney, kj, lukasz.langa, mark.dickinson, rhettinger, stutzbach, tim.peters
Priority: normal Keywords: patch

Created on 2022-01-17 06:46 by Crowthebird, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30628 closed Crowthebird, 2022-01-17 07:21
PR 30653 merged Crowthebird, 2022-01-17 23:09
PR 31025 merged kj, 2022-01-30 16:07
Messages (8)
msg410744 - (view) Author: Jeremiah Gabriel Pascual (Crowthebird) * Date: 2022-01-17 06:46
Optimize calculating powers of 2 for integers. Does not include optimizing modular exponentiation because benchmarking shows current version of modular exponentiation is faster. Also optimizes any call with the structure `l_divmod(a, b, NULL, &mod)`.

> python_modified.exe -m timeit -s "x = 2" "x**10000000"
10000 loops, best of 5: 33.1 usec per loop
> python_current.exe -m timeit -s "x = 2" "x**10000000"
10 loops, best of 5: 35.2 msec per loop
msg410746 - (view) Author: Jeremiah Gabriel Pascual (Crowthebird) * Date: 2022-01-17 07:09
Note that `n` should not be over PY_SSIZE_T_MAX, else an error should occur.
msg410749 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2022-01-17 09:11
Another option to consider would be a table lookup of a pre-computed table of [1 << i for i in range(64)].
msg410765 - (view) Author: Jeremiah Gabriel Pascual (Crowthebird) * Date: 2022-01-17 12:11
> Another option to consider would be a table lookup of a pre-computed table of [1 << i for i in range(64)].
Does it have a significantly better performance compared to not having a table lookup?
msg410768 - (view) Author: Jeremiah Gabriel Pascual (Crowthebird) * Date: 2022-01-17 12:27
Also how would it be implemented? in terms of a PyLongObject or just a uint64_t?
msg411949 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2022-01-28 00:46
New changeset f10dafc430279b4e6cf5b981ae3d1d76e8f431ad by Crowthebird in branch 'main':
bpo-46407: Optimizing some modulo operations (GH-30653)
https://github.com/python/cpython/commit/f10dafc430279b4e6cf5b981ae3d1d76e8f431ad
msg411951 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2022-01-28 00:48
I only merged the split-off PR that added new remainder-only functions. Still thinking about the `1 << n` and `2**n` one.
msg412195 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2022-01-31 10:41
New changeset 768569325abc0a9cd5aae65c531889ec390847aa by Ken Jin in branch 'main':
bpo-46407: Fix long_mod refleak (GH-31025)
https://github.com/python/cpython/commit/768569325abc0a9cd5aae65c531889ec390847aa
History
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90565
2022-01-31 10:41:50lukasz.langasetnosy: + lukasz.langa
messages: + msg412195
2022-01-30 16:07:48kjsetnosy: + kj

pull_requests: + pull_request29206
2022-01-28 00:48:57tim.peterssetstatus: open -> closed
messages: + msg411951

assignee: tim.peters
resolution: fixed
stage: patch review -> resolved
2022-01-28 00:46:48tim.peterssetmessages: + msg411949
2022-01-17 23:09:41Crowthebirdsetpull_requests: + pull_request28854
2022-01-17 12:27:47Crowthebirdsetmessages: + msg410768
2022-01-17 12:11:25Crowthebirdsetmessages: + msg410765
2022-01-17 09:11:56Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg410749
2022-01-17 08:33:44serhiy.storchakasetnosy: + tim.peters, rhettinger, mark.dickinson, stutzbach
2022-01-17 07:22:17Crowthebirdsettype: performance
2022-01-17 07:21:54Crowthebirdsetkeywords: + patch
stage: patch review
pull_requests: + pull_request28831
2022-01-17 07:09:08Crowthebirdsetmessages: + msg410746
2022-01-17 06:46:43Crowthebirdcreate