pyo3: Most efficient way of returning large byte array from Rust to Python
I’m looking for some guidance on the best way to return a large byte array to Python code and/or share access to raw memory between Rust and Python.
Basically, my application transfers large amounts of data over the network in Rust and then hands the data over to Python (but in some cases I still want concurrent access to the data on the Rust side as well). One of my ideas is to allocate a numpy array and have the networking code write into it directly, though in that case I don’t think the Rust code can still safely access the array because it might be modified concurrently (related question, if I store a reference to a numpy array object on the Rust side, will GC work properly or could the numpy array get deallocated once all references on the Python side disappear?).
I think PyBytes::from_ptr might be what I want but I haven’t found any documentation on what invariants it expects to be maintained without triggering unsafety.
If I have a Vec<u8> on the Rust side and return direct access to it with PyBytes::from_ptr, I assume this is fine as long as I don’t deallocate the Vec and the Python code doesn’t do anything funky? Is there way to ever safely/automatically deallocate the Vec, or would that require something like exposing a free method that has to be called explicitly by the Python code after it stops accessing the bytes object?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 17 (11 by maintainers)
Judging by the example code you’ve written, the timing in rust is measured after the creation of the
PyBytesobject, so I don’t think that the latency you see is caused by returning the bytes.Some random stabs in the dark which might help you locate the issue:
--release?I’d like to chime in with my use case (and my admittedly much simpler understanding) here that is causing me grief:
I’m doing parallel processing in Rust and returning 5x ~150MiB
PyBytesto Python which then get sent over the wire to be aggregated elsewhere, at which point those chunks of memory are passed from Python to Rust. I’m seeing significant latency in passing the data back to Python. The Rust code looks roughly like:And I call it from Python:
These times vary by a significant margin: around 70 seconds of Rust time to 78 seconds of Python time, quite consistently. Passing 5x150mb from Python to Rust is instantaneous.
In my case, once I pass the memory from Rust to Python or vice-versa, I don’t need to maintain the original data. I’m surprised that there’s a ~7-8 sec delta between when Rust completes and Python receives the data. Is this something that would be addressed by
memoryview? Or is this more likely PEBCAK?I’ve figured out the remaining details and implemented
ArcVecwhich wraps anArc<Vec<u8>>and allows it to be used as a bytes-like object by Python. Majority of my implementation is just based off the existing buffer protocol test. I removed thedrop_calledfield since I don’t think it provides that much protection, if you use rogue C extensions that decide to hang on to pointers that have been freed by Python GC and Rust memory allocator then you’re probably have a bad time either way 😛Basic example/test case:
From what I understand this implementation should be sound, though I may well have missed something. When a C extension wants to obtain access to the memory buffer, it calls
PyObject_GetBuffer(bf_getbuffer) and we increment the reference count of our Python object which prevents it from getting garbage collected by Python and dropped by Rust. Once the C extension is done accessing the buffer, it callsPyBuffer_Releaseand Python automatically decreases the reference count of our object, allowing it to be garbage collected once all other outstanding buffers and references to the objects are returned. Alternatively, a C extension might return some kind of view that includes a reference to our object and which is garbage collected using the normal mechanisms.