From 54328bdbd429029273d0f48603a800f1c0317fd2 Mon Sep 17 00:00:00 2001 From: Evgeny Akhtimirov Date: Thu, 18 Jan 2024 12:38:14 +0300 Subject: [PATCH] Perf | avoid boxing of SqlGuid in SqlBuffer (#2300) --- .../netcore/src/Microsoft/Data/SqlClient/TdsParser.cs | 2 +- .../netfx/src/Microsoft/Data/SqlClient/TdsParser.cs | 2 +- .../Data/SqlClient/Server/ValueUtilsSmi.netfx.cs | 4 ---- .../src/Microsoft/Data/SqlClient/SqlBuffer.cs | 9 ++++++--- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs index 7e04f6b9c5..8dc72f355c 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -5504,7 +5504,7 @@ internal static object GetNullSqlValue(SqlBuffer nullVal, SqlMetaDataPriv md, Sq break; case SqlDbType.UniqueIdentifier: - nullVal.SqlGuid = SqlGuid.Null; + nullVal.SetToNullOfType(SqlBuffer.StorageType.SqlGuid); break; case SqlDbType.Bit: diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs index 7a9bbfdfd3..10ef7f8b33 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -6310,7 +6310,7 @@ internal static object GetNullSqlValue( break; case SqlDbType.UniqueIdentifier: - nullVal.SqlGuid = SqlGuid.Null; + nullVal.SetToNullOfType(SqlBuffer.StorageType.SqlGuid); break; case SqlDbType.Bit: diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.netfx.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.netfx.cs index 53bcedf146..0196687bf1 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.netfx.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.netfx.cs @@ -152,10 +152,6 @@ private static void GetNullOutputParameterSmi(SmiMetaData metaData, SqlBuffer ta // special case SqlBinary, 'cause tds parser never sets SqlBuffer to null, just to empty! targetBuffer.SqlBinary = SqlBinary.Null; } - else if (SqlBuffer.StorageType.SqlGuid == stype) - { - targetBuffer.SqlGuid = SqlGuid.Null; - } else { targetBuffer.SetToNullOfType(stype); diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBuffer.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBuffer.cs index 9cfc077da4..3783421b20 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBuffer.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBuffer.cs @@ -109,7 +109,7 @@ internal struct Storage private bool _isNull; private StorageType _type; private Storage _value; - private object _object; // String, SqlBinary, SqlCachedBuffer, SqlGuid, SqlString, SqlXml + private object _object; // String, SqlBinary, SqlCachedBuffer, SqlString, SqlXml internal SqlBuffer() { @@ -815,14 +815,17 @@ internal SqlGuid SqlGuid } else if (StorageType.SqlGuid == _type) { - return IsNull ? SqlGuid.Null : (SqlGuid)_object; + return IsNull ? SqlGuid.Null : new SqlGuid(_value._guid); } return (SqlGuid)SqlValue; // anything else we haven't thought of goes through boxing. } set { Debug.Assert(IsEmpty, "setting value a second time?"); - _object = value; + if (!value.IsNull) + { + _value._guid = value.Value; + } _type = StorageType.SqlGuid; _isNull = value.IsNull; }