Python 3.15’s interpreter for Windows x86-64 should hopefully be 15% faster

(fidget-spinner.github.io)

114 points | by lumpa 3 hours ago

10 comments

  • mananaysiempre 1 hour ago
    The money shot (wish this were included in the blog post):

      #   if defined(_MSC_VER) && !defined(__clang__)
      #      define Py_MUSTTAIL [[msvc::musttail]]
      #      define Py_PRESERVE_NONE_CC __preserve_none
      #   else
      #       define Py_MUSTTAIL __attribute__((musttail))
      #       define Py_PRESERVE_NONE_CC __attribute__((preserve_none))
      #   endif
    
    https://github.com/python/cpython/pull/143068/files#diff-45b...

    Apparently(?) this also needs to be attached to the function declarator and does not work as a function specifier: `static void *__preserve_none slowpath();` and not `__preserve_none static void *slowpath();` (unlike GCC attribute syntax, which tends to be fairly gung-ho about this sort of thing, sometimes with confusing results).

    Yay to getting undocumented MSVC features disclosed if Microsoft thinks you’re important enough :/

    • publicdebates 1 hour ago
      Important enough, or benefits them directly? I have no good guesses how improving Python's performance would benefit them, but I would guess that's the real reason.
      • andix 26 minutes ago
        I guess there are some Python workloads on Azure, Microsoft provides a lot of data analysis and LLM tools as a service (not paid by CPU minutes). Saving CPU cycles there directly translates to financial savings.
      • HPsquared 1 hour ago
        I wonder if this is related to Python in Excel. You'll have lots of people running numerical stuff written in Python, running on Microsoft servers.
      • mkoubaa 53 minutes ago
        A lot of commercial engineering and scientific software runs on windows.
    • monster_truck 40 minutes ago
      I don't understand what the scene in a porno when they cum has to do with python
  • jtrn 15 minutes ago
    Im a bit out of the loop with this, but hope its not like that time with python 3.14, when it was claimed a geometric mean speedup of about 9-15% over the standard interpreter when built with Clang 19. It turned out the results were inflated due to a bug in LLVM 19 that prevented proper "tail duplication" optimization in the baseline interpreter's dispatch loop. Actual gains was aprox 4%.

    Edit: Read through it and have come to the conclusion that the post is 100% OK and properly framed: He explicitly says his approach is to "sharing early and making a fool of myself," prioritizing transparency and rapid iteration over ironclad verification upfront.

    One could make an argument that he should have cross-compiler checks, independent audits, or delayed announcements until results are bulletproof across all platforms. But given that he is 100% transparent with his thinking and how he works, it's all good in the hood.

  • redox99 1 hour ago
    This seems like very low hanging fruit. How is the core loop not already hyper optimized?

    I'd have expected it to be hand rolled assembly for the major ISAs, with a C backup for less common ones.

    How much energy has been wasted worldwide because of a relatively unoptimized interpreter?

    • kccqzy 1 hour ago
      Python’s goal is never really to be fast. If that were its goal, it would’ve had a JIT long ago instead of toying with optimizing the interpreter. Guido prioritized code simplicity over speed. A lot of speed improvements including the JIT (PEP 744 – JIT Compilation) came about after he stepped down.
    • LtWorf 23 minutes ago
      If you want fast just use pypy and forget about cpython.
  • g947o 2 hours ago
    > This has caused many issues for compilers in the past, too many to list in fact. I have a EuroPython 2025 talk about this.

    Looks like it refers to this:

    https://youtu.be/pUj32SF94Zw

    (wish it's a link in the article)

  • Hendrikto 1 hour ago
    TLDR: The tail-calling interpreter is slightly faster than computed goto.

    > I used to believe the the tailcalling interpreters get their speedup from better register use. While I still believe that now, I suspect that is not the main reason for speedups in CPython.

    > My main guess now is that tail calling resets compiler heuristics to sane levels, so that compilers can do their jobs.

    > Let me show an example, at the time of writing, CPython 3.15’s interpreter loop is around 12k lines of C code. That’s 12k lines in a single function for the switch-case and computed goto interpreter.

    > […] In short, this overly large function breaks a lot of compiler heuristics.

    > One of the most beneficial optimisations is inlining. In the past, we’ve found that compilers sometimes straight up refuse to inline even the simplest of functions in that 12k loc eval loop.

  • bgwalter 20 minutes ago
    MSVC mostly generates slower code than gcc/clang, so maybe this trick reduces the gap.
    • metaltyphoon 18 minutes ago
      Is this backed by real evidence?
      • bluecalm 5 minutes ago
        My experience is 10%-15% slower than GCC. That was 10 years ago though.
  • develatio 1 hour ago
    if the author of this blog reads this: can we can an RSS, please?
    • kenjin4096 1 hour ago
      Got it. I'll try to set one up this weekend.
  • Rakshath_1 40 minutes ago
    [dead]
  • mishrapravin441 1 hour ago
    [flagged]
    • kenjin4096 1 hour ago
      Thanks for reading! For now, we maintain all 3 of the interpreters in CPython. We don't plan to remove the other interpreters anytime soon, probably never. If MSVC breaks the tail calling interpreter, we'll just go back to building and distributing the switch-case interpreter. Windows binaries will be slower again, but such is life :(.

      Also the interpreter loop's dispatch is autogenerated and can be selected via configure flags. So there's almost no additional maintenance overhead. The main burden is the MSVC-specific changes we needed to get this working (amounting to a few hundred lines of code).

      > Impact on debugging/profiling

      I don't think there should be any, at least for Windows. Though I can't say for certain.

      • mishrapravin441 1 hour ago
        That makes sense, thanks for the detailed clarification. Having the switch-case interpreter as a fallback and keeping the dispatch autogenerated definitely reduces the long-term risk.
    • pxeger1 45 minutes ago
      Profile of llm generated comments
  • machinationu 2 hours ago
    The Python interpreter core loop sounds like the perfect problem for AlphaEvolve. Or it's open source equivalent OpenEvolve if DeepMind doesn't want to speed up Python for the competition.