From bc5d796f26f113da34e259792548aa393dcb5cd4 Mon Sep 17 00:00:00 2001 From: Evgeny Akhtimirov Date: Sun, 21 Jan 2024 20:15:33 +0300 Subject: [PATCH] Perf | memory allocations and performance in SqlBuffer (#2300) --- .../src/Microsoft/Data/SqlClient/SqlBuffer.cs | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) 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 602666e455..53a9ea64ae 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBuffer.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBuffer.cs @@ -96,6 +96,10 @@ internal struct Storage internal long _int64; // also used to store Money, UtcDateTime, Date , and Time [FieldOffset(0)] internal Guid _guid; +#if NET8_0_OR_GREATER + [FieldOffset(0)] + internal SqlGuid _sqlGuid; +#endif [FieldOffset(0)] internal float _single; [FieldOffset(0)] @@ -109,6 +113,9 @@ internal struct Storage private bool _isNull; private StorageType _type; private Storage _value; +#if !NET8_0_OR_GREATER + private SqlGuid _sqlGuid; +#endif private object _object; // String, SqlBinary, SqlCachedBuffer, SqlString, SqlXml internal SqlBuffer() @@ -383,7 +390,11 @@ internal Guid Guid } else if (StorageType.SqlGuid == _type) { - return _value._guid; +#if NET8_0_OR_GREATER + return _value._sqlGuid.Value; +#else + return _sqlGuid.Value; +#endif } return (Guid)Value; } @@ -815,17 +826,26 @@ internal SqlGuid SqlGuid } else if (StorageType.SqlGuid == _type) { - return IsNull ? SqlGuid.Null : new SqlGuid(_value._guid); + if (IsNull) + { + return SqlGuid.Null; + } +#if NET8_0_OR_GREATER + return _value._sqlGuid; +#else + return _sqlGuid; +#endif } return (SqlGuid)SqlValue; // anything else we haven't thought of goes through boxing. } set { Debug.Assert(IsEmpty, "setting value a second time?"); - if (!value.IsNull) - { - _value._guid = value.Value; - } +#if NET8_0_OR_GREATER + _value._sqlGuid = value; +#else + _sqlGuid = value; +#endif _type = StorageType.SqlGuid; _isNull = value.IsNull; }