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: Integer operations are inefficient for "medium" integers.
Type: performance Stage: resolved
Components: Interpreter Core Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Mark.Shannon, mark.dickinson, vstinner, xuxinhang
Priority: normal Keywords: patch

Created on 2021-08-18 10:25 by Mark.Shannon, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 27832 closed Mark.Shannon, 2021-08-19 12:22
PR 30044 open xuxinhang, 2021-12-11 04:39
Messages (5)
msg399832 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-08-18 10:25
"Medium" integers are those with a single internal digit or zero.
Medium integers are integers in the range -2**30 to +2**30 on 64 bit machines.
"Small" integers, -5 to 256 are cached, but are represented as medium integers internally.

To a good approximation, all integers are "medium".

However, we make little effort to exploit that fact in the code for binary operations, which are very common operations on integers.
msg399833 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-08-18 10:41
We already special-case medium integers in the Objects/longobject.c code, in various places. For example for addition, here:

https://github.com/python/cpython/blob/3240bc62f4e0afa09964f3afc845697f0a0806b9/Objects/longobject.c#L3070-L3072

Are you proposing further changes in longobject.c, or some other mechanism?
msg399834 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-08-18 10:43
See also #21955, #10044, and https://github.com/python/cpython/blob/3240bc62f4e0afa09964f3afc845697f0a0806b9/Python/ceval.c#L1986-L1991
msg399838 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-08-18 11:59
Just changes to longobject.c.

There are still various minor inefficiencies in testing to see whether an int is a medium value, and then throwing away size information before creating result objects.

I'm not expecting this to make much difference, but every little helps.
msg400263 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-08-25 13:28
New changeset 15d50d7ed8afd3ab26b00210b5b4feaaadd5af51 by Mark Shannon in branch 'main':
bpo-44946: Streamline operators and creation of ints for common case of single 'digit'. (GH-27832)
https://github.com/python/cpython/commit/15d50d7ed8afd3ab26b00210b5b4feaaadd5af51
History
Date User Action Args
2022-04-11 14:59:48adminsetgithub: 89109
2021-12-11 04:39:53xuxinhangsetnosy: + xuxinhang

pull_requests: + pull_request28270
2021-08-26 15:09:37Mark.Shannonsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-08-25 13:28:47Mark.Shannonsetmessages: + msg400263
2021-08-19 12:22:06Mark.Shannonsetkeywords: + patch
stage: patch review
pull_requests: + pull_request26296
2021-08-18 11:59:10Mark.Shannonsetmessages: + msg399838
2021-08-18 10:44:15mark.dickinsonsetnosy: + vstinner
2021-08-18 10:43:21mark.dickinsonsetmessages: + msg399834
2021-08-18 10:41:40mark.dickinsonsetnosy: + mark.dickinson
messages: + msg399833
2021-08-18 10:25:32Mark.Shannoncreate