Repository navigation
Description
After making pull request #301 (Performance enhancement by skipping the (unnecessary) checks in ArraySegment) I started to wonder if it was possible to make the CRC calculation even faster. Since CRC calculation speed has a big influence on overall unzipping speed (and probably zipping too) this can potentially greatly improve the performance of SharpZipLib.
Sure enough, I found an algorithm that is significantly faster than the algorithm currently in use in SharpZipLib. Total unzipping time dropped by approx. 33% after I used this algorithm in SharpZipLib. The algorithm itself can be found here: https://github.com/force-net/Crc32.NET/blob/develop/Crc32.NET/SafeProxy.cs
Please note that the CRC lookup table is generated during the first call, so the first call is slightly slower (although I can hardly measure any difference on an 8k zipped file)
I would like to make a pull request to incorporate this faster algorithm in SharpZipLib. But I'm not sure how best to incorporate this since SharpZipLib at the moment does not have a lot of external dependencies. I can either
- Reference CRC32.net through nuget (no more maintenance of the CRC code within SharpZipLib, but an extra external dependency and less flexibility)
- Take only the code we need and incorporate that in SharpZipLib (Possible maintenance in the future, but it would add flexibility to i.e. pre-calculate the CRC lookup table and speed up the first call like it is done now in SharpZipLib)
- Internalize the dependency on CRC32.net. At the moment I'm not sure how to go about it though.
- ..? Maybe there's some other preferred way?
Can you please advise?
Activity
[-]CRC-check can be improved[/-][+]CRC calculation can be improved[/+]Numpsy commentedon Jan 20, 2019
Sounds nice, and it does seem a shame to add an extra external dependency for what looks like a small amount of code.
Would it be possible and/or reasonable to 'internalize' it by adding it as a submodule and then including the source file for the internal class in the SZL project? maybe overkill compared ot just copying one or two files though.
piksel commentedon Jan 21, 2019
We could use it as a paket dependency. One of the "core" directives I have for this repo is to not add any consumer dependencies.
Preferably we should do this without polluting the namespace (internal etc.)
Numpsy commentedon Jan 24, 2019
On a related note, the Crc32.net issue @ force-net/Crc32.NET#10 refers to a CRC32 implementation using intrinsics for extra performance.
Sounce interesting, though the Spreads repo is MPL licenced so not as nice as the Crc32.NET licence.
decipherer commentedon Jan 28, 2019
That intrinsics implementation looks to be very fast indeed! A pity that the license does not match.
I have not worked with paket before, but it seems it's possible to add a reference to a specific GitHub file,, so a reference to SafeProxy.cs could be added this way. Since this class is internal it would meet that requirement also.
It seems to be somewhat overkill to introduce paket for just referencing a single file though. Wouldn't it be easier to just copy the one file into SharpZipLib?
Numpsy commentedon Jan 28, 2019
Sounds the simplest idea if it is just one file - not something that would be complicated to update if a new version were picked up later.
piksel commentedon Jan 28, 2019
Yes, perhaps it's a bit overkill. We could just add the source repo and commit hash to the header as a mean of upstream reference.
decipherer commentedon Jan 29, 2019
Ok, I will create a pull request for further discussion based on copying the CRC32.net code whilst referencing the original code in the header.
Thanks for all the input thus far!
decipherer commentedon Feb 7, 2019
I created a pull request for this: #318
It might be a bit different than expected, as upon implementation I found out that CRC32 is also used for Bzip2. This is a different form, so it was not supported by CRC32.NET directly. Therefore I made some changes to the code I pulled from that repository. The credits are still there though. Also credits go out to https://github.com/Michaelangel007/crc32 for accurately describing the different CRC32 forms.
Some remarks:
Please let me know what you think.
Numpsy commentedon Feb 7, 2019
I don't know much about the implementation details of the CRC bits, but fwiw, I tried testing the change against a simple unit test that I previously wrote to test creating a Zip64 file with several gigabytes of zeros, and:
Creating the file took 1 min 42 seconds using the master branch and 1 min 20 seconds with this change.
Opening the file and calling TestArchive on it takes 50 seconds with the master branch and 25 seconds with this change.
So, a pretty good speed up.
decipherer commentedon Feb 8, 2019
Thanks for testing! I'm no CRC expert by any means either, but the writeup from Michealangel007 is pretty good, with some nice code examples.
I made another pull request (#319) for this, which is basically the same change, but using subclassing instead of Func<>'s. I feel that some people probably might like this better.
Numpsy commentedon Feb 24, 2019
fwiw, added a couple of BenchmarkDotNet benchmarks comparing this PR to the current code @ #328
piksel commentedon Oct 3, 2020
I am currently running benchmarks on #319 and #516 using
piksel commentedon Oct 3, 2020
Current implementation (master)
piksel commentedon Oct 3, 2020
PR #319
piksel commentedon Oct 3, 2020
PR #516
decipherer commentedon Oct 3, 2020
Look good, both pull requests have a significant performance enhancement 👍
piksel commentedon Oct 3, 2020
@decipherer Yeah, the difference between them is not really significant. Either would be a great improvement in performance.
piksel commentedon Nov 22, 2020
The CRC32 saga has been concluded as of v1.3.1 which included #516.
It was chosen because I could actually be confident in the changes it made.
Sorry for taking this long and not using your PRs @decipherer and @dpethes. I got a little bit burned by #202 and shied away from CRC changes after that.
Thanks for your contributions!