Skip to content

Commit bc5d796

Browse files
committed
Perf | memory allocations and performance in SqlBuffer (#2300)
1 parent 318d870 commit bc5d796

File tree

1 file changed

+26
-6
lines changed
  • src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient

1 file changed

+26
-6
lines changed

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBuffer.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ internal struct Storage
9696
internal long _int64; // also used to store Money, UtcDateTime, Date , and Time
9797
[FieldOffset(0)]
9898
internal Guid _guid;
99+
#if NET8_0_OR_GREATER
100+
[FieldOffset(0)]
101+
internal SqlGuid _sqlGuid;
102+
#endif
99103
[FieldOffset(0)]
100104
internal float _single;
101105
[FieldOffset(0)]
@@ -109,6 +113,9 @@ internal struct Storage
109113
private bool _isNull;
110114
private StorageType _type;
111115
private Storage _value;
116+
#if !NET8_0_OR_GREATER
117+
private SqlGuid _sqlGuid;
118+
#endif
112119
private object _object; // String, SqlBinary, SqlCachedBuffer, SqlString, SqlXml
113120

114121
internal SqlBuffer()
@@ -383,7 +390,11 @@ internal Guid Guid
383390
}
384391
else if (StorageType.SqlGuid == _type)
385392
{
386-
return _value._guid;
393+
#if NET8_0_OR_GREATER
394+
return _value._sqlGuid.Value;
395+
#else
396+
return _sqlGuid.Value;
397+
#endif
387398
}
388399
return (Guid)Value;
389400
}
@@ -815,17 +826,26 @@ internal SqlGuid SqlGuid
815826
}
816827
else if (StorageType.SqlGuid == _type)
817828
{
818-
return IsNull ? SqlGuid.Null : new SqlGuid(_value._guid);
829+
if (IsNull)
830+
{
831+
return SqlGuid.Null;
832+
}
833+
#if NET8_0_OR_GREATER
834+
return _value._sqlGuid;
835+
#else
836+
return _sqlGuid;
837+
#endif
819838
}
820839
return (SqlGuid)SqlValue; // anything else we haven't thought of goes through boxing.
821840
}
822841
set
823842
{
824843
Debug.Assert(IsEmpty, "setting value a second time?");
825-
if (!value.IsNull)
826-
{
827-
_value._guid = value.Value;
828-
}
844+
#if NET8_0_OR_GREATER
845+
_value._sqlGuid = value;
846+
#else
847+
_sqlGuid = value;
848+
#endif
829849
_type = StorageType.SqlGuid;
830850
_isNull = value.IsNull;
831851
}

0 commit comments

Comments
 (0)