-
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rust file-like objects and accept bytes/bytearray/numpy #45
Conversation
Fascinating! I wonder if it's possible to derive from IOBase, maybe add the class at runtime, since some functions will check |
That was a fleeting thought, make it fully compatible; slightly larger scope than what cramjam would need, but so close to being compatible it might as well be. 🤷♂️ Looking at Then making it a subclass of |
I'm not suggesting you need to implement the whole of IOBase - most applications will only call read (and read_into!); but seek/tell is nice. Adding it as a subclass would be nice, but only if its easy. After all, one could always wrap the thing in a python |
* Enable pyo3 extension-module feature * Use a proper manylinux2014 arm cross compilation Docker image * Disable mimallocator feature for aarch64 and armv7l mimallocator requires a C11 compiler
@martindurant This was ran using this benchmark routine equivelent from cramjam import snappy, Buffer
data = b"oh what a beautiful morning, oh what a beautiful day!!" * 1000000
compressed = Buffer()
decompressed = Buffer()
snappy.compress_raw_into(data, compressed)
compressed.seek(0)
snappy.decompress_raw_into(compressed, decompressed) Hopefully that sort of workflow works for you, should you choose to use these types. I think the other main benefit of the PR will be that any variant can take any combination of ie cramjam.snappy.compress_raw(<<numpy array>>) # gives back numpy array in return (returning the type it got)
cramjam.snappy.compress_raw_into(<<numpy array>>, <<cramjam.Buffer>>) # mixing input/output types are also ok The mixing types is also okay, even when the output is >>> from cramjam import snappy
>>> out = b'000000000000000000'
>>> snappy.compress_raw_into(b'bytes', out)
7
>>> out
b'\x05\x10bytes00000000000'' Anyway, I'm going to add some docs and tighten a few bolts and nuts, but it's basically done. Feel free to make any suggestions in the meantime. Thanks. |
I think it would be cool to have file-like objects from the Rust side. This could allow side-stepping certain allocations to/from Python by allowing Rust to hold all the bytes.
Most notably, compressing one file into another should be quite performant.
ie.
and decompressing right into a buffer on the rust side:
de/compress_into
to accept numpy array or these native typesAdd some benchmarks for these typesNot worth it, more convenience than drastically better performance.Probably make some commonFileLike
trait following IOBase interface#[pymethods]
not supported for trait impls