Skip to content

Commit

Permalink
Reduce string allocations when parsing variable names.
Browse files Browse the repository at this point in the history
Signed-off-by: Bradley Grainger <bgrainger@gmail.com>
  • Loading branch information
bgrainger committed Jul 17, 2024
1 parent 8466783 commit 3326a0e
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions src/MySqlConnector/Protocol/Payloads/OkPayload.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Globalization;
using System.Buffers.Text;
using System.Text;
using MySqlConnector.Core;
using MySqlConnector.Protocol.Serialization;
Expand Down Expand Up @@ -85,24 +85,21 @@ public static void Verify(ReadOnlySpan<byte> span, IServerCapabilities serverCap
break;

case SessionTrackKind.SystemVariables:
var systemVariableOffset = reader.Offset + dataLength;
var systemVariablesEndOffset = reader.Offset + dataLength;
do
{
var variableSv = Encoding.ASCII.GetString(reader.ReadLengthEncodedByteString());
var lenSv = reader.ReadLengthEncodedIntegerOrNull();
var valueSv = lenSv == -1
? null
: Encoding.ASCII.GetString(reader.ReadByteString(lenSv));
switch (variableSv)
var systemVariableName = reader.ReadLengthEncodedByteString();
var systemVariableValueLength = reader.ReadLengthEncodedIntegerOrNull();
var systemVariableValue = systemVariableValueLength == -1 ? default : reader.ReadByteString(systemVariableValueLength);
if (systemVariableName.SequenceEqual("character_set_client"u8) && systemVariableValueLength != 0)
{
case "character_set_client":
clientCharacterSet = valueSv;
break;
case "connection_id":
connectionId = Convert.ToInt32(valueSv, CultureInfo.InvariantCulture);
break;
clientCharacterSet = Encoding.ASCII.GetString(systemVariableValue);
}
} while (reader.Offset < systemVariableOffset);
else if (systemVariableName.SequenceEqual("connection_id"u8))
{
connectionId = Utf8Parser.TryParse(systemVariableValue, out int parsedConnectionId, out var bytesConsumed) && bytesConsumed == systemVariableValue.Length ? parsedConnectionId : default(int?);
}
} while (reader.Offset < systemVariablesEndOffset);
break;

default:
Expand Down

0 comments on commit 3326a0e

Please sign in to comment.