Skip to content

Commit

Permalink
4. Merging "Buffer read" methods of TdsParserStateObject, port dotnet…
Browse files Browse the repository at this point in the history
…#285 to netfx.
  • Loading branch information
panoskj committed Sep 25, 2023
1 parent 4d1c0eb commit 342e411
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2445,8 +2445,6 @@ partial void SetStackInternal(string value)
private PacketData _snapshotInBuffList;
private PacketData _sparePacket;

internal byte[] _plpBuffer;

private int _snapshotInBuffCount;

#if DEBUG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ internal bool TryReadPlpBytes(ref byte[] buff, int offset, int len, out int tota
Debug.Assert(_longlenleft == 0);
if (buff == null)
{
buff = new byte[0];
buff = Array.Empty<byte>();
}

AssertValidState();
Expand All @@ -409,7 +409,19 @@ internal bool TryReadPlpBytes(ref byte[] buff, int offset, int len, out int tota
// If total length is known up front, allocate the whole buffer in one shot instead of realloc'ing and copying over each time
if (buff == null && _longlen != TdsEnums.SQL_PLP_UNKNOWNLEN)
{
buff = new byte[(Math.Min((int)_longlen, len))];
if (_snapshot != null)
{
// if there is a snapshot and it contains a stored plp buffer take it
// and try to use it if it is the right length
buff = _snapshot._plpBuffer;
_snapshot._plpBuffer = null;
}

if ((ulong)(buff?.Length ?? 0) != _longlen)
{
// if the buffer is null or the wrong length create one to use
buff = new byte[(Math.Min((int)_longlen, len))];
}
}

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

Expand All @@ -462,6 +480,12 @@ internal bool TryReadPlpBytes(ref byte[] buff, int offset, int len, out int tota
// Read the next chunk or cleanup state if hit the end
if (!TryReadPlpLength(false, out _))
{
if (_snapshot != null)
{
// a partial read has happened so store the target buffer in the snapshot
// so it can be re-used when another packet arrives and we read again
_snapshot._plpBuffer = buff;
}
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1928,6 +1928,8 @@ internal void Restore(TdsParserStateObject stateObj)
private TdsParserStateObject _stateObj;
private StateObjectData _replayStateData;

internal byte[] _plpBuffer;

#if DEBUG
private int _rollingPend = 0;
private int _rollingPendCount = 0;
Expand Down

0 comments on commit 342e411

Please sign in to comment.