oleg-st / ZstdSharp

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

Request: Multi-threaded compression #30

Closed johnsanc314 closed 8 months ago

johnsanc314 commented 8 months ago

Unless I am completely overlooking something, I see no way to enable multi-threading.

Is there a way to enable this? If not, is it in the roadmap to support?

oleg-st commented 8 months ago

There is currently no way to enable multi-threading. This may be added in the near future

oleg-st commented 8 months ago

In the original ztd library this is achieved using the ZSTD_c_nbWorkers parameter in streaming compression, I can port this.

The code would be like this:

using var input = File.OpenRead("dickens");
using var output = File.OpenWrite("dickens.zst");
using var compressionStream = new CompressionStream(output, level);
compressionStream.SetParameter(ZSTD_cParameter.ZSTD_c_nbWorkers, 4);
input.CopyTo(compressionStream);

@johnsanc314 Is this what you need?

johnsanc314 commented 8 months ago

Yes! that's precisely what's needed. I noticed in the code its basically locked down to single thread everywhere, but not sure the implications of allowing multi-threading like you described.

If you can add that it would be a massive performance enhancement.

oleg-st commented 8 months ago

Added multi-threading support in master

Spacefish commented 8 months ago

Haha ran into that literally today, i tried to

compressor.SetParameter(ZstdSharp.Unsafe.ZSTD_cParameter.ZSTD_c_nbWorkers, Environment.ProcessorCount);

but got the: "Unsupported parameter"

thanks for adding it!

oleg-st commented 8 months ago

Released in 0.7.5

johnsanc314 commented 8 months ago

Amazing! Thank you for such quick turnaround. It was a long shot even asking, but I'm glad I did. Sounds like several others were wanting this feature as well.