Skip to content

Improve async string perf and fix reading chars with initial offset. #3377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13088,17 +13088,17 @@ bool writeDataSizeToSnapshot
||
(buff.Length >= offst + len)
||
(buff.Length == (startOffsetByteCount >> 1) + 1),
(buff.Length >= (startOffsetByteCount >> 1) + 1),
"Invalid length sent to ReadPlpUnicodeChars()!"
);
charsLeft = len;

// If total length is known up front, the length isn't specified as unknown
// and the caller doesn't pass int.max/2 indicating that it doesn't know the length
// allocate the whole buffer in one shot instead of realloc'ing and copying over each time
if (buff == null && stateObj._longlen != TdsEnums.SQL_PLP_UNKNOWNLEN && len < (int.MaxValue >> 1))
// If total data length is known up front from the plp header by being not SQL_PLP_UNKNOWNLEN
// and the number of chars required is less than int.max/2 allocate the entire buffer now to avoid
// later needing to repeatedly allocate new target buffers and copy data as we discover new data
if (buff == null && stateObj._longlen != TdsEnums.SQL_PLP_UNKNOWNLEN && stateObj._longlen < (int.MaxValue >> 1))
{
if (supportRentedBuff && len < 1073741824) // 1 Gib
if (supportRentedBuff && stateObj._longlen < 1073741824) // 1 Gib
{
buff = ArrayPool<char>.Shared.Rent((int)Math.Min((int)stateObj._longlen, len));
rentedBuff = true;
Expand Down Expand Up @@ -13133,8 +13133,7 @@ bool writeDataSizeToSnapshot

totalCharsRead = (startOffsetByteCount >> 1);
charsLeft -= totalCharsRead;
offst = totalCharsRead;

offst += totalCharsRead;

while (charsLeft > 0)
{
Expand All @@ -13152,7 +13151,10 @@ bool writeDataSizeToSnapshot
}
else
{
newbuf = new char[offst + charsRead];
// grow by an arbitrary number of packets to avoid needing to reallocate
// the newbuf on each loop iteration of long packet sequences which causes
// a performance problem as we spend large amounts of time copying and in gc
newbuf = new char[offst + charsRead + (stateObj.GetPacketSize() * 8)];
rentedBuff = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13267,7 +13267,7 @@ bool writeDataSizeToSnapshot
{
int charsRead = 0;
int charsLeft = 0;

if (stateObj._longlen == 0)
{
Debug.Assert(stateObj._longlenleft == 0);
Expand All @@ -13277,21 +13277,21 @@ bool writeDataSizeToSnapshot

Debug.Assert((ulong)stateObj._longlen != TdsEnums.SQL_PLP_NULL, "Out of sync plp read request");
Debug.Assert(
(buff == null && offst == 0)
||
(buff == null && offst == 0)
||
(buff.Length >= offst + len)
||
(buff.Length == (startOffsetByteCount >> 1) + 1),
(buff.Length >= (startOffsetByteCount >> 1) + 1),
"Invalid length sent to ReadPlpUnicodeChars()!"
);
charsLeft = len;

// If total length is known up front, the length isn't specified as unknown
// and the caller doesn't pass int.max/2 indicating that it doesn't know the length
// allocate the whole buffer in one shot instead of realloc'ing and copying over each time
if (buff == null && stateObj._longlen != TdsEnums.SQL_PLP_UNKNOWNLEN && len < (int.MaxValue >> 1))
if (buff == null && stateObj._longlen != TdsEnums.SQL_PLP_UNKNOWNLEN && stateObj._longlen < (int.MaxValue >> 1))
{
if (supportRentedBuff && len < 1073741824) // 1 Gib
if (supportRentedBuff && stateObj._longlen < 1073741824) // 1 Gib
{
buff = ArrayPool<char>.Shared.Rent((int)Math.Min((int)stateObj._longlen, len));
rentedBuff = true;
Expand Down Expand Up @@ -13326,9 +13326,9 @@ bool writeDataSizeToSnapshot

totalCharsRead = (startOffsetByteCount >> 1);
charsLeft -= totalCharsRead;
offst = totalCharsRead;
offst += totalCharsRead;


while (charsLeft > 0)
{
if (!partialReadInProgress)
Expand All @@ -13345,7 +13345,10 @@ bool writeDataSizeToSnapshot
}
else
{
newbuf = new char[offst + charsRead];
// grow by an arbitrary number of packets to avoid needing to reallocate
// the newbuf on each loop iteration of long packet sequences which causes
// a performance problem as we spend large amounts of time copying and in gc
newbuf = new char[offst + charsRead + (stateObj.GetPacketSize() * 8)];
rentedBuff = false;
}

Expand Down Expand Up @@ -13385,7 +13388,7 @@ bool writeDataSizeToSnapshot
&& (charsLeft > 0)
)
{
byte b1 = 0;
byte b1 = 0;
byte b2 = 0;
if (partialReadInProgress)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,11 @@ internal bool SetPacketSize(int size)
return false;
}

internal int GetPacketSize()
{
return _inBuff.Length;
}

///////////////////////////////////////
// Buffer read methods - data values //
///////////////////////////////////////
Expand Down Expand Up @@ -4027,6 +4032,7 @@ internal void CheckStack(string trace)
Debug.Assert(_stateObj._permitReplayStackTraceToDiffer || prev.Stack == trace, "The stack trace on subsequent replays should be the same");
}
}

#endif
public bool ContinueEnabled => !LocalAppContextSwitches.UseCompatibilityAsyncBehaviour;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Buffers;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlTypes;
Expand Down Expand Up @@ -268,7 +269,7 @@ first_name varchar(100) null,
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
public static void CheckNullRowVersionIsBDNull()
public static void CheckNullRowVersionIsDBNull()
{
lock (s_rowVersionLock)
{
Expand Down Expand Up @@ -659,6 +660,94 @@ DROP TABLE IF EXISTS [{tableName}]
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static async Task CanGetCharsSequentially()
{
const CommandBehavior commandBehavior = CommandBehavior.SequentialAccess | CommandBehavior.SingleResult;
const int length = 32000;
const string sqlCharWithArg = "SELECT CONVERT(BIGINT, 1) AS [Id], CONVERT(NVARCHAR(MAX), @input) AS [Value];";

using (var sqlConnection = new SqlConnection(DataTestUtility.TCPConnectionString))
{
await sqlConnection.OpenAsync();

StringBuilder inputBuilder = new StringBuilder(length);
Random random = new Random();
for (int i = 0; i < length; i++)
{
inputBuilder.Append((char)random.Next(0x30, 0x5A));
}
string input = inputBuilder.ToString();

using (var sqlCommand = new SqlCommand())
{
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandTimeout = 0;
sqlCommand.CommandText = sqlCharWithArg;
sqlCommand.Parameters.Add(new SqlParameter("@input", SqlDbType.NVarChar, -1) { Value = input });

using (var sqlReader = await sqlCommand.ExecuteReaderAsync(commandBehavior))
{
if (await sqlReader.ReadAsync())
{
long id = sqlReader.GetInt64(0);
if (id != 1)
{
Assert.Fail("Id not 1");
}

var sliced = GetPooledChars(sqlReader, 1, input);
if (!sliced.SequenceEqual(input.ToCharArray()))
{
Assert.Fail("sliced != input");
}
}
}
}
}

static char[] GetPooledChars(SqlDataReader sqlDataReader, int ordinal, string input)
{
var buffer = ArrayPool<char>.Shared.Rent(8192);
int offset = 0;
while (true)
{
int read = (int)sqlDataReader.GetChars(ordinal, offset, buffer, offset, buffer.Length - offset);
if (read == 0)
{
break;
}

ReadOnlySpan<char> fetched = buffer.AsSpan(offset, read);
ReadOnlySpan<char> origin = input.AsSpan(offset, read);

if (!fetched.Equals(origin, StringComparison.Ordinal))
{
Assert.Fail($"chunk (start:{offset}, for:{read}), is not the same as the input");
}

offset += read;

if (buffer.Length - offset < 128)
{
buffer = Resize(buffer);
}
}

var sliced = buffer.AsSpan(0, offset).ToArray();
ArrayPool<char>.Shared.Return(buffer);
return sliced;

static char[] Resize(char[] buffer)
{
var newBuffer = ArrayPool<char>.Shared.Rent(buffer.Length * 2);
Array.Copy(buffer, newBuffer, buffer.Length);
ArrayPool<char>.Shared.Return(buffer);
return newBuffer;
}
}
}

// Synapse: Cannot find data type 'rowversion'.
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
public static void CheckLegacyNullRowVersionIsEmptyArray()
Expand Down
Loading