Skip to content

Commit af52060

Browse files
authored
Revert "Rent byte buffers in SqlSequentialTextReader (#2356)"
This reverts commit 1bf025a.
1 parent ca9272d commit af52060

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlSequentialTextReader.cs

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

55
using System;
6-
using System.Buffers;
76
using System.Buffers.Binary;
87
using System.Diagnostics;
98
using System.Text;
@@ -19,8 +18,7 @@ sealed internal class SqlSequentialTextReader : System.IO.TextReader
1918
private readonly int _columnIndex; // The index of out column in the table
2019
private readonly Encoding _encoding; // Encoding for this character stream
2120
private readonly Decoder _decoder; // Decoder based on the encoding (NOTE: Decoders are stateful as they are designed to process streams of data)
22-
private byte[] _leftOverBytes; // Bytes leftover from the last Read() operation - this can be null if there were no bytes leftover
23-
private int _leftOverByteBufferUsed; //Number of bytes used from _leftOverBytes buffer - will be zero in case of null buffer
21+
private byte[] _leftOverBytes; // Bytes leftover from the last Read() operation - this can be null if there were no bytes leftover (Possible optimization: re-use the same array?)
2422
private int _peekedChar; // The last character that we peeked at (or -1 if we haven't peeked at anything)
2523
private Task _currentTask; // The current async task
2624
private readonly CancellationTokenSource _disposalTokenSource; // Used to indicate that a cancellation is requested due to disposal
@@ -359,18 +357,23 @@ private byte[] PrepareByteBuffer(int numberOfChars, out int byteBufferUsed)
359357

360358
if (_leftOverBytes != null)
361359
{
362-
// Copy over the leftover buffer
363-
byteBuffer = ArrayPool<byte>.Shared.Rent(byteBufferSize);
364-
Buffer.BlockCopy(_leftOverBytes, 0, byteBuffer, 0, _leftOverByteBufferUsed);
365-
byteBufferUsed = _leftOverByteBufferUsed;
366-
//return _leftOverBytes and clean _leftOverBytes reference
367-
ArrayPool<byte>.Shared.Return(_leftOverBytes);
368-
_leftOverBytes = null;
369-
_leftOverByteBufferUsed = 0;
360+
// If we have more leftover bytes than we need for this conversion, then just re-use the leftover buffer
361+
if (_leftOverBytes.Length > byteBufferSize)
362+
{
363+
byteBuffer = _leftOverBytes;
364+
byteBufferUsed = byteBuffer.Length;
365+
}
366+
else
367+
{
368+
// Otherwise, copy over the leftover buffer
369+
byteBuffer = new byte[byteBufferSize];
370+
Buffer.BlockCopy(_leftOverBytes, 0, byteBuffer, 0, _leftOverBytes.Length);
371+
byteBufferUsed = _leftOverBytes.Length;
372+
}
370373
}
371374
else
372375
{
373-
byteBuffer = ArrayPool<byte>.Shared.Rent(byteBufferSize);
376+
byteBuffer = new byte[byteBufferSize];
374377
byteBufferUsed = 0;
375378
}
376379
}
@@ -399,26 +402,14 @@ private int DecodeBytesToChars(byte[] inBuffer, int inBufferCount, char[] outBuf
399402
// completed may be false and there is no spare bytes if the Decoder has stored bytes to use later
400403
if ((!completed) && (bytesUsed < inBufferCount))
401404
{
402-
_leftOverByteBufferUsed = inBufferCount - bytesUsed;
403-
_leftOverBytes = ArrayPool<byte>.Shared.Rent(_leftOverByteBufferUsed);
404-
405-
Buffer.BlockCopy(inBuffer, bytesUsed, _leftOverBytes, 0, _leftOverByteBufferUsed);
405+
_leftOverBytes = new byte[inBufferCount - bytesUsed];
406+
Buffer.BlockCopy(inBuffer, bytesUsed, _leftOverBytes, 0, _leftOverBytes.Length);
406407
}
407408
else
408409
{
409410
// If Convert() sets completed to true, then it must have used all of the bytes we gave it
410411
Debug.Assert(bytesUsed >= inBufferCount, "Converted completed, but not all bytes were used");
411-
if (_leftOverBytes != null)
412-
{
413-
ArrayPool<byte>.Shared.Return(_leftOverBytes);
414-
_leftOverBytes = null;
415-
_leftOverByteBufferUsed = 0;
416-
}
417-
}
418-
419-
if (inBuffer.Length > 0)
420-
{
421-
ArrayPool<byte>.Shared.Return(inBuffer);
412+
_leftOverBytes = null;
422413
}
423414

424415
Debug.Assert(((_reader == null) || (_reader.ColumnDataBytesRemaining() > 0) || (!completed) || (_leftOverBytes == null)), "Stream has run out of data and the decoder finished, but there are leftover bytes");

0 commit comments

Comments
 (0)