Skip to content

Commit 4a38169

Browse files
committed
Replace ReadOnlySpan<byte> properties with locals.
The same C# compiler optimisation works for locals, and they're now scoped more appropriately.
1 parent bb39cd8 commit 4a38169

File tree

7 files changed

+36
-32
lines changed

7 files changed

+36
-32
lines changed

src/MySqlConnector/Core/BatchedCommandPayloadCreator.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,26 @@ public bool WriteQueryCommand(ref CommandListPosition commandListPosition, IDict
1515
writer.Write((byte) CommandKind.Multi);
1616
bool? firstResult = default;
1717
bool wroteCommand;
18+
ReadOnlySpan<byte> padding = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1819
do
1920
{
2021
// save room for command length
2122
var position = writer.Position;
22-
writer.Write(Padding);
23+
writer.Write(padding);
2324

2425
wroteCommand = SingleCommandPayloadCreator.Instance.WriteQueryCommand(ref commandListPosition, cachedProcedures, writer);
2526
firstResult ??= wroteCommand;
2627

2728
// write command length
28-
var commandLength = writer.Position - position - Padding.Length;
29+
var commandLength = writer.Position - position - padding.Length;
2930
var span = writer.ArraySegment.AsSpan().Slice(position);
3031
span[0] = 0xFE;
3132
BinaryPrimitives.WriteUInt64LittleEndian(span.Slice(1), (ulong) commandLength);
3233
} while (wroteCommand);
3334

3435
// remove the padding that was saved for the final command (which wasn't written)
35-
writer.TrimEnd(Padding.Length);
36+
writer.TrimEnd(padding.Length);
3637
return firstResult.Value;
3738
}
38-
39-
static ReadOnlySpan<byte> Padding => new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
4039
}
4140
}

src/MySqlConnector/Core/ServerVersion.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public ServerVersion(ReadOnlySpan<byte> versionString)
2727
if (versionString.Length != 0 && versionString[0] == 0x2D)
2828
{
2929
versionString = versionString.Slice(1);
30-
var mariaDbIndex = versionString.IndexOf(MariaDb);
30+
ReadOnlySpan<byte> mariaDb = new byte[] { 0x2D, 0x4D, 0x61, 0x72, 0x69, 0x61, 0x44, 0x42 }; // -MariaDB
31+
var mariaDbIndex = versionString.IndexOf(mariaDb);
3132
if (mariaDbIndex != -1)
3233
{
3334
var totalBytesRead = 0;
@@ -62,7 +63,5 @@ private ServerVersion()
6263
OriginalString = "";
6364
Version = new Version(0, 0);
6465
}
65-
66-
static ReadOnlySpan<byte> MariaDb => new byte[] { 0x2D, 0x4D, 0x61, 0x72, 0x69, 0x61, 0x44, 0x42 }; // -MariaDB
6766
}
6867
}

src/MySqlConnector/Core/SingleCommandPayloadCreator.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,19 +193,25 @@ private static bool WriteCommand(IMySqlCommand command, ByteBufferWriter writer)
193193
var isSchemaOnly = (command.CommandBehavior & CommandBehavior.SchemaOnly) != 0;
194194
var isSingleRow = (command.CommandBehavior & CommandBehavior.SingleRow) != 0;
195195
if (isSchemaOnly)
196-
writer.Write(SetSqlSelectLimit0);
196+
{
197+
ReadOnlySpan<byte> setSqlSelectLimit0 = new byte[] { 83, 69, 84, 32, 115, 113, 108, 95, 115, 101, 108, 101, 99, 116, 95, 108, 105, 109, 105, 116, 61, 48, 59, 10 }; // SET sql_select_limit=0;\n
198+
writer.Write(setSqlSelectLimit0);
199+
}
197200
else if (isSingleRow)
198-
writer.Write(SetSqlSelectLimit1);
201+
{
202+
ReadOnlySpan<byte> setSqlSelectLimit1 = new byte[] { 83, 69, 84, 32, 115, 113, 108, 95, 115, 101, 108, 101, 99, 116, 95, 108, 105, 109, 105, 116, 61, 49, 59, 10 }; // SET sql_select_limit=1;\n
203+
writer.Write(setSqlSelectLimit1);
204+
}
199205
var preparer = new StatementPreparer(command.CommandText!, command.RawParameters, command.CreateStatementPreparerOptions());
200206
var isComplete = preparer.ParseAndBindParameters(writer);
201207
if (isComplete && (isSchemaOnly || isSingleRow))
202-
writer.Write(ClearSqlSelectLimit);
208+
{
209+
ReadOnlySpan<byte> clearSqlSelectLimit = new byte[] { 10, 83, 69, 84, 32, 115, 113, 108, 95, 115, 101, 108, 101, 99, 116, 95, 108, 105, 109, 105, 116, 61, 100, 101, 102, 97, 117, 108, 116, 59 }; // \nSET sql_select_limit=default;
210+
writer.Write(clearSqlSelectLimit);
211+
}
203212
return isComplete;
204213
}
205214

206-
static ReadOnlySpan<byte> SetSqlSelectLimit0 => new byte[] { 83, 69, 84, 32, 115, 113, 108, 95, 115, 101, 108, 101, 99, 116, 95, 108, 105, 109, 105, 116, 61, 48, 59, 10 }; // SET sql_select_limit=0;\n
207-
static ReadOnlySpan<byte> SetSqlSelectLimit1 => new byte[] { 83, 69, 84, 32, 115, 113, 108, 95, 115, 101, 108, 101, 99, 116, 95, 108, 105, 109, 105, 116, 61, 49, 59, 10 }; // SET sql_select_limit=1;\n
208-
static ReadOnlySpan<byte> ClearSqlSelectLimit => new byte[] { 10, 83, 69, 84, 32, 115, 113, 108, 95, 115, 101, 108, 101, 99, 116, 95, 108, 105, 109, 105, 116, 61, 100, 101, 102, 97, 117, 108, 116, 59 }; // \nSET sql_select_limit=default;
209215
static readonly IMySqlConnectorLogger Log = MySqlConnectorLogManager.CreateLogger(nameof(SingleCommandPayloadCreator));
210216
}
211217
}

src/MySqlConnector/MySqlBulkCopy.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,13 +425,14 @@ static bool WriteValue(MySqlConnection connection, object? value, ref int inputI
425425

426426
if (value is null || value == DBNull.Value)
427427
{
428-
if (output.Length < EscapedNull.Length)
428+
ReadOnlySpan<byte> escapedNull = new byte[] { 0x5C, 0x4E };
429+
if (output.Length < escapedNull.Length)
429430
{
430431
bytesWritten = 0;
431432
return false;
432433
}
433-
EscapedNull.CopyTo(output);
434-
bytesWritten = EscapedNull.Length;
434+
escapedNull.CopyTo(output);
435+
bytesWritten = escapedNull.Length;
435436
return true;
436437
}
437438
else if (value is string stringValue)
@@ -648,7 +649,6 @@ static bool WriteBytes(ReadOnlySpan<byte> value, ref int inputIndex, Span<byte>
648649
static void WriteNibble(int value, Span<byte> output) => output[0] = value < 10 ? (byte) (value + 0x30) : (byte) (value + 0x57);
649650
}
650651

651-
private static ReadOnlySpan<byte> EscapedNull => new byte[] { 0x5C, 0x4E };
652652
private static readonly char[] s_specialCharacters = new char[] { '\t', '\\', '\n' };
653653
private static readonly IMySqlConnectorLogger Log = MySqlConnectorLogManager.CreateLogger(nameof(MySqlBulkCopy));
654654

src/MySqlConnector/MySqlParameter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ internal void AppendSqlString(ByteBufferWriter writer, StatementPreparerOptions
214214

215215
if (Value is null || Value == DBNull.Value)
216216
{
217-
writer.Write(NullBytes);
217+
ReadOnlySpan<byte> nullBytes = new byte[] { 0x4E, 0x55, 0x4C, 0x4C }; // NULL
218+
writer.Write(nullBytes);
218219
}
219220
else if (Value is string stringValue)
220221
{
@@ -309,7 +310,9 @@ internal void AppendSqlString(ByteBufferWriter writer, StatementPreparerOptions
309310
}
310311
else if (Value is bool boolValue)
311312
{
312-
writer.Write(boolValue ? TrueBytes : FalseBytes);
313+
ReadOnlySpan<byte> trueBytes = new byte[] { 0x74, 0x72, 0x75, 0x65 }; // true
314+
ReadOnlySpan<byte> falseBytes = new byte[] { 0x66, 0x61, 0x6C, 0x73, 0x65 }; // false
315+
writer.Write(boolValue ? trueBytes : falseBytes);
313316
}
314317
else if (Value is float || Value is double)
315318
{
@@ -681,9 +684,6 @@ private static void WriteTime(ByteBufferWriter writer, TimeSpan timeSpan)
681684
}
682685
}
683686

684-
static ReadOnlySpan<byte> NullBytes => new byte[] { 0x4E, 0x55, 0x4C, 0x4C }; // NULL
685-
static ReadOnlySpan<byte> TrueBytes => new byte[] { 0x74, 0x72, 0x75, 0x65 }; // true
686-
static ReadOnlySpan<byte> FalseBytes => new byte[] { 0x66, 0x61, 0x6C, 0x73, 0x65 }; // false
687687
static ReadOnlySpan<byte> BinaryBytes => new byte[] { 0x5F, 0x62, 0x69, 0x6E, 0x61, 0x72, 0x79, 0x27 }; // _binary'
688688

689689
DbType m_dbType;

src/MySqlConnector/Protocol/Payloads/HandshakeResponse41Payload.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ private static ByteBufferWriter CreateCapabilitiesPayload(ProtocolCapabilities s
3030
additionalCapabilities));
3131
writer.Write(0x4000_0000);
3232
writer.Write((byte) characterSet);
33-
writer.Write(Padding);
33+
34+
// NOTE: not new byte[19]; see https://github.com/dotnet/roslyn/issues/33088
35+
ReadOnlySpan<byte> padding = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
36+
writer.Write(padding);
37+
3438
if ((serverCapabilities & ProtocolCapabilities.LongPassword) == 0)
3539
{
3640
// MariaDB writes extended capabilities at the end of the padding
@@ -67,8 +71,5 @@ public static PayloadData Create(InitialHandshakePayload handshake, ConnectionSe
6771

6872
return writer.ToPayloadData();
6973
}
70-
71-
// NOTE: not new byte[19]; see https://github.com/dotnet/roslyn/issues/33088
72-
static ReadOnlySpan<byte> Padding => new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
7374
}
7475
}

src/MySqlConnector/Utilities/Utility.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ private static RSAParameters GetRsaParameters(ReadOnlySpan<byte> data, bool isPr
118118

119119
if (!isPrivate)
120120
{
121-
if (!data.Slice(0, s_rsaOid.Length).SequenceEqual(s_rsaOid))
121+
// encoded OID sequence for PKCS #1 rsaEncryption szOID_RSA_RSA = "1.2.840.113549.1.1.1"
122+
ReadOnlySpan<byte> rsaOid = new byte[] { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };
123+
if (!data.Slice(0, rsaOid.Length).SequenceEqual(rsaOid))
122124
throw new FormatException("Expected RSA OID but read {0}".FormatInvariant(BitConverter.ToString(data.Slice(0, 15).ToArray())));
123-
data = data.Slice(s_rsaOid.Length);
125+
data = data.Slice(rsaOid.Length);
124126

125127
// BIT STRING (0x03) followed by length
126128
if (data[0] != 0x03)
@@ -576,8 +578,5 @@ private static bool TryReadAsnInteger(ReadOnlySpan<byte> data, out ReadOnlySpan<
576578

577579
return true;
578580
}
579-
580-
// encoded OID sequence for PKCS #1 rsaEncryption szOID_RSA_RSA = "1.2.840.113549.1.1.1"
581-
private static ReadOnlySpan<byte> s_rsaOid => new byte[] { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };
582581
}
583582
}

0 commit comments

Comments
 (0)