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 Oct 25, 2023
1 parent dc60f51 commit d941d59
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2411,10 +2411,5 @@ internal void CloneCleanupAltMetaDataSetArray()
_snapshot.CloneCleanupAltMetaDataSetArray();
}
}

sealed partial class StateSnapshot
{
internal byte[] _plpBuffer;
}
}
}
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 @@ -2030,6 +2030,8 @@ internal void Restore(TdsParserStateObject stateObj)
private TdsParserStateObject _stateObj;
private StateObjectData _replayStateData;

internal byte[] _plpBuffer;

private PacketData _lastPacket;
private PacketData _firstPacket;
private PacketData _current;
Expand Down Expand Up @@ -2149,7 +2151,7 @@ internal bool MoveNext()
_stateObj._inBuff = _current.Buffer;
_stateObj._inBytesUsed = 0;
_stateObj._inBytesRead = _current.Read;
_stateObj._snapshotReplay = true;
_stateObj._snapshotReplay = true;
retval = true;
}

Expand Down

0 comments on commit d941d59

Please sign in to comment.