Time to JIT your Python with Pyjion?
Pyjion is a drop-in JIT compiler for Python 3.10. Pyjion uses the power of the .NET 6 cross-platform JIT compiler to optimize Python code on the fly, with NO changes to your source code required. It runs on Linux, macOS, and Windows, x64 and ARM64.
Episode Deep Dive
Guest Introduction and Background
Anthony Shaw is a seasoned Python developer, prolific contributor to open-source projects, and a Python Software Foundation Fellow. He has worked extensively on Python performance and internals, including the new JIT compiler project called Pyjion. Anthony is also well-known for his contributions to CPython and for creating numerous libraries and tools that blend Python with other ecosystems such as .NET and C++. He’s passionate about bridging technology gaps and sharing his deep curiosity with the broader Python community.
What to Know If You're New to Python
If this is one of your first deep-dives into Python, here are a few points to help you follow along:
- Python has multiple “implementations” beyond just the official CPython (e.g., PyPy, Jython, IronPython). Pyjion sits atop standard CPython but gives it JIT superpowers.
- You’ll hear references to “bytecode,” “C API,” and “machine code.” Just know these are lower-level details that make Python run.
- If you see or hear the term “PEP,” it stands for Python Enhancement Proposal. These are design documents that guide and shape Python’s development.
Key Points and Takeaways
Why JIT (Just-In-Time) Compilation in Python Matters Pyjion aims to address Python’s sometimes-slower runtime performance by compiling Python functions into native machine code on the fly. By skipping bytecode interpretation in tight loops or numeric-heavy tasks, this can greatly accelerate certain workloads.
- Links and Tools:
How Pyjion Works Under the Hood Pyjion uses .NET 6’s cross-platform JIT compiler as a backend, translating Python bytecode into .NET’s intermediate language, which is then turned into machine code. Thanks to PEP 523, Pyjion can “take over” code evaluation from CPython without needing a custom fork.
- Links and Tools:
Profile-Guided Optimizations for Speed Pyjion supports a “profile-guided” approach, where the first pass through a function profiles data types (e.g., ints, floats) and then recompiles it for faster execution. This means code that is run frequently (“hot code”) can get significant speedups over time.
- Links and Tools:
Impressive Gains in Numeric and Math-Heavy Code Many Python users who do numeric computations directly in Python (as opposed to calling out to libraries like NumPy) can see big boosts. Anthony mentioned micro-benchmarks showing up to a 20x speedup in certain pure-Python math loops, and a big lift on the “n-body” planetary simulation problem.
- Links and Tools:
Fallback to CPython for Unsupported Features Not every code path is fully optimized by Pyjion, especially certain
async
/await
features or specialized C extension modules. Instead of crashing, Pyjion gracefully hands unsupported code back to CPython’s normal interpreter loop. This makes it less risky to try on large existing codebases.Comparisons to Other Speedups: PyPy, Numba, and Cython While PyPy reimplements the entire Python interpreter and Numba focuses on accelerating NumPy-based workflows, Pyjion is a drop-in “extension” to stock CPython. It aims for broad compatibility, minimal code modifications, and integration with standard C APIs that tools like Cython also rely upon.
Working with Web Frameworks and WSGI Middleware Pyjion offers a WSGI middleware so you can enable JIT compilation in frameworks such as Flask or Django with minimal setup. While dynamic frameworks may not see the same dramatic speedups as math-bound code, the overhead of repeated function calls can still see improvements in certain endpoints.
- Tools:
Installation and Activation Getting started with Pyjion is straightforward on Python 3.10 or later:
- Install .NET 6 or higher
pip install pyjion
import pyjion; pyjion.enable()
Or use thepyjion
command instead ofpython
to launch scripts or modules.
- Links:
Type Specialization vs. Python’s Dynamic Nature Much of the performance speedup comes from “unboxing” integers, floats, and other small data types. When Python’s dynamic typing remains unpredictable, Pyjion may produce more general code. This dynamic vs. specialized tradeoff is a core challenge for any JIT in a dynamically typed language.
- Links and Tools:
Future Directions and .NET Collaboration Anthony has worked closely with the .NET compiler team, filing issues and collaborating to improve the JIT for scenarios like Python. This paves the way for deeper cross-ecosystem synergy, possibly letting Python devs reuse .NET’s tooling and speed without rewriting code.
- Links and Tools:
Interesting Quotes and Stories
- On Curiosity and Starting Pyjion: “I was one of those projects I got started on because it just had my curiosity. I didn’t understand how it could work and really wanted to learn.” — Anthony Shaw
- On Speeding Up ‘N-Body’: “We went from around 33% faster than CPython to more like 65%, and even 20 times faster for certain micro-benchmarks.” — Anthony Shaw
Key Definitions and Terms
- CPython: The default and most widely used implementation of the Python language, written in C.
- JIT Compiler: Just-In-Time compiler converts interpreted bytecode into machine code as the program runs, often with real-time optimizations.
- PEP 523: A Python Enhancement Proposal that allows customizing Python’s “ceval” loop, enabling alternative bytecode evaluators like Pyjion.
- Specializations: Optimizations where the JIT compiler narrows down code paths based on data types (e.g., int vs. float).
- Profile-Guided Optimization (PGO): A technique where runtime data about code usage (frequently used paths, variable types, etc.) feeds into a second or third compilation pass to make the code even faster.
Learning Resources
If you are new to Python or want to deepen your understanding, check out these courses and references:
- Python for Absolute Beginners: A thorough introduction to Python covering all the basics you’ll need before exploring advanced topics like JIT compilation.
- Python for .NET Developers: Ideal for .NET developers looking to leverage or integrate Python effectively, especially relevant if you’re interested in .NET-based JIT methods like Pyjion.
Overall Takeaway
Pyjion represents a major leap in bridging Python’s ease of use with highly optimized machine code. It aims to provide a near-seamless drop-in JIT solution compatible with most of your existing Python code, all while retaining the strengths of CPython and .NET. For data scientists, web developers, and Python enthusiasts at large, Pyjion signals the exciting possibility that you can keep writing idiomatic Python while still harnessing serious performance gains—no major rewrites or specialized frameworks required.
Links from the show
Pyjion: github.com
Restarting Pyjion Presentation: youtube.com
Hathi: SQL host scanner and dictionary attack tool: github.com
Try Pyjion online: trypyjion.com
Pyjion optimizations: readthedocs.io
Pyjion docs: readthedocs.io
.NET: dotnet.microsoft.com
PEP 523: python.org
Pydantic validation decorator: helpmanual.io
Tortoise ORM: github.com
pypy: pypy.org
Numba: numba.pydata.org
NGen AOT Compiler: microsoft.com
Watch this episode on YouTube: youtube.com
Episode transcripts: talkpython.fm
--- Stay in touch with us ---
Subscribe to Talk Python on YouTube: youtube.com
Talk Python on Bluesky: @talkpython.fm at bsky.app
Talk Python on Mastodon: talkpython
Michael on Bluesky: @mkennedy.codes at bsky.app
Michael on Mastodon: mkennedy