Skip to content

Commit

Permalink
Prefer readonlyspan properties over readonly array fields (#59670)
Browse files Browse the repository at this point in the history
* Fix violations in System.IO.Compression

* Fix System.Private.DataContractSerialization

* Fix System.Net.HttpListener

* Fix System.Text.Encoding.CodePages

* Revert changes to .editorconfig

* Apply suggestions from code review

Co-authored-by: Günther Foidl <gue@korporal.at>

Co-authored-by: Günther Foidl <gue@korporal.at>
  • Loading branch information
NewellClark and gfoidl committed Sep 29, 2021
1 parent 06a2088 commit 4590efe
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,4 @@ indent_size = 2
end_of_line = lf
[*.{cmd,bat}]
end_of_line = crlf

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal sealed class InflaterManaged
// const tables used in decoding:

// Extra bits for length code 257 - 285.
private static readonly byte[] s_extraLengthBits =
private static ReadOnlySpan<byte> ExtraLengthBits => 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
Expand All @@ -33,9 +33,9 @@ internal sealed class InflaterManaged
};

// code lengths for code length alphabet is stored in following order
private static readonly byte[] s_codeOrder = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
private static ReadOnlySpan<byte> CodeOrder => 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_staticDistanceTreeTable =
private static ReadOnlySpan<byte> StaticDistanceTreeTable => 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,
Expand Down Expand Up @@ -388,11 +388,11 @@ private bool DecodeBlock(out bool end_of_block_code_seen)
}
else
{
if (symbol < 0 || symbol >= s_extraLengthBits.Length)
if ((uint)symbol >= ExtraLengthBits.Length)
{
throw new InvalidDataException(SR.GenericInvalidData);
}
_extraBits = s_extraLengthBits[symbol];
_extraBits = ExtraLengthBits[symbol];
Debug.Assert(_extraBits != 0, "We handle other cases separately!");
}
_length = symbol;
Expand Down Expand Up @@ -431,7 +431,7 @@ private bool DecodeBlock(out bool end_of_block_code_seen)
_distanceCode = _input.GetBits(5);
if (_distanceCode >= 0)
{
_distanceCode = s_staticDistanceTreeTable[_distanceCode];
_distanceCode = StaticDistanceTreeTable[_distanceCode];
}
}

Expand Down Expand Up @@ -544,13 +544,13 @@ private bool DecodeDynamicBlockHeader()
{
return false;
}
_codeLengthTreeCodeLength[s_codeOrder[_loopCounter]] = (byte)bits;
_codeLengthTreeCodeLength[CodeOrder[_loopCounter]] = (byte)bits;
++_loopCounter;
}

for (int i = _codeLengthCodeCount; i < s_codeOrder.Length; i++)
for (int i = _codeLengthCodeCount; i < CodeOrder.Length; i++)
{
_codeLengthTreeCodeLength[s_codeOrder[i]] = 0;
_codeLengthTreeCodeLength[CodeOrder[i]] = 0;
}

// create huffman tree for code length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ private void UpdateAfterWrite(uint dataWritten)
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, "dataWritten:" + dataWritten + " _leftToWrite:" + _leftToWrite + " _closed:" + _closed);
}

private static readonly byte[] s_chunkTerminator = new byte[] { (byte)'0', (byte)'\r', (byte)'\n', (byte)'\r', (byte)'\n' };
private static ReadOnlySpan<byte> ChunkTerminator => new byte[] { (byte)'0', (byte)'\r', (byte)'\n', (byte)'\r', (byte)'\n' };

private void DisposeCore()
{
Expand All @@ -303,15 +303,15 @@ private void DisposeCore()
{
flags |= Interop.HttpApi.HTTP_FLAGS.HTTP_SEND_RESPONSE_FLAG_DISCONNECT;
}
fixed (void* pBuffer = &s_chunkTerminator[0])
fixed (void* pBuffer = &MemoryMarshal.GetReference(ChunkTerminator))
{
Interop.HttpApi.HTTP_DATA_CHUNK* pDataChunk = null;
if (_httpContext.Response.BoundaryType == BoundaryType.Chunked)
{
Interop.HttpApi.HTTP_DATA_CHUNK dataChunk = default;
dataChunk.DataChunkType = Interop.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
dataChunk.pBuffer = (byte*)pBuffer;
dataChunk.BufferLength = (uint)s_chunkTerminator.Length;
dataChunk.BufferLength = (uint)ChunkTerminator.Length;
pDataChunk = &dataChunk;
}
if (!sentHeaders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal class XmlUTF8NodeWriter : XmlStreamNodeWriter
(byte)'e', (byte)'n', (byte)'c', (byte)'o', (byte)'d', (byte)'i', (byte)'n', (byte)'g', (byte)'=', (byte)'"', (byte)'u', (byte)'t', (byte)'f', (byte)'-', (byte)'8', (byte)'"',
(byte)'?', (byte)'>'
};
private static readonly byte[] s_digits =
private static ReadOnlySpan<byte> Digits => new byte[]
{
(byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7',
(byte) '8', (byte) '9', (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F'
Expand Down Expand Up @@ -638,7 +638,7 @@ private int ToBase16(byte[] chars, int offset, uint value)
do
{
count++;
chars[--offset] = s_digits[(int)(value & 0x0F)];
chars[--offset] = Digits[(int)(value & 0x0F)];
value /= 16;
}
while (value != 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public override unsafe int GetBytes(char* chars, int charCount, byte* bytes, int
$"[ISCIIEncoding.GetBytes]Expected indicTwoBytes from 1-3, not {(indicTwoBytes >> 12)}");

// Already did buffer checking, but...
if (!buffer.AddByte(s_SecondIndicByte[indicTwoBytes >> 12]))
if (!buffer.AddByte(SecondIndicByte[indicTwoBytes >> 12]))
break;
}
}
Expand Down Expand Up @@ -1936,7 +1936,7 @@ internal override bool HasState
// This is used if the UnicodeToIndic table 4 high bits are set, this is
// the value of the second Indic byte when applicable.
////////////////////////////////////////////////////////////////////////////
private static readonly byte[] s_SecondIndicByte =
private static ReadOnlySpan<byte> SecondIndicByte => new byte[]
{
0x00,
0xe9,
Expand Down

0 comments on commit 4590efe

Please sign in to comment.