Skip to content

Commit 342e411

Browse files
committed
4. Merging "Buffer read" methods of TdsParserStateObject, port dotnet#285 to netfx.
1 parent 4d1c0eb commit 342e411

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.netcore.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,8 +2445,6 @@ partial void SetStackInternal(string value)
24452445
private PacketData _snapshotInBuffList;
24462446
private PacketData _sparePacket;
24472447

2448-
internal byte[] _plpBuffer;
2449-
24502448
private int _snapshotInBuffCount;
24512449

24522450
#if DEBUG

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.netfx.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ internal bool TryReadPlpBytes(ref byte[] buff, int offset, int len, out int tota
393393
Debug.Assert(_longlenleft == 0);
394394
if (buff == null)
395395
{
396-
buff = new byte[0];
396+
buff = Array.Empty<byte>();
397397
}
398398

399399
AssertValidState();
@@ -409,7 +409,19 @@ internal bool TryReadPlpBytes(ref byte[] buff, int offset, int len, out int tota
409409
// If total length is known up front, allocate the whole buffer in one shot instead of realloc'ing and copying over each time
410410
if (buff == null && _longlen != TdsEnums.SQL_PLP_UNKNOWNLEN)
411411
{
412-
buff = new byte[(Math.Min((int)_longlen, len))];
412+
if (_snapshot != null)
413+
{
414+
// if there is a snapshot and it contains a stored plp buffer take it
415+
// and try to use it if it is the right length
416+
buff = _snapshot._plpBuffer;
417+
_snapshot._plpBuffer = null;
418+
}
419+
420+
if ((ulong)(buff?.Length ?? 0) != _longlen)
421+
{
422+
// if the buffer is null or the wrong length create one to use
423+
buff = new byte[(Math.Min((int)_longlen, len))];
424+
}
413425
}
414426

415427
if (_longlenleft == 0)
@@ -454,6 +466,12 @@ internal bool TryReadPlpBytes(ref byte[] buff, int offset, int len, out int tota
454466
_longlenleft -= (ulong)bytesRead;
455467
if (!result)
456468
{
469+
if (_snapshot != null)
470+
{
471+
// a partial read has happened so store the target buffer in the snapshot
472+
// so it can be re-used when another packet arrives and we read again
473+
_snapshot._plpBuffer = buff;
474+
}
457475
return false;
458476
}
459477

@@ -462,6 +480,12 @@ internal bool TryReadPlpBytes(ref byte[] buff, int offset, int len, out int tota
462480
// Read the next chunk or cleanup state if hit the end
463481
if (!TryReadPlpLength(false, out _))
464482
{
483+
if (_snapshot != null)
484+
{
485+
// a partial read has happened so store the target buffer in the snapshot
486+
// so it can be re-used when another packet arrives and we read again
487+
_snapshot._plpBuffer = buff;
488+
}
465489
return false;
466490
}
467491
}

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,8 @@ internal void Restore(TdsParserStateObject stateObj)
19281928
private TdsParserStateObject _stateObj;
19291929
private StateObjectData _replayStateData;
19301930

1931+
internal byte[] _plpBuffer;
1932+
19311933
#if DEBUG
19321934
private int _rollingPend = 0;
19331935
private int _rollingPendCount = 0;

0 commit comments

Comments
 (0)