oleg-st / ZstdSharp

Port of zstd compression library to c#
MIT License
200 stars 29 forks source link

Question: Reuse of Compressor/Decompressor #35

Closed bill-poole closed 3 months ago

bill-poole commented 3 months ago

Firstly, I'd like to thank you for creating this excellent library.!

I'm doing compression/decompression in a high performance environment, and as such I want to reuse each Compressor/Decompressor object to compress/decompress many streams because there is an overhead with creating/initializing the Compressor/Decompressor objects. However, the library is throwing an exception saying the input Zstandard is corrupt if I reuse a single Decompressor object across multiple threads concurrently.

I couldn't find any documentation saying whether the Compressor and Decompressor objects are thread-safe. I assume based on the errors I'm getting that they are not. Is that correct? If so, do you recommend creating an instance of each per thread (using a [ThreadStatic] attribute?

Apologies if this is documented somewhere and I missed it.

oleg-st commented 3 months ago

It's just like the original zstd library, you need a different Compressor/Decompressor object for each thread. https://facebook.github.io/zstd/zstd_manual.html#Chapter4

bill-poole commented 3 months ago

Ah, excellent, thanks for pointing me to the relevant documentation.