Implementing complex type conversion between Python and Rust. #3144
-
Hi! I'm looking into PyO3 to speed up my Python project by rewriting its time-consuming part in Rust. My project deals with graphs (nodes and edges). What would be a general recommendation for building something with some complexity in conversion? Thank a lot. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This is probably not overly helpful, but it usually depends. Let's your rather expensive conversion takes seconds. If the algorithm you want to implement takes seconds itself that overhead might be prohibitive, but if it takes hours then those extra seconds will most likely not matter at all. So this is hard to answer without concrete numbers and experimentation. Alternative strategies would include wrapping your Rust-side types like Another option is to use a general purpose graph library like My personal experience would suggest to give the most straight-forward conversion operation a try and measure the results. With involved computations even heavy pre and post processing often works out in the end. If it does not, you can often try to gradually move a larger part of the algorithm into Rust to avoid more conversion overheads until you achieve the desired speed up. But hopefully well before you had to rewrite your whole application... |
Beta Was this translation helpful? Give feedback.
This is probably not overly helpful, but it usually depends. Let's your rather expensive conversion takes seconds. If the algorithm you want to implement takes seconds itself that overhead might be prohibitive, but if it takes hours then those extra seconds will most likely not matter at all. So this is hard to answer without concrete numbers and experimentation.
Alternative strategies would include wrapping your Rust-side types like
rs_graph::Digraph
in a custom#[pyclass]
so that there is no conversion. But this means that you will have to implement a Python API so that the rest of your Python code can use your custom#[pyclass]
instead ofnetworkx.DiGraph
which might be a lot of effort…