Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreaded compression in C#

Is there a library in .net that does multithreaded compression of a stream? I'm thinking of something like the built in System.IO.GZipStream, but using multiple threads to perform the work (and thereby utilizing all the cpu cores).

I know that, for example 7-zip compresses using multiple threads, but the C# SDK that they've released doesn't seem to do that.

like image 562
Gareth Avatar asked Jul 31 '09 07:07

Gareth


People also ask

What is multi threading in C?

Multithreading is a model of program execution that allows for multiple threads to be created within a process, executing independently but concurrently sharing process resources. Depending on the hardware, threads can run fully parallel if they are distributed to their own CPU core.

Does C allow multi threading?

C does not contain any built-in support for multithreaded applications. Instead, it relies entirely upon the operating system to provide this feature. This tutorial assumes that you are working on Linux OS and we are going to write multi-threaded C program using POSIX.

Is gzip multithreaded?

There is an implementation of gzip that is multithreaded, pigz. Since it is compressing one file on multiple threads, it should be able to read from disk more efficiently, compared to compressing multiple files at once.

Is LZ4 multithreaded?

LZ4 is extremely fast (400MB/s per CPU core) compression algorithm by Yann Collet, with average compression ration at around 60%. Compressed stream follows LZ4 frame format (and thus can be decompressed by commandline routines provided by LZ4 author, Yann Collet). Activates multithreaded mode.


1 Answers

I think your best bet is to split the data stream at equal intervals yourself, and launch threads to compress each part separately in parallel, if using non-parallelized algorithms. (After which a single thread concatenates them into a single stream (you can make a stream class that continues reading from the next stream when the current one ends)).

You may wish to take a look at SharpZipLib which is somewhat better than the intrinsic compression streams in .NET.

EDIT: You will need a header to tell where each new stream begins, of course. :)

like image 150
Cecil Has a Name Avatar answered Sep 19 '22 19:09

Cecil Has a Name