gh-126703: add freelist for ints with small number of digits#129638
Open
eendebakpt wants to merge 8 commits intopython:mainfrom
Open
gh-126703: add freelist for ints with small number of digits#129638eendebakpt wants to merge 8 commits intopython:mainfrom
eendebakpt wants to merge 8 commits intopython:mainfrom
Conversation
eendebakpt
commented
Feb 4, 2025
| assert(is_medium_int(x)); | ||
|
|
||
| PyLongObject *v = (PyLongObject *)_Py_FREELIST_POP(PyLongObject, ints); | ||
| PyLongObject *v = (PyLongObject *)_Py_FREELIST_POP(PyLongObject, ints[1]); |
Member
There was a problem hiding this comment.
Why ints[1] is used here?
Maybe some comment on it will be useful?
Member
There was a problem hiding this comment.
Here we need only ndigits==1 integers.
New implementation uses several free lists (though, ints[0] isn't used).
Contributor
Author
There was a problem hiding this comment.
ints[0] is indeed not used. It takes a tiny bit of extra memory, but makes the code cleaner.
| _Py_FREELIST_FREE(ints, self, PyObject_Free); | ||
| if (PyLong_CheckExact(self)) { | ||
| if (!maybe_freelist_push(self)) { | ||
| PyObject_Free(self); |
Member
There was a problem hiding this comment.
Is it equivalent to run PyObject_Free(self) and to run Py_TYPE(self)->tp_free(self) for integers with PyLong_MAXSAVESIZE or more digits?
Contributor
Author
There was a problem hiding this comment.
It is for exact ints (but might not be for subclasses).
efimov-mikhail
approved these changes
Jun 15, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Benchmark:
The
bench_intis not affected since the compact ints already have a freelist in current main. Thebench_idis a microbenchmark that does minimal amount of work. Thebench_gcdandbench_uuid8are benchmarks that are representative for real code.Statistics for the number of allocations saved can be found in the corresponding issue.
Benchmark script: