Python 3.14 Goes Faster With Tail-Call Optimization
Written by Mike James   
Wednesday, 12 February 2025

Python 3.14, which should really be called Pi-thon, has seen its fifth alpha release. It introduces a new interpreter that can be as much 30% faster, depending on what you are doing.

pfsbanner

You might think that given the microscope that the CPython code is generally under that there was little scope for speed improvements. However, this particular improvement is more about what compilers can do for you:

"A new type of interpreter based on tail calls has been added to CPython. For certain newer compilers, this interpreter provides significantly better performance. Preliminary numbers on our machines suggest anywhere from -3% to 30% faster Python code, and a geometric mean of 9-15% faster on pyperformance depending on platform and architecture.

This interpreter currently only works with Clang 19 and newer on x86-64 and AArch64 architectures. However, we expect that a future release of GCC will support this as well."

The fact that the improvement seems to depend on Clang and that GCC will catch up should give you the clue that there is something odd going on. The improved speed is due to Clang 19 supporting tail call optimization. If you are wondering what improving recursion has to do with making Python faster then you are in the right ballpark, but not quite in the game. There is something special about the last subroutine or function to be called in a nest of calls. For example:

def funA:
 do things
 funB()
 do things
 return
def funB:
 do things
 funC()
 return

You can see here that funC is a tail call as nothing happens after it is called. The optimization is almost trivial - replace the call with a jump then when funC does its return it is equivalent to the return in funB. This is a simple and safe optimization because funC still has access to the local variables in funB and everything works as before.  What is surprising is that just a simple optimization has so much effect. It means that there is no need to create a new stack frame and it reduces the overall pressure on stack space. It also seems that tail calls are very common and eliminating them is worth doing.

At the moment only Clang supports the optimization and the more widely-used GCC doesn't yet support it..

 "This feature is opt-in for now. We highly recommend enabling profile-guided optimization with the new interpreter as it is the only configuration we have tested and can validate its improved performance. For further information on how to build Python, see --with-tail-call-interp."

I can't see that such a simple optimization has any scope for hiding problems, so it should be safe to try out.

python3

  • Mike James is the author of the Programmer's Python: Something Completely Different series of books which set out to show how Python diverges from other programming languages and how its special features deserve our attention. His more recent book, Extending & Embedding Python Using C, shows how combining C with Python leads to a speed increase of at least 10 times faster.  

More Information

https://www.python.org/downloads/release/python-3140a5/

Related Articles

Python 3.13 Is Here

Goodbye GIL - But Will It Make Python Faster?

Python 3.11 Goes Faster

Guido And Microsoft Want To Make Python x2 Faster

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

 

Banner


First Official SDK In Rust
05/03/2025

We are all being urged to work in Rust because its safer, but this is easier said than done. The embedded world really needs a memory-safe language, but Rust gets surprisingly little support from manu [ ... ]



Artists Opposed To Auction Of AI-Augmented Artworks
16/02/2025

This week Christie's is running its inaugural AI art auction. With the title Augmented Intelligence, it is billed as the first ever artificial intelligence-dedicated sale at a major auction  [ ... ]


More News

 

<ASIN:B0CK3X93KF>

<ASIN:1871962749>

<ASIN:1871962595>

<ASIN:1871962765>

 

 

Last Updated ( Wednesday, 12 February 2025 )