While Python is widely used in fields such as AI and data analysis and offers the advantage of fast coding, it often causes some performance issues because Python is an interpreter language.
Of course, there are various alternatives to address Python's speed issues. In some cases, Python code is used only for initial prototypes, and the development is re-evaluated in Rust or C/C++ upon transitioning to production.
However, I'm experimentally developing this tool called Rextio as a new alternative to solve speed problems without abandoning Python.
Rextio improves performance by automatically converting parts of Python hot paths that can be compiled natively (such as type-hinted static code) into Rust code and compiling them into binaries. It then falls back to the rest of the code as is and executes it using CPython. This allows for the complete preservation of the original Python logic while aiming to improve execution speed for specific parts.
Roughly speaking, it is as follows:
``` typed Python project
-> analyze supported native candidates
-> reject unsafe or unsupported functions
-> generate Rust + PyO3 for accepted functions
-> generate Python fallback wrappers for the rest
-> build import-compatible artifacts ```
Please refer to the documentations in the Git repository for details.
Performance improvements are also confirmed when comparing execution with the native binary converted to Rextio versus execution with the original Python code only using CPython.
* When running basic Python code converted to native: 57.73×
* When running NumPy converted to native: 2.52×
* When running NetworkX' Dijkstra algorithm converted to native: 3.68×
* When running `pandas`' `Series.map` converted to native: 66.14×
* When running PyTorch deep MLP converted to native: 1.02× (Speed improvement limited due to using C extensions in existing Python packages)
* When running TensorFlow eager chain converted to native: 1.04× (Speed improvement limited due to using C extensions in existing Python packages)
Please refer to the [`rextio-benchmark` Git repository](https://github.com/rextio/rextio-benchmark) for the source code and execution environment information used in the benchmarks that yielded the above results.
Rextio has a Core CLI that converts and compiles Python code, and we are developing additional plugins that define rules for converting individual Python packages into Rust code to be attached to it. We have currently developed some plugins for NumPy and others (`rextio-numpy`, `rextio-networkx`, `rextio-pandas`, `rextio-torch`, `rextio-tensorflow`, etc.), and the benchmark results above were obtained using these plugins.
Rextio is still an early-stage project with much room for improvement, and compiling Python code into Rust-based native code does not guarantee performance gains in all situations.
For instance, if there is frequent data exchange between natively compiled parts and CPython fallbacks, data conversions crossing the boundaries can actually lead to a decrease in speed. However, in such cases, Rextio tries to fall back the entire process to CPython without passing through native code to prevent this degradation.
If you have any questions or feedback, I'll do my best to answer them and incorporate them into feature improvements.
Thank you.