Merge pull request #508 from turbedi/minor_optimizations

Minor optimizations
This commit is contained in:
Adam Hathcock
2020-04-11 09:08:07 +01:00
committed by GitHub
16 changed files with 40 additions and 41 deletions

View File

@@ -129,12 +129,12 @@ namespace SharpCompress.Archives.SevenZip
}
}
private static readonly byte[] SIGNATURE = {(byte)'7', (byte)'z', 0xBC, 0xAF, 0x27, 0x1C};
private static ReadOnlySpan<byte> SIGNATURE => new byte[] {(byte)'7', (byte)'z', 0xBC, 0xAF, 0x27, 0x1C};
private static bool SignatureMatch(Stream stream)
{
BinaryReader reader = new BinaryReader(stream);
byte[] signatureBytes = reader.ReadBytes(6);
ReadOnlySpan<byte> signatureBytes = reader.ReadBytes(6);
return signatureBytes.SequenceEqual(SIGNATURE);
}

View File

@@ -196,7 +196,7 @@ namespace SharpCompress.Common.Tar.Headers
{
name.CopyTo(buffer);
int i = Math.Min(length, name.Length);
buffer.Slice(i, length - i).Fill(0);
buffer.Slice(i, length - i).Clear();
}
private static void WriteStringBytes(string name, byte[] buffer, int offset, int length)

View File

@@ -211,7 +211,7 @@ namespace SharpCompress.Compressors.ADC
}
output = new byte[outPosition];
Array.Copy(buffer, 0, output, 0, outPosition);
Array.Copy(buffer, output, outPosition);
return position - start;
}
}

View File

@@ -372,17 +372,16 @@ namespace SharpCompress.Compressors.Deflate
{
return;
}
if (_fileName.IndexOf("/") != -1)
if (_fileName.Contains("/"))
{
_fileName = _fileName.Replace("/", "\\");
_fileName = _fileName.Replace('/', '\\');
}
if (_fileName.EndsWith("\\"))
{
throw new InvalidOperationException("Illegal filename");
}
var index = _fileName.IndexOf("\\");
if (index != -1)
if (_fileName.Contains("\\"))
{
// trim any leading path
int length = _fileName.Length;

View File

@@ -37,7 +37,7 @@ namespace SharpCompress.Compressors.Deflate64
// const tables used in decoding:
// Extra bits for length code 257 - 285.
private static readonly byte[] S_EXTRA_LENGTH_BITS =
private static ReadOnlySpan<byte> S_EXTRA_LENGTH_BITS => new byte[]
{ 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,16 };
// The base length for length code 257 - 285.
@@ -51,9 +51,9 @@ namespace SharpCompress.Compressors.Deflate64
{ 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,32769,49153 };
// code lengths for code length alphabet is stored in following order
private static readonly byte[] S_CODE_ORDER = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
private static ReadOnlySpan<byte> S_CODE_ORDER => new byte[] { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
private static readonly byte[] S_STATIC_DISTANCE_TREE_TABLE =
private static ReadOnlySpan<byte> S_STATIC_DISTANCE_TREE_TABLE => new byte[]
{
0x00,0x10,0x08,0x18,0x04,0x14,0x0c,0x1c,0x02,0x12,0x0a,0x1a,
0x06,0x16,0x0e,0x1e,0x01,0x11,0x09,0x19,0x05,0x15,0x0d,0x1d,
@@ -720,7 +720,7 @@ namespace SharpCompress.Compressors.Deflate64
byte[] distanceTreeCodeLength = new byte[HuffmanTree.MAX_DIST_TREE_ELEMENTS];
// Create literal and distance tables
Array.Copy(_codeList, 0, literalTreeCodeLength, 0, _literalLengthCodeCount);
Array.Copy(_codeList, literalTreeCodeLength, _literalLengthCodeCount);
Array.Copy(_codeList, _literalLengthCodeCount, distanceTreeCodeLength, 0, _distanceCodeCount);
// Make sure there is an end-of-block code, otherwise how could we ever end?

View File

@@ -137,7 +137,7 @@ namespace SharpCompress.Compressors.PPMd.H
private void RestartModelRare()
{
new Span<int>(_charMask).Fill(0);
new Span<int>(_charMask).Clear();
SubAlloc.InitSubAllocator();
_initRl = -(_maxOrder < 12 ? _maxOrder : 12) - 1;
int addr = SubAlloc.AllocContext();
@@ -228,7 +228,7 @@ namespace SharpCompress.Compressors.PPMd.H
private void ClearMask()
{
_escCount = 1;
new Span<int>(_charMask).Fill(0);
new Span<int>(_charMask).Clear();
}
internal bool DecodeInit(IRarUnpack unpackRead, int escChar)

View File

@@ -360,7 +360,7 @@ namespace SharpCompress.Compressors.PPMd.H
public virtual void InitSubAllocator()
{
int i, k;
new Span<byte>(_heap, _freeListPos, SizeOfFreeList()).Fill(0);
new Span<byte>(_heap, _freeListPos, SizeOfFreeList()).Clear();
_pText = _heapStart;

View File

@@ -58,7 +58,7 @@ namespace SharpCompress.Compressors.PPMd.I1
0x6051
};
private static readonly byte[] EXPONENTIAL_ESCAPES = {25, 14, 9, 7, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2};
private static ReadOnlySpan<byte> EXPONENTIAL_ESCAPES => new byte[] {25, 14, 9, 7, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2};
#region Public Methods

View File

@@ -729,13 +729,13 @@ namespace SharpCompress.Compressors.Rar.UnpackV1
if (!solid)
{
tablesRead = false;
new Span<int>(oldDist).Fill(0); // memset(oldDist,0,sizeof(OldDist));
new Span<int>(oldDist).Clear(); // memset(oldDist,0,sizeof(OldDist));
oldDistPtr = 0;
lastDist = 0;
lastLength = 0;
new Span<byte>(unpOldTable).Fill(0); // memset(UnpOldTable,0,sizeof(UnpOldTable));
new Span<byte>(unpOldTable).Clear(); // memset(UnpOldTable,0,sizeof(UnpOldTable));
unpPtr = 0;
wrPtr = 0;
@@ -837,7 +837,7 @@ WriteBorder=Math.Min(MaxWinSize,UNPACK_MAX_WRITE)&MaxWinMask;
if ((bitField & 0x4000) == 0)
{
new Span<byte>(unpOldTable).Fill(0); // memset(UnpOldTable,0,sizeof(UnpOldTable));
new Span<byte>(unpOldTable).Clear(); // memset(UnpOldTable,0,sizeof(UnpOldTable));
}
AddBits(2);
@@ -1109,7 +1109,7 @@ WriteBorder=Math.Min(MaxWinSize,UNPACK_MAX_WRITE)&MaxWinMask;
oldFilterLengths[FiltPos] = StackFilter.BlockLength;
// memset(StackFilter->Prg.InitR,0,sizeof(StackFilter->Prg.InitR));
new Span<int>(StackFilter.Program.InitR).Fill(0);
new Span<int>(StackFilter.Program.InitR).Clear();
StackFilter.Program.InitR[3] = RarVM.VM_GLOBALMEMADDR; // StackFilter->Prg.InitR[3]=VM_GLOBALMEMADDR;
StackFilter.Program.InitR[4] = StackFilter.BlockLength;

View File

@@ -652,9 +652,9 @@ namespace SharpCompress.Compressors.Rar.UnpackV1
ChSetC[I] = ((~I + 1) & 0xff) << 8;
}
new Span<int>(NToPl).Fill(0); // memset(NToPl,0,sizeof(NToPl));
new Span<int>(NToPlB).Fill(0); // memset(NToPlB,0,sizeof(NToPlB));
new Span<int>(NToPlC).Fill(0); // memset(NToPlC,0,sizeof(NToPlC));
new Span<int>(NToPl).Clear(); // memset(NToPl,0,sizeof(NToPl));
new Span<int>(NToPlB).Clear(); // memset(NToPlB,0,sizeof(NToPlB));
new Span<int>(NToPlC).Clear(); // memset(NToPlC,0,sizeof(NToPlC));
corrHuff(ChSetB, NToPlB);
}
@@ -670,7 +670,7 @@ namespace SharpCompress.Compressors.Rar.UnpackV1
// & ~0xff) | I;
}
}
new Span<int>(NumToPlace).Fill(0); // memset(NumToPlace,0,sizeof(NToPl));
new Span<int>(NumToPlace).Clear(); // memset(NumToPlace,0,sizeof(NToPl));
for (I = 6; I >= 0; I--)
{
NumToPlace[I] = (7 - I) * 32;

View File

@@ -38,7 +38,7 @@ namespace SharpCompress.Compressors.Rar.UnpackV1
56, 64, 80, 96, 112, 128, 160, 192, 224
};
private static readonly byte[] LBits =
private static ReadOnlySpan<byte> LBits => new byte[]
{
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,
4, 5, 5, 5, 5
@@ -263,7 +263,7 @@ namespace SharpCompress.Compressors.Rar.UnpackV1
if (0 == (BitField & 0x4000))
{
// memset(UnpOldTable20,0,sizeof(UnpOldTable20));
new Span<byte>(UnpOldTable20).Fill(0);
new Span<byte>(UnpOldTable20).Clear();
}
AddBits(2);
@@ -371,7 +371,7 @@ namespace SharpCompress.Compressors.Rar.UnpackV1
AudV[3] = new AudioVariables();
// memset(UnpOldTable20,0,sizeof(UnpOldTable20));
new Span<byte>(UnpOldTable20).Fill(0);
new Span<byte>(UnpOldTable20).Clear();
}
}

View File

@@ -187,7 +187,7 @@ namespace SharpCompress.Compressors.Rar.UnpackV1
int i;
long M, N;
new Span<int>(dec.DecodeNum).Fill(0); // memset(Dec->DecodeNum,0,Size*sizeof(*Dec->DecodeNum));
new Span<int>(dec.DecodeNum).Clear(); // memset(Dec->DecodeNum,0,Size*sizeof(*Dec->DecodeNum));
for (i = 0; i < size; i++)
{

View File

@@ -546,9 +546,9 @@ internal static class Unpack15Local {
ChSetA[I]=(ushort)I;
ChSetC[I]=(ushort)(((~I+1) & 0xff)<<8);
}
new Span<byte>(NToPl).Fill(0);
new Span<byte>(NToPlB).Fill(0);
new Span<byte>(NToPlC).Fill(0);
new Span<byte>(NToPl).Clear();
new Span<byte>(NToPlB).Clear();
new Span<byte>(NToPlC).Clear();
CorrHuff(ChSetB,NToPlB);
}
@@ -558,7 +558,7 @@ internal static class Unpack15Local {
for (I=7;I>=0;I--)
for (J=0;J<32;J++)
CharSet[J]=(ushort)((CharSet[J] & ~0xff) | I);
new Span<byte>(NumToPlace, 0, NToPl.Length).Fill(0);
new Span<byte>(NumToPlace, 0, NToPl.Length).Clear();
for (I=6;I>=0;I--)
NumToPlace[I]=(byte)((7-I)*32);
}

View File

@@ -231,7 +231,7 @@ internal static class Unpack20Local {
if ((BitField & 0x4000) != 0)
{
new Span<byte>(UnpOldTable20).Fill(0);
new Span<byte>(UnpOldTable20).Clear();
}
Inp.addbits(2);
@@ -328,7 +328,7 @@ internal static class Unpack20Local {
MakeDecodeTables(Table,(int)(NC20+DC20),BlockTables.RD,RC20);
}
//x memcpy(UnpOldTable20,Table,sizeof(UnpOldTable20));
Array.Copy(Table,0,UnpOldTable20,0,UnpOldTable20.Length);
Array.Copy(Table,UnpOldTable20,UnpOldTable20.Length);
return true;
}
@@ -363,7 +363,7 @@ internal static class Unpack20Local {
//memset(AudV,0,sizeof(AudV));
AudV = new AudioVariables[4];
new Span<byte>(UnpOldTable20).Fill(0);
new Span<byte>(UnpOldTable20).Clear();
//memset(MD,0,sizeof(MD));
MD = new DecodeTable[4];
}

View File

@@ -220,7 +220,7 @@ public Unpack(/* ComprDataIO *DataIO */)
{
if (!Solid)
{
new Span<uint>(OldDist).Fill(0);
new Span<uint>(OldDist).Clear();
OldDistPtr=0;
LastDist=LastLength=0;
// memset(Window,0,MaxWinSize);
@@ -270,7 +270,7 @@ public Unpack(/* ComprDataIO *DataIO */)
// Set the entire DecodeNum to zero.
//memset(Dec->DecodeNum,0,Size*sizeof(*Dec->DecodeNum));
new Span<ushort>(Dec.DecodeNum).Fill(0);
new Span<ushort>(Dec.DecodeNum).Clear();
// Initialize not really used entry for zero length code.
Dec.DecodePos[0]=0;
@@ -304,7 +304,7 @@ public Unpack(/* ComprDataIO *DataIO */)
// so we cannot use the original DecodePos.
uint[] CopyDecodePos = new uint[Dec.DecodePos.Length];
//memcpy(CopyDecodePos,Dec->DecodePos,sizeof(CopyDecodePos));
Array.Copy(Dec.DecodePos, 0, CopyDecodePos, 0, CopyDecodePos.Length);
Array.Copy(Dec.DecodePos, CopyDecodePos, CopyDecodePos.Length);
// For every bit length in the bit length table and so for every item
// of alphabet.

View File

@@ -9,7 +9,7 @@ namespace SharpCompress.Crypto
private static readonly int MAXKC = (256 / 4);
private static readonly byte[] Logtable =
private static ReadOnlySpan<byte> Logtable => new byte[]
{
0, 0, 25, 1, 50, 2, 26, 198,
75, 199, 27, 104, 51, 238, 223, 3,
@@ -45,7 +45,7 @@ namespace SharpCompress.Crypto
13, 99, 140, 128, 192, 247, 112, 7
};
private static readonly byte[] Alogtable =
private static ReadOnlySpan<byte> Alogtable => new byte[]
{
0, 3, 5, 15, 17, 51, 85, 255, 26, 46, 114, 150, 161, 248, 19, 53,
95, 225, 56, 72, 216, 115, 149, 164, 247, 2, 6, 10, 30, 34, 102, 170,
@@ -121,7 +121,7 @@ namespace SharpCompress.Crypto
23, 43, 4, 126, 186, 119, 214, 38, 225, 105, 20, 99, 85, 33, 12, 125
};
private static readonly byte[] rcon =
private static ReadOnlySpan<byte> rcon => new byte[]
{
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a,
0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91