-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-126703: add freelist for ints with small number of digits #129638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@@ -248,7 +263,7 @@ _PyLong_FromMedium(sdigit x) | |||
assert(!IS_SMALL_INT(x)); | |||
assert(is_medium_int(x)); | |||
|
|||
PyLongObject *v = (PyLongObject *)_Py_FREELIST_POP(PyLongObject, ints); | |||
PyLongObject *v = (PyLongObject *)_Py_FREELIST_POP(PyLongObject, ints[1]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why ints[1]
is used here?
Maybe some comment on it will be useful?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we need only ndigits==1 integers.
New implementation uses several free lists (though, ints[0]
isn't used).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is for exact ints (but might not be for subclasses).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for the answers.
Benchmark:
The
bench_int
is not affected since the compact ints already have a freelist in current main. Thebench_id
is a microbenchmark that does minimal amount of work. Thebench_gcd
andbench_uuid8
are benchmarks that are representative for real code.Statistics for the number of allocations saved can be found in the corresponding issue.
Benchmark script: