Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 9b6618f

Browse files
Wraith2saurabh500
authored andcommitted
Optimize SqlClient connection pool performance (#33580)
* change pool to fetch user object only once and use direct tostirng change SSPI login buffers to arraypool allocations * change rental to keep track of rented array in case the caller reassigns * address feedback
1 parent e27c7da commit 9b6618f

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/System.Data.SqlClient/src/System/Data/ProviderBase/DbConnectionPoolIdentity.Windows.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ private static DbConnectionPoolIdentity GetCurrentNative()
2222
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
2323
{
2424
IntPtr token = identity.AccessToken.DangerousGetHandle();
25-
bool isNetwork = identity.User.IsWellKnown(WellKnownSidType.NetworkSid);
26-
string sidString = identity.User.Value;
25+
SecurityIdentifier user = identity.User;
26+
bool isNetwork = user.IsWellKnown(WellKnownSidType.NetworkSid);
27+
string sidString = user.Value;
2728

2829
// Win32NativeMethods.IsTokenRestricted will raise exception if the native call fails
2930
bool isRestricted = Win32NativeMethods.IsTokenRestrictedWrapper(token);

src/System.Data.SqlClient/src/System/Data/SqlClient/TdsParser.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Collections.Generic;
6+
using System.Buffers;
67
using System.Data.Common;
78
using System.Data.Sql;
89
using System.Data.SqlTypes;
@@ -6203,6 +6204,7 @@ internal void TdsLogin(SqlLogin rec, TdsEnums.FeatureExtension requestedFeatures
62036204
}
62046205

62056206
// allocate memory for SSPI variables
6207+
byte[] rentedSSPIBuff = null;
62066208
byte[] outSSPIBuff = null;
62076209
uint outSSPILength = 0;
62086210

@@ -6220,7 +6222,8 @@ internal void TdsLogin(SqlLogin rec, TdsEnums.FeatureExtension requestedFeatures
62206222
if (rec.useSSPI)
62216223
{
62226224
// now allocate proper length of buffer, and set length
6223-
outSSPIBuff = new byte[s_maxSSPILength];
6225+
rentedSSPIBuff = ArrayPool<byte>.Shared.Rent((int)s_maxSSPILength);
6226+
outSSPIBuff = rentedSSPIBuff;
62246227
outSSPILength = s_maxSSPILength;
62256228

62266229
// Call helper function for SSPI data and actual length.
@@ -6528,6 +6531,11 @@ internal void TdsLogin(SqlLogin rec, TdsEnums.FeatureExtension requestedFeatures
65286531
throw;
65296532
}
65306533

6534+
if (rentedSSPIBuff != null)
6535+
{
6536+
ArrayPool<byte>.Shared.Return(rentedSSPIBuff, clearArray: true);
6537+
}
6538+
65316539
_physicalStateObj.WritePacket(TdsEnums.HARDFLUSH);
65326540
_physicalStateObj.ResetSecurePasswordsInformation();
65336541
_physicalStateObj._pendingData = true;
@@ -6582,7 +6590,8 @@ private void ProcessSSPI(int receivedLength)
65826590
if (!result) { throw SQL.SynchronousCallMayNotPend(); }
65836591

65846592
// allocate send buffer and initialize length
6585-
byte[] sendBuff = new byte[s_maxSSPILength];
6593+
byte[] rentedSendBuff = ArrayPool<byte>.Shared.Rent((int)s_maxSSPILength);
6594+
byte[] sendBuff = rentedSendBuff;
65866595
uint sendLength = s_maxSSPILength;
65876596

65886597
// make call for SSPI data
@@ -6592,6 +6601,8 @@ private void ProcessSSPI(int receivedLength)
65926601
// DO NOT SEND LENGTH - TDS DOC INCORRECT! JUST SEND SSPI DATA!
65936602
_physicalStateObj.WriteByteArray(sendBuff, (int)sendLength, 0);
65946603

6604+
ArrayPool<byte>.Shared.Return(rentedSendBuff, clearArray: true);
6605+
65956606
// set message type so server knows its a SSPI response
65966607
_physicalStateObj._outputMessageType = TdsEnums.MT_SSPI;
65976608

0 commit comments

Comments
 (0)