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

Commit cb00bee

Browse files
Gene Leesaurabh500
authored andcommitted
SNIPacket performance improvement (#27187)
* SNIPacket Perf Improvement * comment fix
1 parent c163350 commit cb00bee

File tree

7 files changed

+162
-198
lines changed

7 files changed

+162
-198
lines changed

src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNIMarsConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode)
207207
};
208208

209209
_dataBytesLeft = (int)_currentHeader.length;
210-
_currentPacket = new SNIPacket(null);
210+
_currentPacket = new SNIPacket();
211211
_currentPacket.Allocate((int)_currentHeader.length);
212212
}
213213

src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNIMarsHandle.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private void SendControlPacket(SNISMUXFlags flags)
100100
GetSMUXHeaderBytes(0, (byte)flags, ref headerBytes);
101101
}
102102

103-
SNIPacket packet = new SNIPacket(null);
103+
SNIPacket packet = new SNIPacket();
104104
packet.SetData(headerBytes, SNISMUXHeader.HEADER_LENGTH);
105105

106106
_connection.Send(packet);
@@ -143,7 +143,7 @@ private SNIPacket GetSMUXEncapsulatedPacket(SNIPacket packet)
143143
byte[] headerBytes = null;
144144
GetSMUXHeaderBytes(packet.Length, (byte)SNISMUXFlags.SMUX_DATA, ref headerBytes);
145145

146-
SNIPacket smuxPacket = new SNIPacket(null);
146+
SNIPacket smuxPacket = new SNIPacket();
147147
smuxPacket.Description = string.Format("({0}) SMUX packet {1}", packet.Description == null ? "" : packet.Description, xSequenceNumber);
148148
smuxPacket.Allocate(16 + packet.Length);
149149
smuxPacket.AppendData(headerBytes, 16);

src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNINpHandle.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public override uint Receive(out SNIPacket packet, int timeout)
154154
packet = null;
155155
try
156156
{
157-
packet = new SNIPacket(null);
157+
packet = new SNIPacket();
158158
packet.Allocate(_bufferSize);
159159
packet.ReadFromStream(_stream);
160160

@@ -181,7 +181,7 @@ public override uint ReceiveAsync(ref SNIPacket packet, bool isMars = false)
181181
{
182182
lock (this)
183183
{
184-
packet = new SNIPacket(null);
184+
packet = new SNIPacket();
185185
packet.Allocate(_bufferSize);
186186

187187
try

src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNIPacket.cs

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,10 @@ internal class SNIPacket : IDisposable, IEquatable<SNIPacket>
1515
{
1616
private byte[] _data;
1717
private int _length;
18-
private int _capacity;
1918
private int _offset;
2019
private string _description;
2120
private SNIAsyncCallback _completionCallback;
2221

23-
/// <summary>
24-
/// Constructor
25-
/// </summary>
26-
/// <param name="handle">Owning SNI handle</param>
27-
public SNIPacket(SNIHandle handle)
28-
{
29-
_offset = 0;
30-
}
31-
3222
/// <summary>
3323
/// Packet description (used for debugging)
3424
/// </summary>
@@ -46,7 +36,7 @@ public string Description
4636
}
4737

4838
/// <summary>
49-
/// Data left to process
39+
/// Length of data left to process
5040
/// </summary>
5141
public int DataLeft
5242
{
@@ -67,9 +57,6 @@ public int Length
6757
}
6858
}
6959

70-
/// <summary>
71-
/// Packet validity
72-
/// </summary>
7360
public bool IsInvalid
7461
{
7562
get
@@ -78,14 +65,10 @@ public bool IsInvalid
7865
}
7966
}
8067

81-
/// <summary>
82-
/// Packet data
83-
/// </summary>
8468
public void Dispose()
8569
{
8670
_data = null;
87-
_length = 0;
88-
_capacity = 0;
71+
Release();
8972
}
9073

9174
/// <summary>
@@ -107,13 +90,18 @@ public void InvokeCompletionCallback(uint sniErrorCode)
10790
}
10891

10992
/// <summary>
110-
/// Allocate space for data
93+
/// Allocate byte array for data.
11194
/// </summary>
112-
/// <param name="capacity">Bytes to allocate</param>
113-
public void Allocate(int capacity)
95+
/// <param name="bufferSize">Minimum length of byte array to be allocated</param>
96+
public void Allocate(int bufferSize)
11497
{
115-
_capacity = capacity;
116-
_data = new Byte[capacity];
98+
if (_data == null || _data.Length != bufferSize)
99+
{
100+
_data = new byte[bufferSize];
101+
}
102+
103+
_length = 0;
104+
_offset = 0;
117105
}
118106

119107
/// <summary>
@@ -122,18 +110,20 @@ public void Allocate(int capacity)
122110
/// <returns>Cloned packet</returns>
123111
public SNIPacket Clone()
124112
{
125-
SNIPacket packet = new SNIPacket(null);
126-
packet._data = new byte[_length];
127-
Buffer.BlockCopy(_data, 0, packet._data, 0, _length);
113+
SNIPacket packet = new SNIPacket();
114+
packet._data = new byte[_data.Length];
115+
Buffer.BlockCopy(_data, 0, packet._data, 0, _data.Length);
128116
packet._length = _length;
117+
packet._description = _description;
118+
packet._completionCallback = _completionCallback;
129119

130120
return packet;
131121
}
132122

133123
/// <summary>
134124
/// Get packet data
135125
/// </summary>
136-
/// <param name="inBuff">Buffer</param>
126+
/// <param name="buffer">Buffer</param>
137127
/// <param name="dataSize">Data in packet</param>
138128
public void GetData(byte[] buffer, ref int dataSize)
139129
{
@@ -150,7 +140,6 @@ public void SetData(byte[] data, int length)
150140
{
151141
_data = data;
152142
_length = length;
153-
_capacity = length;
154143
_offset = 0;
155144
}
156145

@@ -208,7 +197,8 @@ public int TakeData(byte[] buffer, int dataOffset, int size)
208197
}
209198

210199
Buffer.BlockCopy(_data, _offset, buffer, dataOffset, size);
211-
_offset = _offset + size;
200+
_offset += size;
201+
212202
return size;
213203
}
214204

@@ -217,9 +207,7 @@ public int TakeData(byte[] buffer, int dataOffset, int size)
217207
/// </summary>
218208
public void Release()
219209
{
220-
_length = 0;
221-
_capacity = 0;
222-
_data = null;
210+
Reset();
223211
}
224212

225213
/// <summary>
@@ -228,7 +216,9 @@ public void Release()
228216
public void Reset()
229217
{
230218
_length = 0;
231-
_data = new byte[_capacity];
219+
_offset = 0;
220+
_description = null;
221+
_completionCallback = null;
232222
}
233223

234224
/// <summary>
@@ -249,7 +239,7 @@ public void ReadFromStreamAsync(Stream stream, SNIAsyncCallback callback, bool i
249239
options |= TaskContinuationOptions.LongRunning;
250240
}
251241

252-
stream.ReadAsync(_data, 0, _capacity).ContinueWith(t =>
242+
stream.ReadAsync(_data, 0, _data.Length).ContinueWith(t =>
253243
{
254244
Exception e = t.Exception != null ? t.Exception.InnerException : null;
255245
if (e != null)
@@ -270,7 +260,7 @@ public void ReadFromStreamAsync(Stream stream, SNIAsyncCallback callback, bool i
270260

271261
if (error)
272262
{
273-
this.Release();
263+
Release();
274264
}
275265

276266
callback(this, error ? TdsEnums.SNI_ERROR : TdsEnums.SNI_SUCCESS);
@@ -286,7 +276,7 @@ public void ReadFromStreamAsync(Stream stream, SNIAsyncCallback callback, bool i
286276
/// <param name="stream">Stream to read from</param>
287277
public void ReadFromStream(Stream stream)
288278
{
289-
_length = stream.Read(_data, 0, _capacity);
279+
_length = stream.Read(_data, 0, _data.Length);
290280
}
291281

292282
/// <summary>
@@ -298,6 +288,11 @@ public void WriteToStream(Stream stream)
298288
stream.Write(_data, 0, _length);
299289
}
300290

291+
public Task WriteToStreamAsync(Stream stream)
292+
{
293+
return stream.WriteAsync(_data, 0, _length);
294+
}
295+
301296
/// <summary>
302297
/// Get hash code
303298
/// </summary>

src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNIProxy.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ public uint SetConnectionBufferSize(SNIHandle handle, uint bufferSize)
189189
}
190190

191191
/// <summary>
192-
/// Get packet data
192+
/// Copies data packets in SNIPacket object to given byte array parameter
193193
/// </summary>
194-
/// <param name="packet">SNI packet</param>
195-
/// <param name="inBuff">Buffer</param>
196-
/// <param name="dataSize">Data size</param>
194+
/// <param name="packet">SNIPacket object containing data packets</param>
195+
/// <param name="inBuff">Destination byte array where data packets are copied to</param>
196+
/// <param name="dataSize">Length of data packets</param>
197197
/// <returns>SNI error status</returns>
198198
public uint PacketGetData(SNIPacket packet, byte[] inBuff, ref uint dataSize)
199199
{
@@ -240,11 +240,11 @@ public uint WritePacket(SNIHandle handle, SNIPacket packet, bool sync)
240240
{
241241
if (sync)
242242
{
243-
return handle.Send(packet.Clone());
243+
return handle.Send(packet);
244244
}
245245
else
246246
{
247-
return handle.SendAsync(packet.Clone());
247+
return handle.SendAsync(packet);
248248
}
249249
}
250250

@@ -426,8 +426,7 @@ private SNINpHandle CreateNpHandle(DataSource details, long timerExpire, object
426426
/// <returns>SNI error status</returns>
427427
public uint ReadAsync(SNIHandle handle, out SNIPacket packet, bool isMars = false)
428428
{
429-
packet = new SNIPacket(null);
430-
429+
packet = null;
431430
return handle.ReceiveAsync(ref packet);
432431
}
433432

0 commit comments

Comments
 (0)