-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[performance] Improve speed of our CRC-64 hasher (#624)
Modify the `Crc64` implementation to use splicing in order to process data in a smaller number of iterations, at the expense of a bit more memory for the extra tables. A quick test of calculating checksum for a 1GB file shows a ~5.5x improvement: Allocating 1043332542 bytes to hold file data Reading file 'android-ndk-r21-linux-x86_64.zip' into memory Calculating checksum using the old method... Hash: 197913BA13B9A1CC; Time: 00:00:04.0649818 Calculating checksum using the new method... Hash: 8203D033719B6B96; Time: 00:00:00.7251845 The calculated hash is different (although still stable and correct) because the new code uses tables generated using a slightly different set of parameters. Instead of using a single lookup table, the new code takes the [slice-by-8 approach][0] which processes 8 bytes at a time, using 8 lookup tables instead of one. [Pycrc][1] was used to generate the `crc_update()` and `crc_reflect()` methods, which were then ported to C# in [`gen-crc-tables.cs`][2], via: pycrc.py --model crc-64-jones --std=C99 --algorithm bbf --generate {h,c} --reflect-out=true Because the calculated hash now differs, we've changed the output prefix from `crc64` to `c64r2`, to make the change slightly more prominent and obvious. Preliminary integration into xamarin-android shows build improvements for a small Xamarin.Forms app, Debug configuration: * First build: Time Elapsed 00:00:36.74 (master) Time Elapsed 00:00:34.43 (crc64) * Incremental build (`.cs` file touched): Time Elapsed 00:00:08.68 (master) Time Elapsed 00:00:08.51 (crc64) And for `<GenerateJavaStubs/>` task, which outputs 9.5MB of data: * First build: 606 ms GenerateJavaStubs 1 calls (master) 570 ms GenerateJavaStubs 1 calls (crc64) * Incremental build: 702 ms GenerateJavaStubs 1 calls (master) 617 ms GenerateJavaStubs 1 calls (crc64) [0]: https://create.stephan-brumme.com/crc32/#slicing-by-8-overview [1]: https://pycrc.org/index.html [2]: https://gist.github.com/grendello/fdb968c795c310b389c611cb8c2fd7d5#file-gen-crc-tables-cs const ulong POLY = 0xad93d23594c935a9; static ulong[,] table = new ulong[8,256]; static ulong crc_reflect (ulong data, ulong data_len) { uint i; ulong ret; ret = data & 0x01; for (i = 1; i < data_len; i++) { data >>= 1; ret = (ret << 1) | (data & 0x01); } return ret; } static ulong crc_update (ulong crc, byte[] data) { int idx = 0; uint i; bool bit; byte c; int data_len = data.Length; while (data_len-- > 0) { c = data[idx]; for (i = 0x01; (i & 0xff) != 0; i <<= 1) { bit = (crc & 0x8000000000000000) != 0; if ((c & i) != 0) { bit = !bit; } crc <<= 1; if (bit) { crc ^= POLY; } } crc &= 0xffffffffffffffff; } crc &= 0xffffffffffffffff; return crc_reflect (crc, 64) ^ 0x0000000000000000; } static void GenerateTable () { ulong crc; byte[] b = { 0 }; for (int n = 0; n < 256; n++) { b[0] = (byte)n; table[0,n] = crc_update (0, b); } for (ulong n = 0; n < 256; n++) { crc = table[0,n]; for (ulong k = 1; k < 8; k++) { crc = table[0,crc & 0xff] ^ (crc >> 8); table[k,n] = crc; } } Console.WriteLine ("static readonly ulong[,] Table = {"); for (int i = 0; i < 8; i++) { Console.WriteLine ("\t{"); for (int j = 0; j < 256; j++) { if (j > 0) { Console.Write (", "); } else { Console.Write ("\t\t"); } if (j % 4 == 0) { Console.WriteLine (); Console.Write ("\t\t"); } Console.Write ("0x"); Console.Write (table[i,j].ToString ("x16")); } Console.WriteLine (); Console.WriteLine ("\t},"); } Console.WriteLine ("};"); }
- Loading branch information
Showing
9 changed files
with
596 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
536 changes: 536 additions & 0 deletions
536
...Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/Crc64.Table.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters