Skip to content

Increase int digit count available in freelists #162150

Open

Description

@cmaloney

Feature or enhancement

Background

Python's int (PyLongObject) uses 30-bit "digits" to store data. For efficiency int stores its digits inline and uses freelists to avoid allocator round trips for short lived objects. int with a single digit are called "compact" and are the only case that gets a freelist currently.

Proposal 1: Use existing 64 bit padding to let 2-digit ints use freelist

Because of struct padding two digits of space are already available on 64 bit platforms. Make it so two digit int on 64-bit can use the freelist objects.

Performance: In a local test branch this makes crypto_pyaes pyperformance benchmark ~10% faster.

Proposal 2: A second freelist for 64 bit integers

64 bit integers require three digits of space. This means they currently miss the freelist. They are commonly returned by hash(str) (usually 61-63 bits) as well as showing up in APIs using nanoseconds (ex. time.time_ns() 61 bits).

Growing PyLongObject to guarantee three digits (90 bits) of space unfortunately would bump it to the next memory allocator size class.

Add a single additional freelist which can hold up to the platform specific pointer size (ex. 3 digit for 64bit) and use that.

Note this isn't a freelist for arbitrary length ints which has been proposed previously. Just keeping a second freelist which can fit pointer sized ints.

cc: @eendebakpt who added the int freelist

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

Linked PRs

Activity

  1. self-assigned this
    on Nov 17, 2026
  2. added
    type-feature
    performance
    interpreter-core
    on Nov 17, 2026
  3. eendebakpt commented on Nov 17, 2026

    @eendebakpt
    Contributor

    There is a PR adding freelists for more int sizes: #129638. Performance on microbenchmarks is ok, although I suspect it will be less in the free-threading build.

    There is also #158002 which might help, as it ensures that in more cases the returned int object will match the allocated size. (which will balance the freelists over different sizes)

    The 64-bit integers are most important here (besides the compact ones), although depending on the implementation it might be easy to add freelists for a bit larger classes even.

  4. cmaloney commented on Nov 17, 2026

    @cmaloney
    ContributorAuthor

    It would be nice to have a "if thing frequently constructed at this size" -> "here's a fast as freelist" automatically (which would remove all this special casing). In theory the memory allocator does that a bit but freelist definitely saves time over it.

    For gh-129638: It's an interesting approach, thinking through the tradeoffs of one freelist with varying size objects in it... Doing one freelist which can do up to 2 digits (the current minimum PyLongObject layout), and a "always 4 digits" space (free threaded + GIL, the next allocation size; GIL could do more but one size seems good enough) feels like it would reduce risks a little. Small ints getting much larger allocations sometimes worries me. Particularly for how perf will go through all the "is compact" cases.

  5. cmaloney commented on Nov 17, 2026

    @cmaloney
    ContributorAuthor

    experimental commit for Proposal 1: cmaloney@87ea9e2

  6. eendebakpt commented on Nov 17, 2026

    @eendebakpt
    Contributor

    experimental commit for Proposal 1: cmaloney@87ea9e2

    Is the long_fits_freelist a runtime calculation or can the compiler optimize it away? It would be nice if we can have a constant MAX_DIGITS_FOR_FREELIST and make the check just ndigits <= MAX_DIGITS_FOR_FREELIST.

  7. cmaloney commented on Nov 17, 2026

    @cmaloney
    ContributorAuthor

    Yep, compiler optimizes away. Moving to MAX_DIGITS_FOR_FREELIST optimizes away even better as then the constant is fully isolated. gh-162173

  8. eendebakpt commented on Nov 22, 2026

    @eendebakpt
    Contributor

    The approach from #162150 is clever. By reusing the same allocation size for bith 1- and 2-digit ints we can extend the range of freelists without any computational overhead. However, we only get the 2-digit case (which includes 32-bit ints, improving crypto_peas benchmark). The 64-bit ints are out of range (which for example like id(obj).

    An approach extending to more digits is #129638. There freelists are used upto 5 digits, although I believe the use for that is limited.

    I asked Claude to implement a combination (basically Proposal 2 from the OP), the result is main...eendebakpt:cpython:int_freelist_size_class

    For all approach we should carefully measure performance on the compact ints (e.g. 1-digit), or look at the generated code. If all are similar, I might prefer the Proposal 2.

  9. cmaloney commented on Nov 22, 2026

    @cmaloney
    ContributorAuthor

    I'm definitely 👍 on doing two freelists. I don't have benchmarks that show exactly how much it helps but given the memory allocation buckets I think it's likely to be good. Some comments in how to structure implementation; I do think the two steps at once is simpler code wise than the two independent steps, just more moving infra which I'm only starting to learn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

interpreter-coreperformancetype-feature

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions