File tree 4 files changed +63
-5
lines changed
benchmark/ICSharpCode.SharpZipLib.Benchmark
src/ICSharpCode.SharpZipLib/BZip2
4 files changed +63
-5
lines changed Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . IO ;
3
+ using BenchmarkDotNet . Attributes ;
4
+
5
+ namespace ICSharpCode . SharpZipLib . Benchmark . BZip2
6
+ {
7
+ [ Config ( typeof ( MultipleRuntimes ) ) ]
8
+ public class BZip2InputStream
9
+ {
10
+ private byte [ ] compressedData ;
11
+
12
+ public BZip2InputStream ( )
13
+ {
14
+ var outputMemoryStream = new MemoryStream ( ) ;
15
+ using ( var outputStream = new SharpZipLib . BZip2 . BZip2OutputStream ( outputMemoryStream ) )
16
+ {
17
+ var random = new Random ( 1234 ) ;
18
+ var inputData = new byte [ 1024 * 1024 * 30 ] ;
19
+ random . NextBytes ( inputData ) ;
20
+ var inputMemoryStream = new MemoryStream ( inputData ) ;
21
+ inputMemoryStream . CopyTo ( outputStream ) ;
22
+ }
23
+
24
+ compressedData = outputMemoryStream . ToArray ( ) ;
25
+ }
26
+
27
+ [ Benchmark ]
28
+ public void DecompressData ( )
29
+ {
30
+ var memoryStream = new MemoryStream ( compressedData ) ;
31
+ using ( var inputStream = new SharpZipLib . BZip2 . BZip2InputStream ( memoryStream ) )
32
+ {
33
+ inputStream . CopyTo ( Stream . Null ) ;
34
+ }
35
+ }
36
+ }
37
+ }
Original file line number Diff line number Diff line change 2
2
3
3
<PropertyGroup >
4
4
<OutputType >Exe</OutputType >
5
- <TargetFrameworks >netcoreapp2.1;net461</TargetFrameworks >
5
+ <TargetFrameworks >netcoreapp2.1;netcoreapp3.1; net461</TargetFrameworks >
6
6
</PropertyGroup >
7
7
8
8
<ItemGroup >
Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ public MultipleRuntimes()
13
13
{
14
14
AddJob ( Job . Default . WithToolchain ( CsProjClassicNetToolchain . Net461 ) . AsBaseline ( ) ) ; // NET 4.6.1
15
15
AddJob ( Job . Default . WithToolchain ( CsProjCoreToolchain . NetCoreApp21 ) ) ; // .NET Core 2.1
16
- //Add (Job.Default.With (CsProjCoreToolchain.NetCoreApp30 )); // .NET Core 3.0
16
+ AddJob ( Job . Default . WithToolchain ( CsProjCoreToolchain . NetCoreApp31 ) ) ; // .NET Core 3.1
17
17
}
18
18
}
19
19
Original file line number Diff line number Diff line change @@ -19,7 +19,11 @@ public class BZip2InputStream : Stream
19
19
private const int NO_RAND_PART_B_STATE = 6 ;
20
20
private const int NO_RAND_PART_C_STATE = 7 ;
21
21
22
- #endregion Constants
22
+ #if NETSTANDARD2_1
23
+ private static readonly int VectorSize = System . Numerics . Vector < byte > . Count ;
24
+ #endif
25
+
26
+ #endregion Constants
23
27
24
28
#region Instance Fields
25
29
@@ -711,10 +715,27 @@ cache misses.
711
715
unzftab [ seqToUnseq [ tmp ] ] ++ ;
712
716
ll8 [ last ] = seqToUnseq [ tmp ] ;
713
717
714
- for ( int j = nextSym - 1 ; j > 0 ; -- j )
718
+ var j = nextSym - 1 ;
719
+
720
+ #if ! NETSTANDARD2_0 && ! NETFRAMEWORK
721
+ // This is vectorized memory move. Going from the back, we're taking chunks of array
722
+ // and write them at the new location shifted by one. Since chunks are VectorSize long,
723
+ // at the end we have to move "tail" (or head actually) of the array using a plain loop.
724
+ // If System.Numerics.Vector API is not available, the plain loop is used to do the whole copying.
725
+
726
+ while ( j >= VectorSize )
715
727
{
716
- yy [ j ] = yy [ j - 1 ] ;
728
+ var arrayPart = new System . Numerics . Vector < byte > ( yy , j - VectorSize ) ;
729
+ arrayPart . CopyTo ( yy , j - VectorSize + 1 ) ;
730
+ j -= VectorSize ;
717
731
}
732
+ #endif
733
+
734
+ while ( j > 0 )
735
+ {
736
+ yy [ j ] = yy [ -- j ] ;
737
+ }
738
+
718
739
yy [ 0 ] = tmp ;
719
740
720
741
if ( groupPos == 0 )
You can’t perform that action at this time.
0 commit comments