Using Rust to corrode insane Python run-times
Vortexa improved a Python task processing GPS signals from 30 hours to 6 hours by developing a custom Rust library, achieving a 24x speed increase while maintaining existing business logic.
Read original articleVortexa faced significant performance issues with a Python task that took 30 hours to process GPS signals from ships, impacting their QA feedback cycle. The task involved filtering these signals through multiple polygons, and profiling revealed that a substantial portion of the runtime was spent in matplotlib for point-in-polygon calculations. The initial attempt to optimize using geopandas resulted in a tenfold increase in runtime, prompting a reevaluation of the approach. The team decided to develop a custom library in Rust, leveraging PyO3 for integration with Python. This new solution involved creating a Python class in Rust that processed geojson strings and numpy arrays, significantly reducing the number of library calls and utilizing integer-based math for efficiency. The results were impressive; the Rust implementation reduced processing time from 29.8 seconds to just 2.9 seconds, achieving a 24x improvement over the original matplotlib approach. The overall task duration decreased from 30 hours to 6 hours, enhancing the speed of model updates and QA processes. While the introduction of Rust added complexity to the codebase, the targeted optimization proved beneficial, maintaining the integrity of the existing business logic. Vortexa continues to seek talent for roles in Python, data science, Java, and Rust, reflecting their commitment to tackling ongoing technical challenges.
Related
Spending too much time optimizing for loops
Researcher Octave Larose shared insights on optimizing Rust interpreters, focusing on improving performance for the SOM language. By enhancing loop handling and addressing challenges, significant speedups were achieved, balancing code elegance with efficiency.
Announcing Polars 1.0 (Blog Post)
Polars releases Python version 1.0 after 4 years, gaining popularity with 27.5K GitHub stars and 7M monthly downloads. Plans include improving performance, GPU acceleration, Polars Cloud, and new features.
Spending too much time optimizing for loops
Researcher Octave Larose discussed optimizing Rust interpreters, focusing on improving performance for the SOM language. They highlighted enhancing loop efficiency through bytecode and primitives, addressing challenges like Rust limitations and complex designs. Despite performance gains, trade-offs between efficiency and code elegance persist.
Photoroom (YC S20) Is Hiring Rust Developers in Paris (X-Platform, Wgpu, WASM)
Photoroom, a Paris-based AI photo editor, seeks a Senior Rust Engineer to enhance cross-platform libraries for Android, iOS, and Web apps. Remote work in Europe with occasional Paris visits. Role involves Rust, WebAssembly, WebGPU development impacting millions. Ideal candidate has 3+ years Rust experience, C/C++ familiarity, and Swift/Kotlin knowledge. Bonus for OpenGL, Metal, WebGPU, WebAssembly, image editing experience. Hiring process includes interviews and home assignment review. Photoroom prioritizes diversity, equity, inclusion, flexible hours, and supportive environment.
Oxidize – Notes on moving Harfbuzz and Freetype tools and libraries to Rust
The "oxidize" project on GitHub aims to migrate tasks from Python & C++ to Rust, such as shaping, rasterization, font compilation, and manipulation. It outlines objectives, priorities, and references. For more details, inquire further.
- Many commenters suggest that spatial indexing techniques could significantly improve performance beyond the Rust implementation.
- There is skepticism about the claimed speedup, with some noting that comparisons should include existing C implementations and multi-threading capabilities.
- Alternative solutions, such as using Numpy, PyPy, or even Julia, are proposed as potentially more efficient options.
- Several users express a desire for more technical details or code examples to better understand the performance improvements.
- There is a general consensus that the optimization landscape is broader than just switching to Rust, with various tools and methods available for performance enhancement.
An interesting thing they didn't mention is that Matplotlib's point-in-path code is actually already in C. So this isn't really a case of Rust being X times faster than Python, it's X times faster than some other C algorithm. That's probably why X is only ~4 (they don't actually give a single-thread comparison), instead of ~50.
https://github.com/matplotlib/matplotlib/blob/cb487f3c077c93...
I expect the Rust code is faster because that code is waaaaay more complicated than what they probably need (https://stackoverflow.com/q/11716268/265521) - e.g. it handles stroke widths.
IMO this result is not very interesting.
> 1. Try breaking the data into chunks, and then using multi-processing (ugly in Python) to leverage a more powerful cloud virtual machine, sticking with matplotlib
> 2. Write a very small native custom library to do the math we want, using threads
There are a many more ways you could try to go about this, off the top of my head: numba, pypy (or other alternative python), jax/torch/tensorflow, multiprocessing, joblib.
On the other hand multiple dispatch and the way imports are handled left me quite confused as a beginner.
But it's powerful and also very fast, especially for plots.
My immediate reaction was to try just annotating ctypes and see what performance delta exists
Several options are available with JIT and AOT toolchains, alongside REPL programming environments.
Related
Spending too much time optimizing for loops
Researcher Octave Larose shared insights on optimizing Rust interpreters, focusing on improving performance for the SOM language. By enhancing loop handling and addressing challenges, significant speedups were achieved, balancing code elegance with efficiency.
Announcing Polars 1.0 (Blog Post)
Polars releases Python version 1.0 after 4 years, gaining popularity with 27.5K GitHub stars and 7M monthly downloads. Plans include improving performance, GPU acceleration, Polars Cloud, and new features.
Spending too much time optimizing for loops
Researcher Octave Larose discussed optimizing Rust interpreters, focusing on improving performance for the SOM language. They highlighted enhancing loop efficiency through bytecode and primitives, addressing challenges like Rust limitations and complex designs. Despite performance gains, trade-offs between efficiency and code elegance persist.
Photoroom (YC S20) Is Hiring Rust Developers in Paris (X-Platform, Wgpu, WASM)
Photoroom, a Paris-based AI photo editor, seeks a Senior Rust Engineer to enhance cross-platform libraries for Android, iOS, and Web apps. Remote work in Europe with occasional Paris visits. Role involves Rust, WebAssembly, WebGPU development impacting millions. Ideal candidate has 3+ years Rust experience, C/C++ familiarity, and Swift/Kotlin knowledge. Bonus for OpenGL, Metal, WebGPU, WebAssembly, image editing experience. Hiring process includes interviews and home assignment review. Photoroom prioritizes diversity, equity, inclusion, flexible hours, and supportive environment.
Oxidize – Notes on moving Harfbuzz and Freetype tools and libraries to Rust
The "oxidize" project on GitHub aims to migrate tasks from Python & C++ to Rust, such as shaping, rasterization, font compilation, and manipulation. It outlines objectives, priorities, and references. For more details, inquire further.