From 3326a0ec06a8b32776271a7c4de103e8088b422a Mon Sep 17 00:00:00 2001 From: Bradley Grainger Date: Tue, 16 Jul 2024 19:23:42 -0700 Subject: [PATCH] Reduce string allocations when parsing variable names. Signed-off-by: Bradley Grainger --- .../Protocol/Payloads/OkPayload.cs | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/MySqlConnector/Protocol/Payloads/OkPayload.cs b/src/MySqlConnector/Protocol/Payloads/OkPayload.cs index 8abc850cc..95e77959c 100644 --- a/src/MySqlConnector/Protocol/Payloads/OkPayload.cs +++ b/src/MySqlConnector/Protocol/Payloads/OkPayload.cs @@ -1,4 +1,4 @@ -using System.Globalization; +using System.Buffers.Text; using System.Text; using MySqlConnector.Core; using MySqlConnector.Protocol.Serialization; @@ -85,24 +85,21 @@ public static void Verify(ReadOnlySpan 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: