Skip to content

Improve performance of pidigits and int arithmetic #157742

Open

Description

@eendebakpt

Feature or enhancement

Proposal:

For obvious reasons pidigits is the most important benchmark we have for Python. We should to everything we can to improve performance, including rust (rust-for-python) or a jit (accept PEP 836).

Tagging release managers @hu g ovk@ s avann ahostrows ki to mark this a release blocker.

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:

No response

Linked PRs

Activity

  1. sergey-miryanov commented on Sep 18, 2026

    @sergey-miryanov
    Contributor
  2. hugovk commented on Sep 18, 2026

    @hugovk
    Member

    Tagging release managers @hu g ovk@ s avann ahostrows ki to mark this a release blocker.

    Erm, not sure if this is a joke, but making a benchmark 1.02x faster isn't a release blocker.

    And performance improvements usually aren't backported, so this is for 3.16/main only.

  3. gvanrossum commented on Sep 18, 2026

    @gvanrossum
    Member

    Joke.

  4. terryjreedy commented on Sep 18, 2026

    @terryjreedy
    Member

    The 3rd PR says 1.24x improvement in pydigits. The 2nd did not test pydigits; perhaps not relevant? A more prosaic issue title would be "speed-up int arithmetic".

  5. changed the title [-]Improve performance of pidigits[/-] [+]Improve performance of pidigits and int arithmetic[/+] on Sep 18, 2026
  6. gvanrossum commented on Sep 18, 2026

    @gvanrossum
    Member

    I can get behind making very long int arithmetic faster, although this matters only for a vanishingly small number of users. IIRC @tim-one has mentioned in the past that people who need the fastest huge integer math are (or should not be) using Python ints but GMP.

    The lead-in about pidigits being the most important benchmark still feels like a joke, and the OP looks like a cat ran over the keyboard at the end. :-)

  7. skirpichev commented on Sep 19, 2026

    @skirpichev
    Member

    although this matters only for a vanishingly small number of users.

    Not sure, assuming that integer arithmetics reinvented several times in the Python ecosystem (Sagemath, gmpy2, python-flint, python-gmp...). IMO, Sage, SymPy or mpmath (major consumers of this) are popular enough.

    people who need the fastest huge integer math are (or should not be) using Python ints but GMP.

    And suffer from severe speed loss for small integers, where builtin integers hard to beat for external extensions. IMO, things go worse with years: bytecode specialization and jit come into play...

    The problem is that practical integer-heavy applications (like e.g. Sagemath) usually use operands of different sizes. In this d.p.o thread that was demonstrated on example of the mpmath test suite.

  8. eendebakpt commented on Sep 19, 2026

    @eendebakpt
    ContributorAuthor

    The lead-in about pidigits being the most important benchmark still feels like a joke, and the OP looks like a cat ran over the keyboard at the end. :-)

    The importance of pidigits was a joke, but I do think making int arithmetic faster if possible is a good thing. The cat has knowledge of Ludolph's constant though ;-)

    For reviewers: just treat the PR's as serious attempts to make int arithmetic faster (pidigits being faster is just a consequence).

  9. skirpichev commented on Sep 20, 2026

    @skirpichev
    Member

    making int arithmetic faster if possible is a good thing.

    Optimization for small integers, perhaps. Not sure that optimization for big ints is ok: we never can beat here professional libraries, like GMP. At least, not at cost of lowering code readability: that's why your 2nd pr looks acceptable for me. (Another example is Karatsuba cutoff, probably it's value could be lowered nowadays.)

    I think it's better just use external library for doing big ints arithmetic.

  10. gvanrossum commented on Sep 20, 2026

    @gvanrossum
    Member

    I think it's better just use external library for doing big ints arithmetic.

    That was my point exactly, but I got the feeling I was overruled.

  11. skirpichev commented on Sep 20, 2026

    @skirpichev
    Member

    I think it's better just use external library for doing big ints arithmetic.

    That was my point exactly

    There is a difference. I meant: use external library in the CPython for this.

  12. gvanrossum commented on Sep 20, 2026

    @gvanrossum
    Member

    There is a difference. I meant: use external library in the CPython for this.

    Oh, sorry. I believe @tim-one once posted an explanation of why that's not an attractive option.

  13. skirpichev commented on Sep 20, 2026

    @skirpichev
    Member

    His objections were rather about license stuff. (I can't judge on this, but it's not a problem for Wolfram, distributing libgmp.so with the Wolfram Engine or Mathematica.)

    That can be workarounded e.g. by a stable API layer for big integers, as suggested by Mark Shannon. Then people could choose external big ints library by some ./configure switch.

    We already have similar layer of functions (x_add, x_mul and so on), but they are tightly coupled to the CPython. E.g. in using exceptions instead of return codes to handle errors, or in memory management. But the rest is same as for other big int libraries, using similar data structures (modulo less sophisticated algorithms).

  14. mdickinson commented on Sep 20, 2026

    @mdickinson
    Member

    His objections were rather about license stuff.

    I think this is only one half of the story. The other concern is that improvements to bigint arithmetic should not come at the expense of decreased performance on small integers. Small int performance is overwhelmingly more important than bigint performance.

    Relevant d.p.o. message from Tim: https://discuss.python.org/t/pylongobject-just-use-gmp/106086/42 (not the only relevant one; read forwards and backwards from that point).

  15. eendebakpt commented on Sep 20, 2026

    @eendebakpt
    ContributorAuthor

    The PRs here either (slightly) improve performance for small integers, or are neutral (e.g. in the noise). The gain is mostly in the area that is not small (e.g. not compact or 64-int), but not yet in the "bigint" sizes (e.g. sizes where algorithms are used with lower complexity).

    For reference: here a plot of the computation times of some int operations, together with the thresholds used for the bigint algorithms in cpython.

    Image
  16. skirpichev commented on Sep 21, 2026

    @skirpichev
    Member

    I think this is only one half of the story. The other concern is that improvements to bigint arithmetic should not come at the expense of decreased performance on small integers.

    Sure, but this concern shared by most (all?) core devs in the thread.

    I don't think it's relevant. We already have special code paths for small integers (something that wasn't here in previous attempts to use GMP), and I'm not suggesting to change that. I don't see how decreased performance for small integers could appear on this road. But of course, only a complete implementation could prove or disprove that.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions